Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // output.cc -- manage the output file for gold |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
a2fb1b05 ILT |
23 | #include "gold.h" |
24 | ||
25 | #include <cstdlib> | |
61ba1cf9 ILT |
26 | #include <cerrno> |
27 | #include <fcntl.h> | |
28 | #include <unistd.h> | |
29 | #include <sys/mman.h> | |
4e9d8586 | 30 | #include <sys/stat.h> |
75f65a3e | 31 | #include <algorithm> |
5ffcaa86 | 32 | #include "libiberty.h" // for unlink_if_ordinary() |
a2fb1b05 | 33 | |
7e1edb90 | 34 | #include "parameters.h" |
a2fb1b05 | 35 | #include "object.h" |
ead1e424 ILT |
36 | #include "symtab.h" |
37 | #include "reloc.h" | |
b8e6aad9 | 38 | #include "merge.h" |
a2fb1b05 ILT |
39 | #include "output.h" |
40 | ||
41 | namespace gold | |
42 | { | |
43 | ||
a3ad94ed ILT |
44 | // Output_data variables. |
45 | ||
27bc2bce | 46 | bool Output_data::allocated_sizes_are_fixed; |
a3ad94ed | 47 | |
a2fb1b05 ILT |
48 | // Output_data methods. |
49 | ||
50 | Output_data::~Output_data() | |
51 | { | |
52 | } | |
53 | ||
730cdc88 ILT |
54 | // Return the default alignment for the target size. |
55 | ||
56 | uint64_t | |
57 | Output_data::default_alignment() | |
58 | { | |
59 | return Output_data::default_alignment_for_size(parameters->get_size()); | |
60 | } | |
61 | ||
75f65a3e ILT |
62 | // Return the default alignment for a size--32 or 64. |
63 | ||
64 | uint64_t | |
730cdc88 | 65 | Output_data::default_alignment_for_size(int size) |
75f65a3e ILT |
66 | { |
67 | if (size == 32) | |
68 | return 4; | |
69 | else if (size == 64) | |
70 | return 8; | |
71 | else | |
a3ad94ed | 72 | gold_unreachable(); |
75f65a3e ILT |
73 | } |
74 | ||
75f65a3e ILT |
75 | // Output_section_header methods. This currently assumes that the |
76 | // segment and section lists are complete at construction time. | |
77 | ||
78 | Output_section_headers::Output_section_headers( | |
16649710 ILT |
79 | const Layout* layout, |
80 | const Layout::Segment_list* segment_list, | |
81 | const Layout::Section_list* unattached_section_list, | |
61ba1cf9 | 82 | const Stringpool* secnamepool) |
9025d29d | 83 | : layout_(layout), |
75f65a3e | 84 | segment_list_(segment_list), |
a3ad94ed | 85 | unattached_section_list_(unattached_section_list), |
61ba1cf9 | 86 | secnamepool_(secnamepool) |
75f65a3e | 87 | { |
61ba1cf9 ILT |
88 | // Count all the sections. Start with 1 for the null section. |
89 | off_t count = 1; | |
16649710 ILT |
90 | for (Layout::Segment_list::const_iterator p = segment_list->begin(); |
91 | p != segment_list->end(); | |
75f65a3e | 92 | ++p) |
ead1e424 ILT |
93 | if ((*p)->type() == elfcpp::PT_LOAD) |
94 | count += (*p)->output_section_count(); | |
16649710 | 95 | count += unattached_section_list->size(); |
75f65a3e | 96 | |
9025d29d | 97 | const int size = parameters->get_size(); |
75f65a3e ILT |
98 | int shdr_size; |
99 | if (size == 32) | |
100 | shdr_size = elfcpp::Elf_sizes<32>::shdr_size; | |
101 | else if (size == 64) | |
102 | shdr_size = elfcpp::Elf_sizes<64>::shdr_size; | |
103 | else | |
a3ad94ed | 104 | gold_unreachable(); |
75f65a3e ILT |
105 | |
106 | this->set_data_size(count * shdr_size); | |
107 | } | |
108 | ||
61ba1cf9 ILT |
109 | // Write out the section headers. |
110 | ||
75f65a3e | 111 | void |
61ba1cf9 | 112 | Output_section_headers::do_write(Output_file* of) |
a2fb1b05 | 113 | { |
9025d29d | 114 | if (parameters->get_size() == 32) |
61ba1cf9 | 115 | { |
9025d29d ILT |
116 | if (parameters->is_big_endian()) |
117 | { | |
118 | #ifdef HAVE_TARGET_32_BIG | |
119 | this->do_sized_write<32, true>(of); | |
120 | #else | |
121 | gold_unreachable(); | |
122 | #endif | |
123 | } | |
61ba1cf9 | 124 | else |
9025d29d ILT |
125 | { |
126 | #ifdef HAVE_TARGET_32_LITTLE | |
127 | this->do_sized_write<32, false>(of); | |
128 | #else | |
129 | gold_unreachable(); | |
130 | #endif | |
131 | } | |
61ba1cf9 | 132 | } |
9025d29d | 133 | else if (parameters->get_size() == 64) |
61ba1cf9 | 134 | { |
9025d29d ILT |
135 | if (parameters->is_big_endian()) |
136 | { | |
137 | #ifdef HAVE_TARGET_64_BIG | |
138 | this->do_sized_write<64, true>(of); | |
139 | #else | |
140 | gold_unreachable(); | |
141 | #endif | |
142 | } | |
61ba1cf9 | 143 | else |
9025d29d ILT |
144 | { |
145 | #ifdef HAVE_TARGET_64_LITTLE | |
146 | this->do_sized_write<64, false>(of); | |
147 | #else | |
148 | gold_unreachable(); | |
149 | #endif | |
150 | } | |
61ba1cf9 ILT |
151 | } |
152 | else | |
a3ad94ed | 153 | gold_unreachable(); |
61ba1cf9 ILT |
154 | } |
155 | ||
156 | template<int size, bool big_endian> | |
157 | void | |
158 | Output_section_headers::do_sized_write(Output_file* of) | |
159 | { | |
160 | off_t all_shdrs_size = this->data_size(); | |
161 | unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size); | |
162 | ||
163 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
164 | unsigned char* v = view; | |
165 | ||
166 | { | |
167 | typename elfcpp::Shdr_write<size, big_endian> oshdr(v); | |
168 | oshdr.put_sh_name(0); | |
169 | oshdr.put_sh_type(elfcpp::SHT_NULL); | |
170 | oshdr.put_sh_flags(0); | |
171 | oshdr.put_sh_addr(0); | |
172 | oshdr.put_sh_offset(0); | |
173 | oshdr.put_sh_size(0); | |
174 | oshdr.put_sh_link(0); | |
175 | oshdr.put_sh_info(0); | |
176 | oshdr.put_sh_addralign(0); | |
177 | oshdr.put_sh_entsize(0); | |
178 | } | |
179 | ||
180 | v += shdr_size; | |
181 | ||
ead1e424 | 182 | unsigned shndx = 1; |
16649710 ILT |
183 | for (Layout::Segment_list::const_iterator p = this->segment_list_->begin(); |
184 | p != this->segment_list_->end(); | |
61ba1cf9 | 185 | ++p) |
593f47df | 186 | v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
16649710 | 187 | this->layout_, this->secnamepool_, v, &shndx |
ead1e424 | 188 | SELECT_SIZE_ENDIAN(size, big_endian)); |
a3ad94ed | 189 | for (Layout::Section_list::const_iterator p = |
16649710 ILT |
190 | this->unattached_section_list_->begin(); |
191 | p != this->unattached_section_list_->end(); | |
61ba1cf9 ILT |
192 | ++p) |
193 | { | |
a3ad94ed | 194 | gold_assert(shndx == (*p)->out_shndx()); |
61ba1cf9 | 195 | elfcpp::Shdr_write<size, big_endian> oshdr(v); |
16649710 | 196 | (*p)->write_header(this->layout_, this->secnamepool_, &oshdr); |
61ba1cf9 | 197 | v += shdr_size; |
ead1e424 | 198 | ++shndx; |
61ba1cf9 ILT |
199 | } |
200 | ||
201 | of->write_output_view(this->offset(), all_shdrs_size, view); | |
a2fb1b05 ILT |
202 | } |
203 | ||
54dc6425 ILT |
204 | // Output_segment_header methods. |
205 | ||
61ba1cf9 | 206 | Output_segment_headers::Output_segment_headers( |
61ba1cf9 | 207 | const Layout::Segment_list& segment_list) |
9025d29d | 208 | : segment_list_(segment_list) |
61ba1cf9 | 209 | { |
9025d29d | 210 | const int size = parameters->get_size(); |
61ba1cf9 ILT |
211 | int phdr_size; |
212 | if (size == 32) | |
213 | phdr_size = elfcpp::Elf_sizes<32>::phdr_size; | |
214 | else if (size == 64) | |
215 | phdr_size = elfcpp::Elf_sizes<64>::phdr_size; | |
216 | else | |
a3ad94ed | 217 | gold_unreachable(); |
61ba1cf9 ILT |
218 | |
219 | this->set_data_size(segment_list.size() * phdr_size); | |
220 | } | |
221 | ||
54dc6425 | 222 | void |
61ba1cf9 | 223 | Output_segment_headers::do_write(Output_file* of) |
75f65a3e | 224 | { |
9025d29d | 225 | if (parameters->get_size() == 32) |
61ba1cf9 | 226 | { |
9025d29d ILT |
227 | if (parameters->is_big_endian()) |
228 | { | |
229 | #ifdef HAVE_TARGET_32_BIG | |
230 | this->do_sized_write<32, true>(of); | |
231 | #else | |
232 | gold_unreachable(); | |
233 | #endif | |
234 | } | |
61ba1cf9 | 235 | else |
9025d29d ILT |
236 | { |
237 | #ifdef HAVE_TARGET_32_LITTLE | |
61ba1cf9 | 238 | this->do_sized_write<32, false>(of); |
9025d29d ILT |
239 | #else |
240 | gold_unreachable(); | |
241 | #endif | |
242 | } | |
61ba1cf9 | 243 | } |
9025d29d | 244 | else if (parameters->get_size() == 64) |
61ba1cf9 | 245 | { |
9025d29d ILT |
246 | if (parameters->is_big_endian()) |
247 | { | |
248 | #ifdef HAVE_TARGET_64_BIG | |
249 | this->do_sized_write<64, true>(of); | |
250 | #else | |
251 | gold_unreachable(); | |
252 | #endif | |
253 | } | |
61ba1cf9 | 254 | else |
9025d29d ILT |
255 | { |
256 | #ifdef HAVE_TARGET_64_LITTLE | |
257 | this->do_sized_write<64, false>(of); | |
258 | #else | |
259 | gold_unreachable(); | |
260 | #endif | |
261 | } | |
61ba1cf9 ILT |
262 | } |
263 | else | |
a3ad94ed | 264 | gold_unreachable(); |
61ba1cf9 ILT |
265 | } |
266 | ||
267 | template<int size, bool big_endian> | |
268 | void | |
269 | Output_segment_headers::do_sized_write(Output_file* of) | |
270 | { | |
271 | const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size; | |
272 | off_t all_phdrs_size = this->segment_list_.size() * phdr_size; | |
273 | unsigned char* view = of->get_output_view(this->offset(), | |
274 | all_phdrs_size); | |
275 | unsigned char* v = view; | |
276 | for (Layout::Segment_list::const_iterator p = this->segment_list_.begin(); | |
277 | p != this->segment_list_.end(); | |
278 | ++p) | |
279 | { | |
280 | elfcpp::Phdr_write<size, big_endian> ophdr(v); | |
281 | (*p)->write_header(&ophdr); | |
282 | v += phdr_size; | |
283 | } | |
284 | ||
285 | of->write_output_view(this->offset(), all_phdrs_size, view); | |
75f65a3e ILT |
286 | } |
287 | ||
288 | // Output_file_header methods. | |
289 | ||
9025d29d | 290 | Output_file_header::Output_file_header(const Target* target, |
75f65a3e ILT |
291 | const Symbol_table* symtab, |
292 | const Output_segment_headers* osh) | |
9025d29d | 293 | : target_(target), |
75f65a3e | 294 | symtab_(symtab), |
61ba1cf9 | 295 | segment_header_(osh), |
75f65a3e ILT |
296 | section_header_(NULL), |
297 | shstrtab_(NULL) | |
298 | { | |
9025d29d | 299 | const int size = parameters->get_size(); |
61ba1cf9 ILT |
300 | int ehdr_size; |
301 | if (size == 32) | |
302 | ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size; | |
303 | else if (size == 64) | |
304 | ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size; | |
305 | else | |
a3ad94ed | 306 | gold_unreachable(); |
61ba1cf9 ILT |
307 | |
308 | this->set_data_size(ehdr_size); | |
75f65a3e ILT |
309 | } |
310 | ||
311 | // Set the section table information for a file header. | |
312 | ||
313 | void | |
314 | Output_file_header::set_section_info(const Output_section_headers* shdrs, | |
315 | const Output_section* shstrtab) | |
316 | { | |
317 | this->section_header_ = shdrs; | |
318 | this->shstrtab_ = shstrtab; | |
319 | } | |
320 | ||
321 | // Write out the file header. | |
322 | ||
323 | void | |
61ba1cf9 | 324 | Output_file_header::do_write(Output_file* of) |
54dc6425 | 325 | { |
27bc2bce ILT |
326 | gold_assert(this->offset() == 0); |
327 | ||
9025d29d | 328 | if (parameters->get_size() == 32) |
61ba1cf9 | 329 | { |
9025d29d ILT |
330 | if (parameters->is_big_endian()) |
331 | { | |
332 | #ifdef HAVE_TARGET_32_BIG | |
333 | this->do_sized_write<32, true>(of); | |
334 | #else | |
335 | gold_unreachable(); | |
336 | #endif | |
337 | } | |
61ba1cf9 | 338 | else |
9025d29d ILT |
339 | { |
340 | #ifdef HAVE_TARGET_32_LITTLE | |
341 | this->do_sized_write<32, false>(of); | |
342 | #else | |
343 | gold_unreachable(); | |
344 | #endif | |
345 | } | |
61ba1cf9 | 346 | } |
9025d29d | 347 | else if (parameters->get_size() == 64) |
61ba1cf9 | 348 | { |
9025d29d ILT |
349 | if (parameters->is_big_endian()) |
350 | { | |
351 | #ifdef HAVE_TARGET_64_BIG | |
352 | this->do_sized_write<64, true>(of); | |
353 | #else | |
354 | gold_unreachable(); | |
355 | #endif | |
356 | } | |
61ba1cf9 | 357 | else |
9025d29d ILT |
358 | { |
359 | #ifdef HAVE_TARGET_64_LITTLE | |
360 | this->do_sized_write<64, false>(of); | |
361 | #else | |
362 | gold_unreachable(); | |
363 | #endif | |
364 | } | |
61ba1cf9 ILT |
365 | } |
366 | else | |
a3ad94ed | 367 | gold_unreachable(); |
61ba1cf9 ILT |
368 | } |
369 | ||
370 | // Write out the file header with appropriate size and endianess. | |
371 | ||
372 | template<int size, bool big_endian> | |
373 | void | |
374 | Output_file_header::do_sized_write(Output_file* of) | |
375 | { | |
a3ad94ed | 376 | gold_assert(this->offset() == 0); |
61ba1cf9 ILT |
377 | |
378 | int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; | |
379 | unsigned char* view = of->get_output_view(0, ehdr_size); | |
380 | elfcpp::Ehdr_write<size, big_endian> oehdr(view); | |
381 | ||
382 | unsigned char e_ident[elfcpp::EI_NIDENT]; | |
383 | memset(e_ident, 0, elfcpp::EI_NIDENT); | |
384 | e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0; | |
385 | e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1; | |
386 | e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2; | |
387 | e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3; | |
388 | if (size == 32) | |
389 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32; | |
390 | else if (size == 64) | |
391 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64; | |
392 | else | |
a3ad94ed | 393 | gold_unreachable(); |
61ba1cf9 ILT |
394 | e_ident[elfcpp::EI_DATA] = (big_endian |
395 | ? elfcpp::ELFDATA2MSB | |
396 | : elfcpp::ELFDATA2LSB); | |
397 | e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT; | |
398 | // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION. | |
399 | oehdr.put_e_ident(e_ident); | |
400 | ||
401 | elfcpp::ET e_type; | |
7e1edb90 | 402 | if (parameters->output_is_object()) |
61ba1cf9 | 403 | e_type = elfcpp::ET_REL; |
436ca963 ILT |
404 | else if (parameters->output_is_shared()) |
405 | e_type = elfcpp::ET_DYN; | |
61ba1cf9 ILT |
406 | else |
407 | e_type = elfcpp::ET_EXEC; | |
408 | oehdr.put_e_type(e_type); | |
409 | ||
410 | oehdr.put_e_machine(this->target_->machine_code()); | |
411 | oehdr.put_e_version(elfcpp::EV_CURRENT); | |
412 | ||
ead1e424 | 413 | // FIXME: Need to support -e, and target specific entry symbol. |
61ba1cf9 ILT |
414 | Symbol* sym = this->symtab_->lookup("_start"); |
415 | typename Sized_symbol<size>::Value_type v; | |
416 | if (sym == NULL) | |
417 | v = 0; | |
418 | else | |
419 | { | |
420 | Sized_symbol<size>* ssym; | |
593f47df | 421 | ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) ( |
5482377d | 422 | sym SELECT_SIZE(size)); |
61ba1cf9 ILT |
423 | v = ssym->value(); |
424 | } | |
425 | oehdr.put_e_entry(v); | |
426 | ||
427 | oehdr.put_e_phoff(this->segment_header_->offset()); | |
428 | oehdr.put_e_shoff(this->section_header_->offset()); | |
429 | ||
430 | // FIXME: The target needs to set the flags. | |
431 | oehdr.put_e_flags(0); | |
432 | ||
433 | oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size); | |
434 | oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size); | |
435 | oehdr.put_e_phnum(this->segment_header_->data_size() | |
436 | / elfcpp::Elf_sizes<size>::phdr_size); | |
437 | oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size); | |
438 | oehdr.put_e_shnum(this->section_header_->data_size() | |
439 | / elfcpp::Elf_sizes<size>::shdr_size); | |
ead1e424 | 440 | oehdr.put_e_shstrndx(this->shstrtab_->out_shndx()); |
61ba1cf9 ILT |
441 | |
442 | of->write_output_view(0, ehdr_size, view); | |
54dc6425 ILT |
443 | } |
444 | ||
dbe717ef ILT |
445 | // Output_data_const methods. |
446 | ||
447 | void | |
a3ad94ed | 448 | Output_data_const::do_write(Output_file* of) |
dbe717ef | 449 | { |
a3ad94ed ILT |
450 | of->write(this->offset(), this->data_.data(), this->data_.size()); |
451 | } | |
452 | ||
453 | // Output_data_const_buffer methods. | |
454 | ||
455 | void | |
456 | Output_data_const_buffer::do_write(Output_file* of) | |
457 | { | |
458 | of->write(this->offset(), this->p_, this->data_size()); | |
dbe717ef ILT |
459 | } |
460 | ||
461 | // Output_section_data methods. | |
462 | ||
16649710 ILT |
463 | // Record the output section, and set the entry size and such. |
464 | ||
465 | void | |
466 | Output_section_data::set_output_section(Output_section* os) | |
467 | { | |
468 | gold_assert(this->output_section_ == NULL); | |
469 | this->output_section_ = os; | |
470 | this->do_adjust_output_section(os); | |
471 | } | |
472 | ||
473 | // Return the section index of the output section. | |
474 | ||
dbe717ef ILT |
475 | unsigned int |
476 | Output_section_data::do_out_shndx() const | |
477 | { | |
a3ad94ed | 478 | gold_assert(this->output_section_ != NULL); |
dbe717ef ILT |
479 | return this->output_section_->out_shndx(); |
480 | } | |
481 | ||
a3ad94ed ILT |
482 | // Output_data_strtab methods. |
483 | ||
27bc2bce | 484 | // Set the final data size. |
a3ad94ed ILT |
485 | |
486 | void | |
27bc2bce | 487 | Output_data_strtab::set_final_data_size() |
a3ad94ed ILT |
488 | { |
489 | this->strtab_->set_string_offsets(); | |
490 | this->set_data_size(this->strtab_->get_strtab_size()); | |
491 | } | |
492 | ||
493 | // Write out a string table. | |
494 | ||
495 | void | |
496 | Output_data_strtab::do_write(Output_file* of) | |
497 | { | |
498 | this->strtab_->write(of, this->offset()); | |
499 | } | |
500 | ||
c06b7b0b ILT |
501 | // Output_reloc methods. |
502 | ||
503 | // Get the symbol index of a relocation. | |
504 | ||
505 | template<bool dynamic, int size, bool big_endian> | |
506 | unsigned int | |
507 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index() | |
508 | const | |
509 | { | |
510 | unsigned int index; | |
511 | switch (this->local_sym_index_) | |
512 | { | |
513 | case INVALID_CODE: | |
a3ad94ed | 514 | gold_unreachable(); |
c06b7b0b ILT |
515 | |
516 | case GSYM_CODE: | |
5a6f7e2d | 517 | if (this->u1_.gsym == NULL) |
c06b7b0b ILT |
518 | index = 0; |
519 | else if (dynamic) | |
5a6f7e2d | 520 | index = this->u1_.gsym->dynsym_index(); |
c06b7b0b | 521 | else |
5a6f7e2d | 522 | index = this->u1_.gsym->symtab_index(); |
c06b7b0b ILT |
523 | break; |
524 | ||
525 | case SECTION_CODE: | |
526 | if (dynamic) | |
5a6f7e2d | 527 | index = this->u1_.os->dynsym_index(); |
c06b7b0b | 528 | else |
5a6f7e2d | 529 | index = this->u1_.os->symtab_index(); |
c06b7b0b ILT |
530 | break; |
531 | ||
436ca963 ILT |
532 | case 0: |
533 | // Relocations without symbols use a symbol index of 0. | |
534 | index = 0; | |
535 | break; | |
536 | ||
c06b7b0b ILT |
537 | default: |
538 | if (dynamic) | |
539 | { | |
540 | // FIXME: It seems that some targets may need to generate | |
541 | // dynamic relocations against local symbols for some | |
542 | // reasons. This will have to be addressed at some point. | |
a3ad94ed | 543 | gold_unreachable(); |
c06b7b0b ILT |
544 | } |
545 | else | |
5a6f7e2d | 546 | index = this->u1_.relobj->symtab_index(this->local_sym_index_); |
c06b7b0b ILT |
547 | break; |
548 | } | |
a3ad94ed | 549 | gold_assert(index != -1U); |
c06b7b0b ILT |
550 | return index; |
551 | } | |
552 | ||
553 | // Write out the offset and info fields of a Rel or Rela relocation | |
554 | // entry. | |
555 | ||
556 | template<bool dynamic, int size, bool big_endian> | |
557 | template<typename Write_rel> | |
558 | void | |
559 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel( | |
560 | Write_rel* wr) const | |
561 | { | |
a3ad94ed | 562 | Address address = this->address_; |
5a6f7e2d ILT |
563 | if (this->shndx_ != INVALID_CODE) |
564 | { | |
565 | off_t off; | |
566 | Output_section* os = this->u2_.relobj->output_section(this->shndx_, | |
567 | &off); | |
568 | gold_assert(os != NULL); | |
730cdc88 ILT |
569 | if (off != -1) |
570 | address += os->address() + off; | |
571 | else | |
572 | { | |
573 | address = os->output_address(this->u2_.relobj, this->shndx_, | |
574 | address); | |
575 | gold_assert(address != -1U); | |
576 | } | |
5a6f7e2d ILT |
577 | } |
578 | else if (this->u2_.od != NULL) | |
579 | address += this->u2_.od->address(); | |
a3ad94ed | 580 | wr->put_r_offset(address); |
c06b7b0b ILT |
581 | wr->put_r_info(elfcpp::elf_r_info<size>(this->get_symbol_index(), |
582 | this->type_)); | |
583 | } | |
584 | ||
585 | // Write out a Rel relocation. | |
586 | ||
587 | template<bool dynamic, int size, bool big_endian> | |
588 | void | |
589 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write( | |
590 | unsigned char* pov) const | |
591 | { | |
592 | elfcpp::Rel_write<size, big_endian> orel(pov); | |
593 | this->write_rel(&orel); | |
594 | } | |
595 | ||
596 | // Write out a Rela relocation. | |
597 | ||
598 | template<bool dynamic, int size, bool big_endian> | |
599 | void | |
600 | Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write( | |
601 | unsigned char* pov) const | |
602 | { | |
603 | elfcpp::Rela_write<size, big_endian> orel(pov); | |
604 | this->rel_.write_rel(&orel); | |
605 | orel.put_r_addend(this->addend_); | |
606 | } | |
607 | ||
608 | // Output_data_reloc_base methods. | |
609 | ||
16649710 ILT |
610 | // Adjust the output section. |
611 | ||
612 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
613 | void | |
614 | Output_data_reloc_base<sh_type, dynamic, size, big_endian> | |
615 | ::do_adjust_output_section(Output_section* os) | |
616 | { | |
617 | if (sh_type == elfcpp::SHT_REL) | |
618 | os->set_entsize(elfcpp::Elf_sizes<size>::rel_size); | |
619 | else if (sh_type == elfcpp::SHT_RELA) | |
620 | os->set_entsize(elfcpp::Elf_sizes<size>::rela_size); | |
621 | else | |
622 | gold_unreachable(); | |
623 | if (dynamic) | |
624 | os->set_should_link_to_dynsym(); | |
625 | else | |
626 | os->set_should_link_to_symtab(); | |
627 | } | |
628 | ||
c06b7b0b ILT |
629 | // Write out relocation data. |
630 | ||
631 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
632 | void | |
633 | Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write( | |
634 | Output_file* of) | |
635 | { | |
636 | const off_t off = this->offset(); | |
637 | const off_t oview_size = this->data_size(); | |
638 | unsigned char* const oview = of->get_output_view(off, oview_size); | |
639 | ||
640 | unsigned char* pov = oview; | |
641 | for (typename Relocs::const_iterator p = this->relocs_.begin(); | |
642 | p != this->relocs_.end(); | |
643 | ++p) | |
644 | { | |
645 | p->write(pov); | |
646 | pov += reloc_size; | |
647 | } | |
648 | ||
a3ad94ed | 649 | gold_assert(pov - oview == oview_size); |
c06b7b0b ILT |
650 | |
651 | of->write_output_view(off, oview_size, oview); | |
652 | ||
653 | // We no longer need the relocation entries. | |
654 | this->relocs_.clear(); | |
655 | } | |
656 | ||
dbe717ef | 657 | // Output_data_got::Got_entry methods. |
ead1e424 ILT |
658 | |
659 | // Write out the entry. | |
660 | ||
661 | template<int size, bool big_endian> | |
662 | void | |
7e1edb90 | 663 | Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const |
ead1e424 ILT |
664 | { |
665 | Valtype val = 0; | |
666 | ||
667 | switch (this->local_sym_index_) | |
668 | { | |
669 | case GSYM_CODE: | |
670 | { | |
671 | Symbol* gsym = this->u_.gsym; | |
672 | ||
673 | // If the symbol is resolved locally, we need to write out its | |
674 | // value. Otherwise we just write zero. The target code is | |
675 | // responsible for creating a relocation entry to fill in the | |
d61c6bd4 ILT |
676 | // value at runtime. For non-preemptible symbols in a shared |
677 | // library, the target will need to record whether or not the | |
678 | // value should be written (e.g., it may use a RELATIVE | |
679 | // relocation type). | |
680 | if (gsym->final_value_is_known() || gsym->needs_value_in_got()) | |
ead1e424 ILT |
681 | { |
682 | Sized_symbol<size>* sgsym; | |
683 | // This cast is a bit ugly. We don't want to put a | |
684 | // virtual method in Symbol, because we want Symbol to be | |
685 | // as small as possible. | |
686 | sgsym = static_cast<Sized_symbol<size>*>(gsym); | |
687 | val = sgsym->value(); | |
688 | } | |
689 | } | |
690 | break; | |
691 | ||
692 | case CONSTANT_CODE: | |
693 | val = this->u_.constant; | |
694 | break; | |
695 | ||
696 | default: | |
e727fa71 ILT |
697 | val = this->u_.object->local_symbol_value(this->local_sym_index_); |
698 | break; | |
ead1e424 ILT |
699 | } |
700 | ||
a3ad94ed | 701 | elfcpp::Swap<size, big_endian>::writeval(pov, val); |
ead1e424 ILT |
702 | } |
703 | ||
dbe717ef | 704 | // Output_data_got methods. |
ead1e424 | 705 | |
dbe717ef ILT |
706 | // Add an entry for a global symbol to the GOT. This returns true if |
707 | // this is a new GOT entry, false if the symbol already had a GOT | |
708 | // entry. | |
709 | ||
710 | template<int size, bool big_endian> | |
711 | bool | |
712 | Output_data_got<size, big_endian>::add_global(Symbol* gsym) | |
ead1e424 | 713 | { |
dbe717ef ILT |
714 | if (gsym->has_got_offset()) |
715 | return false; | |
ead1e424 | 716 | |
dbe717ef ILT |
717 | this->entries_.push_back(Got_entry(gsym)); |
718 | this->set_got_size(); | |
719 | gsym->set_got_offset(this->last_got_offset()); | |
720 | return true; | |
721 | } | |
ead1e424 | 722 | |
e727fa71 ILT |
723 | // Add an entry for a local symbol to the GOT. This returns true if |
724 | // this is a new GOT entry, false if the symbol already has a GOT | |
725 | // entry. | |
726 | ||
727 | template<int size, bool big_endian> | |
728 | bool | |
729 | Output_data_got<size, big_endian>::add_local( | |
730 | Sized_relobj<size, big_endian>* object, | |
731 | unsigned int symndx) | |
732 | { | |
733 | if (object->local_has_got_offset(symndx)) | |
734 | return false; | |
07f397ab | 735 | |
e727fa71 ILT |
736 | this->entries_.push_back(Got_entry(object, symndx)); |
737 | this->set_got_size(); | |
738 | object->set_local_got_offset(symndx, this->last_got_offset()); | |
739 | return true; | |
740 | } | |
741 | ||
07f397ab ILT |
742 | // Add an entry (or a pair of entries) for a global TLS symbol to the GOT. |
743 | // In a pair of entries, the first value in the pair will be used for the | |
744 | // module index, and the second value will be used for the dtv-relative | |
745 | // offset. This returns true if this is a new GOT entry, false if the symbol | |
746 | // already has a GOT entry. | |
747 | ||
748 | template<int size, bool big_endian> | |
749 | bool | |
750 | Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym, | |
751 | bool need_pair) | |
752 | { | |
753 | if (gsym->has_tls_got_offset(need_pair)) | |
754 | return false; | |
755 | ||
756 | this->entries_.push_back(Got_entry(gsym)); | |
757 | gsym->set_tls_got_offset(this->last_got_offset(), need_pair); | |
758 | if (need_pair) | |
759 | this->entries_.push_back(Got_entry(gsym)); | |
760 | this->set_got_size(); | |
761 | return true; | |
762 | } | |
763 | ||
764 | // Add an entry (or a pair of entries) for a local TLS symbol to the GOT. | |
765 | // In a pair of entries, the first value in the pair will be used for the | |
766 | // module index, and the second value will be used for the dtv-relative | |
767 | // offset. This returns true if this is a new GOT entry, false if the symbol | |
768 | // already has a GOT entry. | |
769 | ||
770 | template<int size, bool big_endian> | |
771 | bool | |
772 | Output_data_got<size, big_endian>::add_local_tls( | |
773 | Sized_relobj<size, big_endian>* object, | |
774 | unsigned int symndx, | |
775 | bool need_pair) | |
776 | { | |
777 | if (object->local_has_tls_got_offset(symndx, need_pair)) | |
778 | return false; | |
779 | ||
780 | this->entries_.push_back(Got_entry(object, symndx)); | |
781 | object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair); | |
782 | if (need_pair) | |
783 | this->entries_.push_back(Got_entry(object, symndx)); | |
784 | this->set_got_size(); | |
785 | return true; | |
786 | } | |
787 | ||
ead1e424 ILT |
788 | // Write out the GOT. |
789 | ||
790 | template<int size, bool big_endian> | |
791 | void | |
dbe717ef | 792 | Output_data_got<size, big_endian>::do_write(Output_file* of) |
ead1e424 ILT |
793 | { |
794 | const int add = size / 8; | |
795 | ||
796 | const off_t off = this->offset(); | |
c06b7b0b | 797 | const off_t oview_size = this->data_size(); |
ead1e424 ILT |
798 | unsigned char* const oview = of->get_output_view(off, oview_size); |
799 | ||
800 | unsigned char* pov = oview; | |
801 | for (typename Got_entries::const_iterator p = this->entries_.begin(); | |
802 | p != this->entries_.end(); | |
803 | ++p) | |
804 | { | |
7e1edb90 | 805 | p->write(pov); |
ead1e424 ILT |
806 | pov += add; |
807 | } | |
808 | ||
a3ad94ed | 809 | gold_assert(pov - oview == oview_size); |
c06b7b0b | 810 | |
ead1e424 ILT |
811 | of->write_output_view(off, oview_size, oview); |
812 | ||
813 | // We no longer need the GOT entries. | |
814 | this->entries_.clear(); | |
815 | } | |
816 | ||
a3ad94ed ILT |
817 | // Output_data_dynamic::Dynamic_entry methods. |
818 | ||
819 | // Write out the entry. | |
820 | ||
821 | template<int size, bool big_endian> | |
822 | void | |
823 | Output_data_dynamic::Dynamic_entry::write( | |
824 | unsigned char* pov, | |
1ddbd1e6 ILT |
825 | const Stringpool* pool |
826 | ACCEPT_SIZE_ENDIAN) const | |
a3ad94ed ILT |
827 | { |
828 | typename elfcpp::Elf_types<size>::Elf_WXword val; | |
829 | switch (this->classification_) | |
830 | { | |
831 | case DYNAMIC_NUMBER: | |
832 | val = this->u_.val; | |
833 | break; | |
834 | ||
835 | case DYNAMIC_SECTION_ADDRESS: | |
16649710 | 836 | val = this->u_.od->address(); |
a3ad94ed ILT |
837 | break; |
838 | ||
839 | case DYNAMIC_SECTION_SIZE: | |
16649710 | 840 | val = this->u_.od->data_size(); |
a3ad94ed ILT |
841 | break; |
842 | ||
843 | case DYNAMIC_SYMBOL: | |
844 | { | |
16649710 ILT |
845 | const Sized_symbol<size>* s = |
846 | static_cast<const Sized_symbol<size>*>(this->u_.sym); | |
a3ad94ed ILT |
847 | val = s->value(); |
848 | } | |
849 | break; | |
850 | ||
851 | case DYNAMIC_STRING: | |
852 | val = pool->get_offset(this->u_.str); | |
853 | break; | |
854 | ||
855 | default: | |
856 | gold_unreachable(); | |
857 | } | |
858 | ||
859 | elfcpp::Dyn_write<size, big_endian> dw(pov); | |
860 | dw.put_d_tag(this->tag_); | |
861 | dw.put_d_val(val); | |
862 | } | |
863 | ||
864 | // Output_data_dynamic methods. | |
865 | ||
16649710 ILT |
866 | // Adjust the output section to set the entry size. |
867 | ||
868 | void | |
869 | Output_data_dynamic::do_adjust_output_section(Output_section* os) | |
870 | { | |
9025d29d | 871 | if (parameters->get_size() == 32) |
16649710 | 872 | os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size); |
9025d29d | 873 | else if (parameters->get_size() == 64) |
16649710 ILT |
874 | os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size); |
875 | else | |
876 | gold_unreachable(); | |
877 | } | |
878 | ||
a3ad94ed ILT |
879 | // Set the final data size. |
880 | ||
881 | void | |
27bc2bce | 882 | Output_data_dynamic::set_final_data_size() |
a3ad94ed ILT |
883 | { |
884 | // Add the terminating entry. | |
885 | this->add_constant(elfcpp::DT_NULL, 0); | |
886 | ||
887 | int dyn_size; | |
9025d29d | 888 | if (parameters->get_size() == 32) |
a3ad94ed | 889 | dyn_size = elfcpp::Elf_sizes<32>::dyn_size; |
9025d29d | 890 | else if (parameters->get_size() == 64) |
a3ad94ed ILT |
891 | dyn_size = elfcpp::Elf_sizes<64>::dyn_size; |
892 | else | |
893 | gold_unreachable(); | |
894 | this->set_data_size(this->entries_.size() * dyn_size); | |
895 | } | |
896 | ||
897 | // Write out the dynamic entries. | |
898 | ||
899 | void | |
900 | Output_data_dynamic::do_write(Output_file* of) | |
901 | { | |
9025d29d | 902 | if (parameters->get_size() == 32) |
a3ad94ed | 903 | { |
9025d29d ILT |
904 | if (parameters->is_big_endian()) |
905 | { | |
906 | #ifdef HAVE_TARGET_32_BIG | |
907 | this->sized_write<32, true>(of); | |
908 | #else | |
909 | gold_unreachable(); | |
910 | #endif | |
911 | } | |
a3ad94ed | 912 | else |
9025d29d ILT |
913 | { |
914 | #ifdef HAVE_TARGET_32_LITTLE | |
915 | this->sized_write<32, false>(of); | |
916 | #else | |
917 | gold_unreachable(); | |
918 | #endif | |
919 | } | |
a3ad94ed | 920 | } |
9025d29d | 921 | else if (parameters->get_size() == 64) |
a3ad94ed | 922 | { |
9025d29d ILT |
923 | if (parameters->is_big_endian()) |
924 | { | |
925 | #ifdef HAVE_TARGET_64_BIG | |
926 | this->sized_write<64, true>(of); | |
927 | #else | |
928 | gold_unreachable(); | |
929 | #endif | |
930 | } | |
a3ad94ed | 931 | else |
9025d29d ILT |
932 | { |
933 | #ifdef HAVE_TARGET_64_LITTLE | |
934 | this->sized_write<64, false>(of); | |
935 | #else | |
936 | gold_unreachable(); | |
937 | #endif | |
938 | } | |
a3ad94ed ILT |
939 | } |
940 | else | |
941 | gold_unreachable(); | |
942 | } | |
943 | ||
944 | template<int size, bool big_endian> | |
945 | void | |
946 | Output_data_dynamic::sized_write(Output_file* of) | |
947 | { | |
948 | const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size; | |
949 | ||
950 | const off_t offset = this->offset(); | |
951 | const off_t oview_size = this->data_size(); | |
952 | unsigned char* const oview = of->get_output_view(offset, oview_size); | |
953 | ||
954 | unsigned char* pov = oview; | |
955 | for (typename Dynamic_entries::const_iterator p = this->entries_.begin(); | |
956 | p != this->entries_.end(); | |
957 | ++p) | |
958 | { | |
1ddbd1e6 ILT |
959 | p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)( |
960 | pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian)); | |
a3ad94ed ILT |
961 | pov += dyn_size; |
962 | } | |
963 | ||
964 | gold_assert(pov - oview == oview_size); | |
965 | ||
966 | of->write_output_view(offset, oview_size, oview); | |
967 | ||
968 | // We no longer need the dynamic entries. | |
969 | this->entries_.clear(); | |
970 | } | |
971 | ||
ead1e424 ILT |
972 | // Output_section::Input_section methods. |
973 | ||
974 | // Return the data size. For an input section we store the size here. | |
975 | // For an Output_section_data, we have to ask it for the size. | |
976 | ||
977 | off_t | |
978 | Output_section::Input_section::data_size() const | |
979 | { | |
980 | if (this->is_input_section()) | |
b8e6aad9 | 981 | return this->u1_.data_size; |
ead1e424 | 982 | else |
b8e6aad9 | 983 | return this->u2_.posd->data_size(); |
ead1e424 ILT |
984 | } |
985 | ||
986 | // Set the address and file offset. | |
987 | ||
988 | void | |
989 | Output_section::Input_section::set_address(uint64_t addr, off_t off, | |
990 | off_t secoff) | |
991 | { | |
992 | if (this->is_input_section()) | |
b8e6aad9 | 993 | this->u2_.object->set_section_offset(this->shndx_, off - secoff); |
ead1e424 | 994 | else |
27bc2bce | 995 | this->u2_.posd->set_address_and_file_offset(addr, off); |
b8e6aad9 ILT |
996 | } |
997 | ||
730cdc88 | 998 | // Try to turn an input offset into an output offset. |
b8e6aad9 ILT |
999 | |
1000 | bool | |
730cdc88 ILT |
1001 | Output_section::Input_section::output_offset(const Relobj* object, |
1002 | unsigned int shndx, | |
1003 | off_t offset, | |
1004 | off_t *poutput) const | |
b8e6aad9 ILT |
1005 | { |
1006 | if (!this->is_input_section()) | |
730cdc88 | 1007 | return this->u2_.posd->output_offset(object, shndx, offset, poutput); |
b8e6aad9 ILT |
1008 | else |
1009 | { | |
730cdc88 | 1010 | if (this->shndx_ != shndx || this->u2_.object != object) |
b8e6aad9 ILT |
1011 | return false; |
1012 | off_t output_offset; | |
1013 | Output_section* os = object->output_section(shndx, &output_offset); | |
1014 | gold_assert(os != NULL); | |
730cdc88 ILT |
1015 | gold_assert(output_offset != -1); |
1016 | *poutput = output_offset + offset; | |
b8e6aad9 ILT |
1017 | return true; |
1018 | } | |
ead1e424 ILT |
1019 | } |
1020 | ||
1021 | // Write out the data. We don't have to do anything for an input | |
1022 | // section--they are handled via Object::relocate--but this is where | |
1023 | // we write out the data for an Output_section_data. | |
1024 | ||
1025 | void | |
1026 | Output_section::Input_section::write(Output_file* of) | |
1027 | { | |
1028 | if (!this->is_input_section()) | |
b8e6aad9 | 1029 | this->u2_.posd->write(of); |
ead1e424 ILT |
1030 | } |
1031 | ||
a2fb1b05 ILT |
1032 | // Output_section methods. |
1033 | ||
1034 | // Construct an Output_section. NAME will point into a Stringpool. | |
1035 | ||
1036 | Output_section::Output_section(const char* name, elfcpp::Elf_Word type, | |
b8e6aad9 | 1037 | elfcpp::Elf_Xword flags) |
a2fb1b05 | 1038 | : name_(name), |
a2fb1b05 ILT |
1039 | addralign_(0), |
1040 | entsize_(0), | |
16649710 | 1041 | link_section_(NULL), |
a2fb1b05 | 1042 | link_(0), |
16649710 | 1043 | info_section_(NULL), |
a2fb1b05 ILT |
1044 | info_(0), |
1045 | type_(type), | |
61ba1cf9 | 1046 | flags_(flags), |
91ea499d | 1047 | out_shndx_(-1U), |
c06b7b0b ILT |
1048 | symtab_index_(0), |
1049 | dynsym_index_(0), | |
ead1e424 ILT |
1050 | input_sections_(), |
1051 | first_input_offset_(0), | |
c51e6221 | 1052 | fills_(), |
a3ad94ed | 1053 | needs_symtab_index_(false), |
16649710 ILT |
1054 | needs_dynsym_index_(false), |
1055 | should_link_to_symtab_(false), | |
730cdc88 | 1056 | should_link_to_dynsym_(false), |
27bc2bce ILT |
1057 | after_input_sections_(false), |
1058 | requires_postprocessing_(false) | |
a2fb1b05 | 1059 | { |
27bc2bce ILT |
1060 | // An unallocated section has no address. Forcing this means that |
1061 | // we don't need special treatment for symbols defined in debug | |
1062 | // sections. | |
1063 | if ((flags & elfcpp::SHF_ALLOC) == 0) | |
1064 | this->set_address(0); | |
a2fb1b05 ILT |
1065 | } |
1066 | ||
54dc6425 ILT |
1067 | Output_section::~Output_section() |
1068 | { | |
1069 | } | |
1070 | ||
16649710 ILT |
1071 | // Set the entry size. |
1072 | ||
1073 | void | |
1074 | Output_section::set_entsize(uint64_t v) | |
1075 | { | |
1076 | if (this->entsize_ == 0) | |
1077 | this->entsize_ = v; | |
1078 | else | |
1079 | gold_assert(this->entsize_ == v); | |
1080 | } | |
1081 | ||
ead1e424 | 1082 | // Add the input section SHNDX, with header SHDR, named SECNAME, in |
730cdc88 ILT |
1083 | // OBJECT, to the Output_section. RELOC_SHNDX is the index of a |
1084 | // relocation section which applies to this section, or 0 if none, or | |
1085 | // -1U if more than one. Return the offset of the input section | |
1086 | // within the output section. Return -1 if the input section will | |
1087 | // receive special handling. In the normal case we don't always keep | |
1088 | // track of input sections for an Output_section. Instead, each | |
1089 | // Object keeps track of the Output_section for each of its input | |
1090 | // sections. | |
a2fb1b05 ILT |
1091 | |
1092 | template<int size, bool big_endian> | |
1093 | off_t | |
730cdc88 ILT |
1094 | Output_section::add_input_section(Sized_relobj<size, big_endian>* object, |
1095 | unsigned int shndx, | |
ead1e424 | 1096 | const char* secname, |
730cdc88 ILT |
1097 | const elfcpp::Shdr<size, big_endian>& shdr, |
1098 | unsigned int reloc_shndx) | |
a2fb1b05 ILT |
1099 | { |
1100 | elfcpp::Elf_Xword addralign = shdr.get_sh_addralign(); | |
1101 | if ((addralign & (addralign - 1)) != 0) | |
1102 | { | |
75f2446e ILT |
1103 | object->error(_("invalid alignment %lu for section \"%s\""), |
1104 | static_cast<unsigned long>(addralign), secname); | |
1105 | addralign = 1; | |
a2fb1b05 | 1106 | } |
a2fb1b05 ILT |
1107 | |
1108 | if (addralign > this->addralign_) | |
1109 | this->addralign_ = addralign; | |
1110 | ||
44a43cf9 | 1111 | typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags(); |
4f833eee | 1112 | uint64_t entsize = shdr.get_sh_entsize(); |
44a43cf9 ILT |
1113 | |
1114 | // .debug_str is a mergeable string section, but is not always so | |
1115 | // marked by compilers. Mark manually here so we can optimize. | |
1116 | if (strcmp(secname, ".debug_str") == 0) | |
4f833eee ILT |
1117 | { |
1118 | sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS); | |
1119 | entsize = 1; | |
1120 | } | |
44a43cf9 | 1121 | |
b8e6aad9 | 1122 | // If this is a SHF_MERGE section, we pass all the input sections to |
730cdc88 ILT |
1123 | // a Output_data_merge. We don't try to handle relocations for such |
1124 | // a section. | |
44a43cf9 | 1125 | if ((sh_flags & elfcpp::SHF_MERGE) != 0 |
730cdc88 | 1126 | && reloc_shndx == 0) |
b8e6aad9 | 1127 | { |
44a43cf9 | 1128 | if (this->add_merge_input_section(object, shndx, sh_flags, |
4f833eee | 1129 | entsize, addralign)) |
b8e6aad9 ILT |
1130 | { |
1131 | // Tell the relocation routines that they need to call the | |
730cdc88 | 1132 | // output_offset method to determine the final address. |
b8e6aad9 ILT |
1133 | return -1; |
1134 | } | |
1135 | } | |
1136 | ||
27bc2bce | 1137 | off_t offset_in_section = this->current_data_size_for_child(); |
c51e6221 ILT |
1138 | off_t aligned_offset_in_section = align_address(offset_in_section, |
1139 | addralign); | |
1140 | ||
1141 | if (aligned_offset_in_section > offset_in_section | |
44a43cf9 | 1142 | && (sh_flags & elfcpp::SHF_EXECINSTR) != 0 |
c51e6221 ILT |
1143 | && object->target()->has_code_fill()) |
1144 | { | |
1145 | // We need to add some fill data. Using fill_list_ when | |
1146 | // possible is an optimization, since we will often have fill | |
1147 | // sections without input sections. | |
1148 | off_t fill_len = aligned_offset_in_section - offset_in_section; | |
1149 | if (this->input_sections_.empty()) | |
1150 | this->fills_.push_back(Fill(offset_in_section, fill_len)); | |
1151 | else | |
1152 | { | |
1153 | // FIXME: When relaxing, the size needs to adjust to | |
1154 | // maintain a constant alignment. | |
1155 | std::string fill_data(object->target()->code_fill(fill_len)); | |
1156 | Output_data_const* odc = new Output_data_const(fill_data, 1); | |
1157 | this->input_sections_.push_back(Input_section(odc)); | |
1158 | } | |
1159 | } | |
1160 | ||
27bc2bce ILT |
1161 | this->set_current_data_size_for_child(aligned_offset_in_section |
1162 | + shdr.get_sh_size()); | |
a2fb1b05 | 1163 | |
ead1e424 ILT |
1164 | // We need to keep track of this section if we are already keeping |
1165 | // track of sections, or if we are relaxing. FIXME: Add test for | |
1166 | // relaxing. | |
c51e6221 | 1167 | if (!this->input_sections_.empty()) |
ead1e424 ILT |
1168 | this->input_sections_.push_back(Input_section(object, shndx, |
1169 | shdr.get_sh_size(), | |
1170 | addralign)); | |
54dc6425 | 1171 | |
c51e6221 | 1172 | return aligned_offset_in_section; |
61ba1cf9 ILT |
1173 | } |
1174 | ||
ead1e424 ILT |
1175 | // Add arbitrary data to an output section. |
1176 | ||
1177 | void | |
1178 | Output_section::add_output_section_data(Output_section_data* posd) | |
1179 | { | |
b8e6aad9 ILT |
1180 | Input_section inp(posd); |
1181 | this->add_output_section_data(&inp); | |
1182 | } | |
1183 | ||
1184 | // Add arbitrary data to an output section by Input_section. | |
c06b7b0b | 1185 | |
b8e6aad9 ILT |
1186 | void |
1187 | Output_section::add_output_section_data(Input_section* inp) | |
1188 | { | |
ead1e424 | 1189 | if (this->input_sections_.empty()) |
27bc2bce | 1190 | this->first_input_offset_ = this->current_data_size_for_child(); |
c06b7b0b | 1191 | |
b8e6aad9 | 1192 | this->input_sections_.push_back(*inp); |
c06b7b0b | 1193 | |
b8e6aad9 | 1194 | uint64_t addralign = inp->addralign(); |
ead1e424 ILT |
1195 | if (addralign > this->addralign_) |
1196 | this->addralign_ = addralign; | |
c06b7b0b | 1197 | |
b8e6aad9 ILT |
1198 | inp->set_output_section(this); |
1199 | } | |
1200 | ||
1201 | // Add a merge section to an output section. | |
1202 | ||
1203 | void | |
1204 | Output_section::add_output_merge_section(Output_section_data* posd, | |
1205 | bool is_string, uint64_t entsize) | |
1206 | { | |
1207 | Input_section inp(posd, is_string, entsize); | |
1208 | this->add_output_section_data(&inp); | |
1209 | } | |
1210 | ||
1211 | // Add an input section to a SHF_MERGE section. | |
1212 | ||
1213 | bool | |
1214 | Output_section::add_merge_input_section(Relobj* object, unsigned int shndx, | |
1215 | uint64_t flags, uint64_t entsize, | |
1216 | uint64_t addralign) | |
1217 | { | |
87f95776 ILT |
1218 | bool is_string = (flags & elfcpp::SHF_STRINGS) != 0; |
1219 | ||
1220 | // We only merge strings if the alignment is not more than the | |
1221 | // character size. This could be handled, but it's unusual. | |
1222 | if (is_string && addralign > entsize) | |
b8e6aad9 ILT |
1223 | return false; |
1224 | ||
b8e6aad9 ILT |
1225 | Input_section_list::iterator p; |
1226 | for (p = this->input_sections_.begin(); | |
1227 | p != this->input_sections_.end(); | |
1228 | ++p) | |
87f95776 | 1229 | if (p->is_merge_section(is_string, entsize, addralign)) |
b8e6aad9 ILT |
1230 | break; |
1231 | ||
1232 | // We handle the actual constant merging in Output_merge_data or | |
1233 | // Output_merge_string_data. | |
1234 | if (p != this->input_sections_.end()) | |
1235 | p->add_input_section(object, shndx); | |
1236 | else | |
1237 | { | |
1238 | Output_section_data* posd; | |
1239 | if (!is_string) | |
87f95776 | 1240 | posd = new Output_merge_data(entsize, addralign); |
b8e6aad9 | 1241 | else if (entsize == 1) |
87f95776 | 1242 | posd = new Output_merge_string<char>(addralign); |
b8e6aad9 | 1243 | else if (entsize == 2) |
87f95776 | 1244 | posd = new Output_merge_string<uint16_t>(addralign); |
b8e6aad9 | 1245 | else if (entsize == 4) |
87f95776 | 1246 | posd = new Output_merge_string<uint32_t>(addralign); |
b8e6aad9 ILT |
1247 | else |
1248 | return false; | |
1249 | ||
1250 | this->add_output_merge_section(posd, is_string, entsize); | |
1251 | posd->add_input_section(object, shndx); | |
1252 | } | |
1253 | ||
1254 | return true; | |
1255 | } | |
1256 | ||
730cdc88 ILT |
1257 | // Given an address OFFSET relative to the start of input section |
1258 | // SHNDX in OBJECT, return whether this address is being included in | |
1259 | // the final link. This should only be called if SHNDX in OBJECT has | |
1260 | // a special mapping. | |
1261 | ||
1262 | bool | |
1263 | Output_section::is_input_address_mapped(const Relobj* object, | |
1264 | unsigned int shndx, | |
1265 | off_t offset) const | |
1266 | { | |
1267 | gold_assert(object->is_section_specially_mapped(shndx)); | |
1268 | ||
1269 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); | |
1270 | p != this->input_sections_.end(); | |
1271 | ++p) | |
1272 | { | |
1273 | off_t output_offset; | |
1274 | if (p->output_offset(object, shndx, offset, &output_offset)) | |
1275 | return output_offset != -1; | |
1276 | } | |
1277 | ||
1278 | // By default we assume that the address is mapped. This should | |
1279 | // only be called after we have passed all sections to Layout. At | |
1280 | // that point we should know what we are discarding. | |
1281 | return true; | |
1282 | } | |
1283 | ||
1284 | // Given an address OFFSET relative to the start of input section | |
1285 | // SHNDX in object OBJECT, return the output offset relative to the | |
1286 | // start of the section. This should only be called if SHNDX in | |
1287 | // OBJECT has a special mapping. | |
1288 | ||
1289 | off_t | |
1290 | Output_section::output_offset(const Relobj* object, unsigned int shndx, | |
1291 | off_t offset) const | |
1292 | { | |
1293 | gold_assert(object->is_section_specially_mapped(shndx)); | |
1294 | // This can only be called meaningfully when layout is complete. | |
1295 | gold_assert(Output_data::is_layout_complete()); | |
1296 | ||
1297 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); | |
1298 | p != this->input_sections_.end(); | |
1299 | ++p) | |
1300 | { | |
1301 | off_t output_offset; | |
1302 | if (p->output_offset(object, shndx, offset, &output_offset)) | |
1303 | return output_offset; | |
1304 | } | |
1305 | gold_unreachable(); | |
1306 | } | |
1307 | ||
b8e6aad9 ILT |
1308 | // Return the output virtual address of OFFSET relative to the start |
1309 | // of input section SHNDX in object OBJECT. | |
1310 | ||
1311 | uint64_t | |
1312 | Output_section::output_address(const Relobj* object, unsigned int shndx, | |
1313 | off_t offset) const | |
1314 | { | |
730cdc88 ILT |
1315 | gold_assert(object->is_section_specially_mapped(shndx)); |
1316 | // This can only be called meaningfully when layout is complete. | |
1317 | gold_assert(Output_data::is_layout_complete()); | |
1318 | ||
b8e6aad9 ILT |
1319 | uint64_t addr = this->address() + this->first_input_offset_; |
1320 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); | |
1321 | p != this->input_sections_.end(); | |
1322 | ++p) | |
1323 | { | |
1324 | addr = align_address(addr, p->addralign()); | |
730cdc88 ILT |
1325 | off_t output_offset; |
1326 | if (p->output_offset(object, shndx, offset, &output_offset)) | |
1327 | { | |
1328 | if (output_offset == -1) | |
1329 | return -1U; | |
1330 | return addr + output_offset; | |
1331 | } | |
b8e6aad9 ILT |
1332 | addr += p->data_size(); |
1333 | } | |
1334 | ||
1335 | // If we get here, it means that we don't know the mapping for this | |
1336 | // input section. This might happen in principle if | |
1337 | // add_input_section were called before add_output_section_data. | |
1338 | // But it should never actually happen. | |
1339 | ||
1340 | gold_unreachable(); | |
ead1e424 ILT |
1341 | } |
1342 | ||
27bc2bce | 1343 | // Set the data size of an Output_section. This is where we handle |
ead1e424 ILT |
1344 | // setting the addresses of any Output_section_data objects. |
1345 | ||
1346 | void | |
27bc2bce | 1347 | Output_section::set_final_data_size() |
ead1e424 ILT |
1348 | { |
1349 | if (this->input_sections_.empty()) | |
27bc2bce ILT |
1350 | { |
1351 | this->set_data_size(this->current_data_size_for_child()); | |
1352 | return; | |
1353 | } | |
ead1e424 | 1354 | |
27bc2bce ILT |
1355 | uint64_t address = this->address(); |
1356 | off_t startoff = this->offset(); | |
ead1e424 ILT |
1357 | off_t off = startoff + this->first_input_offset_; |
1358 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
1359 | p != this->input_sections_.end(); | |
1360 | ++p) | |
1361 | { | |
1362 | off = align_address(off, p->addralign()); | |
1363 | p->set_address(address + (off - startoff), off, startoff); | |
1364 | off += p->data_size(); | |
1365 | } | |
1366 | ||
1367 | this->set_data_size(off - startoff); | |
1368 | } | |
1369 | ||
61ba1cf9 ILT |
1370 | // Write the section header to *OSHDR. |
1371 | ||
1372 | template<int size, bool big_endian> | |
1373 | void | |
16649710 ILT |
1374 | Output_section::write_header(const Layout* layout, |
1375 | const Stringpool* secnamepool, | |
61ba1cf9 ILT |
1376 | elfcpp::Shdr_write<size, big_endian>* oshdr) const |
1377 | { | |
1378 | oshdr->put_sh_name(secnamepool->get_offset(this->name_)); | |
1379 | oshdr->put_sh_type(this->type_); | |
1380 | oshdr->put_sh_flags(this->flags_); | |
1381 | oshdr->put_sh_addr(this->address()); | |
1382 | oshdr->put_sh_offset(this->offset()); | |
1383 | oshdr->put_sh_size(this->data_size()); | |
16649710 ILT |
1384 | if (this->link_section_ != NULL) |
1385 | oshdr->put_sh_link(this->link_section_->out_shndx()); | |
1386 | else if (this->should_link_to_symtab_) | |
1387 | oshdr->put_sh_link(layout->symtab_section()->out_shndx()); | |
1388 | else if (this->should_link_to_dynsym_) | |
1389 | oshdr->put_sh_link(layout->dynsym_section()->out_shndx()); | |
1390 | else | |
1391 | oshdr->put_sh_link(this->link_); | |
1392 | if (this->info_section_ != NULL) | |
1393 | oshdr->put_sh_info(this->info_section_->out_shndx()); | |
1394 | else | |
1395 | oshdr->put_sh_info(this->info_); | |
61ba1cf9 ILT |
1396 | oshdr->put_sh_addralign(this->addralign_); |
1397 | oshdr->put_sh_entsize(this->entsize_); | |
a2fb1b05 ILT |
1398 | } |
1399 | ||
ead1e424 ILT |
1400 | // Write out the data. For input sections the data is written out by |
1401 | // Object::relocate, but we have to handle Output_section_data objects | |
1402 | // here. | |
1403 | ||
1404 | void | |
1405 | Output_section::do_write(Output_file* of) | |
1406 | { | |
c51e6221 ILT |
1407 | off_t output_section_file_offset = this->offset(); |
1408 | for (Fill_list::iterator p = this->fills_.begin(); | |
1409 | p != this->fills_.end(); | |
1410 | ++p) | |
1411 | { | |
1412 | std::string fill_data(of->target()->code_fill(p->length())); | |
1413 | of->write(output_section_file_offset + p->section_offset(), | |
1414 | fill_data.data(), fill_data.size()); | |
1415 | } | |
1416 | ||
ead1e424 ILT |
1417 | for (Input_section_list::iterator p = this->input_sections_.begin(); |
1418 | p != this->input_sections_.end(); | |
1419 | ++p) | |
1420 | p->write(of); | |
1421 | } | |
1422 | ||
a2fb1b05 ILT |
1423 | // Output segment methods. |
1424 | ||
1425 | Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags) | |
54dc6425 | 1426 | : output_data_(), |
75f65a3e | 1427 | output_bss_(), |
a2fb1b05 ILT |
1428 | vaddr_(0), |
1429 | paddr_(0), | |
1430 | memsz_(0), | |
1431 | align_(0), | |
1432 | offset_(0), | |
1433 | filesz_(0), | |
1434 | type_(type), | |
ead1e424 ILT |
1435 | flags_(flags), |
1436 | is_align_known_(false) | |
a2fb1b05 ILT |
1437 | { |
1438 | } | |
1439 | ||
1440 | // Add an Output_section to an Output_segment. | |
1441 | ||
1442 | void | |
75f65a3e | 1443 | Output_segment::add_output_section(Output_section* os, |
dbe717ef ILT |
1444 | elfcpp::Elf_Word seg_flags, |
1445 | bool front) | |
a2fb1b05 | 1446 | { |
a3ad94ed ILT |
1447 | gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0); |
1448 | gold_assert(!this->is_align_known_); | |
75f65a3e | 1449 | |
ead1e424 | 1450 | // Update the segment flags. |
75f65a3e | 1451 | this->flags_ |= seg_flags; |
75f65a3e ILT |
1452 | |
1453 | Output_segment::Output_data_list* pdl; | |
1454 | if (os->type() == elfcpp::SHT_NOBITS) | |
1455 | pdl = &this->output_bss_; | |
1456 | else | |
1457 | pdl = &this->output_data_; | |
54dc6425 | 1458 | |
a2fb1b05 ILT |
1459 | // So that PT_NOTE segments will work correctly, we need to ensure |
1460 | // that all SHT_NOTE sections are adjacent. This will normally | |
1461 | // happen automatically, because all the SHT_NOTE input sections | |
1462 | // will wind up in the same output section. However, it is possible | |
1463 | // for multiple SHT_NOTE input sections to have different section | |
1464 | // flags, and thus be in different output sections, but for the | |
1465 | // different section flags to map into the same segment flags and | |
1466 | // thus the same output segment. | |
54dc6425 ILT |
1467 | |
1468 | // Note that while there may be many input sections in an output | |
1469 | // section, there are normally only a few output sections in an | |
1470 | // output segment. This loop is expected to be fast. | |
1471 | ||
61ba1cf9 | 1472 | if (os->type() == elfcpp::SHT_NOTE && !pdl->empty()) |
a2fb1b05 | 1473 | { |
a3ad94ed | 1474 | Output_segment::Output_data_list::iterator p = pdl->end(); |
75f65a3e | 1475 | do |
54dc6425 | 1476 | { |
75f65a3e | 1477 | --p; |
54dc6425 ILT |
1478 | if ((*p)->is_section_type(elfcpp::SHT_NOTE)) |
1479 | { | |
dbe717ef | 1480 | // We don't worry about the FRONT parameter. |
54dc6425 | 1481 | ++p; |
75f65a3e | 1482 | pdl->insert(p, os); |
54dc6425 ILT |
1483 | return; |
1484 | } | |
1485 | } | |
75f65a3e | 1486 | while (p != pdl->begin()); |
54dc6425 ILT |
1487 | } |
1488 | ||
1489 | // Similarly, so that PT_TLS segments will work, we need to group | |
75f65a3e ILT |
1490 | // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special |
1491 | // case: we group the SHF_TLS/SHT_NOBITS sections right after the | |
1492 | // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS | |
07f397ab ILT |
1493 | // correctly. SHF_TLS sections get added to both a PT_LOAD segment |
1494 | // and the PT_TLS segment -- we do this grouping only for the | |
1495 | // PT_LOAD segment. | |
1496 | if (this->type_ != elfcpp::PT_TLS | |
1497 | && (os->flags() & elfcpp::SHF_TLS) != 0 | |
1498 | && !this->output_data_.empty()) | |
54dc6425 | 1499 | { |
75f65a3e ILT |
1500 | pdl = &this->output_data_; |
1501 | bool nobits = os->type() == elfcpp::SHT_NOBITS; | |
ead1e424 | 1502 | bool sawtls = false; |
a3ad94ed | 1503 | Output_segment::Output_data_list::iterator p = pdl->end(); |
75f65a3e | 1504 | do |
a2fb1b05 | 1505 | { |
75f65a3e | 1506 | --p; |
ead1e424 ILT |
1507 | bool insert; |
1508 | if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)) | |
1509 | { | |
1510 | sawtls = true; | |
1511 | // Put a NOBITS section after the first TLS section. | |
1512 | // But a PROGBITS section after the first TLS/PROGBITS | |
1513 | // section. | |
1514 | insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS); | |
1515 | } | |
1516 | else | |
1517 | { | |
1518 | // If we've gone past the TLS sections, but we've seen a | |
1519 | // TLS section, then we need to insert this section now. | |
1520 | insert = sawtls; | |
1521 | } | |
1522 | ||
1523 | if (insert) | |
a2fb1b05 | 1524 | { |
dbe717ef | 1525 | // We don't worry about the FRONT parameter. |
a2fb1b05 | 1526 | ++p; |
75f65a3e | 1527 | pdl->insert(p, os); |
a2fb1b05 ILT |
1528 | return; |
1529 | } | |
1530 | } | |
75f65a3e | 1531 | while (p != pdl->begin()); |
ead1e424 | 1532 | |
dbe717ef ILT |
1533 | // There are no TLS sections yet; put this one at the requested |
1534 | // location in the section list. | |
a2fb1b05 ILT |
1535 | } |
1536 | ||
dbe717ef ILT |
1537 | if (front) |
1538 | pdl->push_front(os); | |
1539 | else | |
1540 | pdl->push_back(os); | |
75f65a3e ILT |
1541 | } |
1542 | ||
1543 | // Add an Output_data (which is not an Output_section) to the start of | |
1544 | // a segment. | |
1545 | ||
1546 | void | |
1547 | Output_segment::add_initial_output_data(Output_data* od) | |
1548 | { | |
a3ad94ed | 1549 | gold_assert(!this->is_align_known_); |
75f65a3e ILT |
1550 | this->output_data_.push_front(od); |
1551 | } | |
1552 | ||
1553 | // Return the maximum alignment of the Output_data in Output_segment. | |
ead1e424 | 1554 | // Once we compute this, we prohibit new sections from being added. |
75f65a3e ILT |
1555 | |
1556 | uint64_t | |
ead1e424 | 1557 | Output_segment::addralign() |
75f65a3e | 1558 | { |
ead1e424 ILT |
1559 | if (!this->is_align_known_) |
1560 | { | |
1561 | uint64_t addralign; | |
1562 | ||
1563 | addralign = Output_segment::maximum_alignment(&this->output_data_); | |
1564 | if (addralign > this->align_) | |
1565 | this->align_ = addralign; | |
1566 | ||
1567 | addralign = Output_segment::maximum_alignment(&this->output_bss_); | |
1568 | if (addralign > this->align_) | |
1569 | this->align_ = addralign; | |
1570 | ||
1571 | this->is_align_known_ = true; | |
1572 | } | |
1573 | ||
75f65a3e ILT |
1574 | return this->align_; |
1575 | } | |
1576 | ||
ead1e424 ILT |
1577 | // Return the maximum alignment of a list of Output_data. |
1578 | ||
1579 | uint64_t | |
1580 | Output_segment::maximum_alignment(const Output_data_list* pdl) | |
1581 | { | |
1582 | uint64_t ret = 0; | |
1583 | for (Output_data_list::const_iterator p = pdl->begin(); | |
1584 | p != pdl->end(); | |
1585 | ++p) | |
1586 | { | |
1587 | uint64_t addralign = (*p)->addralign(); | |
1588 | if (addralign > ret) | |
1589 | ret = addralign; | |
1590 | } | |
1591 | return ret; | |
1592 | } | |
1593 | ||
4f4c5f80 ILT |
1594 | // Return the number of dynamic relocs applied to this segment. |
1595 | ||
1596 | unsigned int | |
1597 | Output_segment::dynamic_reloc_count() const | |
1598 | { | |
1599 | return (this->dynamic_reloc_count_list(&this->output_data_) | |
1600 | + this->dynamic_reloc_count_list(&this->output_bss_)); | |
1601 | } | |
1602 | ||
1603 | // Return the number of dynamic relocs applied to an Output_data_list. | |
1604 | ||
1605 | unsigned int | |
1606 | Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const | |
1607 | { | |
1608 | unsigned int count = 0; | |
1609 | for (Output_data_list::const_iterator p = pdl->begin(); | |
1610 | p != pdl->end(); | |
1611 | ++p) | |
1612 | count += (*p)->dynamic_reloc_count(); | |
1613 | return count; | |
1614 | } | |
1615 | ||
75f65a3e | 1616 | // Set the section addresses for an Output_segment. ADDR is the |
ead1e424 ILT |
1617 | // address and *POFF is the file offset. Set the section indexes |
1618 | // starting with *PSHNDX. Return the address of the immediately | |
1619 | // following segment. Update *POFF and *PSHNDX. | |
75f65a3e ILT |
1620 | |
1621 | uint64_t | |
ead1e424 ILT |
1622 | Output_segment::set_section_addresses(uint64_t addr, off_t* poff, |
1623 | unsigned int* pshndx) | |
75f65a3e | 1624 | { |
a3ad94ed | 1625 | gold_assert(this->type_ == elfcpp::PT_LOAD); |
75f65a3e ILT |
1626 | |
1627 | this->vaddr_ = addr; | |
1628 | this->paddr_ = addr; | |
1629 | ||
1630 | off_t orig_off = *poff; | |
1631 | this->offset_ = orig_off; | |
1632 | ||
ead1e424 ILT |
1633 | *poff = align_address(*poff, this->addralign()); |
1634 | ||
1635 | addr = this->set_section_list_addresses(&this->output_data_, addr, poff, | |
1636 | pshndx); | |
75f65a3e ILT |
1637 | this->filesz_ = *poff - orig_off; |
1638 | ||
1639 | off_t off = *poff; | |
1640 | ||
61ba1cf9 | 1641 | uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr, |
ead1e424 | 1642 | poff, pshndx); |
75f65a3e ILT |
1643 | this->memsz_ = *poff - orig_off; |
1644 | ||
1645 | // Ignore the file offset adjustments made by the BSS Output_data | |
1646 | // objects. | |
1647 | *poff = off; | |
61ba1cf9 ILT |
1648 | |
1649 | return ret; | |
75f65a3e ILT |
1650 | } |
1651 | ||
b8e6aad9 ILT |
1652 | // Set the addresses and file offsets in a list of Output_data |
1653 | // structures. | |
75f65a3e ILT |
1654 | |
1655 | uint64_t | |
1656 | Output_segment::set_section_list_addresses(Output_data_list* pdl, | |
ead1e424 ILT |
1657 | uint64_t addr, off_t* poff, |
1658 | unsigned int* pshndx) | |
75f65a3e | 1659 | { |
ead1e424 | 1660 | off_t startoff = *poff; |
75f65a3e | 1661 | |
ead1e424 | 1662 | off_t off = startoff; |
75f65a3e ILT |
1663 | for (Output_data_list::iterator p = pdl->begin(); |
1664 | p != pdl->end(); | |
1665 | ++p) | |
1666 | { | |
ead1e424 | 1667 | off = align_address(off, (*p)->addralign()); |
27bc2bce | 1668 | (*p)->set_address_and_file_offset(addr + (off - startoff), off); |
ead1e424 ILT |
1669 | |
1670 | // Unless this is a PT_TLS segment, we want to ignore the size | |
1671 | // of a SHF_TLS/SHT_NOBITS section. Such a section does not | |
1672 | // affect the size of a PT_LOAD segment. | |
1673 | if (this->type_ == elfcpp::PT_TLS | |
1674 | || !(*p)->is_section_flag_set(elfcpp::SHF_TLS) | |
1675 | || !(*p)->is_section_type(elfcpp::SHT_NOBITS)) | |
1676 | off += (*p)->data_size(); | |
75f65a3e | 1677 | |
ead1e424 ILT |
1678 | if ((*p)->is_section()) |
1679 | { | |
1680 | (*p)->set_out_shndx(*pshndx); | |
1681 | ++*pshndx; | |
1682 | } | |
75f65a3e ILT |
1683 | } |
1684 | ||
1685 | *poff = off; | |
ead1e424 | 1686 | return addr + (off - startoff); |
75f65a3e ILT |
1687 | } |
1688 | ||
1689 | // For a non-PT_LOAD segment, set the offset from the sections, if | |
1690 | // any. | |
1691 | ||
1692 | void | |
1693 | Output_segment::set_offset() | |
1694 | { | |
a3ad94ed | 1695 | gold_assert(this->type_ != elfcpp::PT_LOAD); |
75f65a3e ILT |
1696 | |
1697 | if (this->output_data_.empty() && this->output_bss_.empty()) | |
1698 | { | |
1699 | this->vaddr_ = 0; | |
1700 | this->paddr_ = 0; | |
1701 | this->memsz_ = 0; | |
1702 | this->align_ = 0; | |
1703 | this->offset_ = 0; | |
1704 | this->filesz_ = 0; | |
1705 | return; | |
1706 | } | |
1707 | ||
1708 | const Output_data* first; | |
1709 | if (this->output_data_.empty()) | |
1710 | first = this->output_bss_.front(); | |
1711 | else | |
1712 | first = this->output_data_.front(); | |
1713 | this->vaddr_ = first->address(); | |
1714 | this->paddr_ = this->vaddr_; | |
1715 | this->offset_ = first->offset(); | |
1716 | ||
1717 | if (this->output_data_.empty()) | |
1718 | this->filesz_ = 0; | |
1719 | else | |
1720 | { | |
1721 | const Output_data* last_data = this->output_data_.back(); | |
1722 | this->filesz_ = (last_data->address() | |
1723 | + last_data->data_size() | |
1724 | - this->vaddr_); | |
1725 | } | |
1726 | ||
1727 | const Output_data* last; | |
1728 | if (this->output_bss_.empty()) | |
1729 | last = this->output_data_.back(); | |
1730 | else | |
1731 | last = this->output_bss_.back(); | |
1732 | this->memsz_ = (last->address() | |
1733 | + last->data_size() | |
1734 | - this->vaddr_); | |
75f65a3e ILT |
1735 | } |
1736 | ||
1737 | // Return the number of Output_sections in an Output_segment. | |
1738 | ||
1739 | unsigned int | |
1740 | Output_segment::output_section_count() const | |
1741 | { | |
1742 | return (this->output_section_count_list(&this->output_data_) | |
1743 | + this->output_section_count_list(&this->output_bss_)); | |
1744 | } | |
1745 | ||
1746 | // Return the number of Output_sections in an Output_data_list. | |
1747 | ||
1748 | unsigned int | |
1749 | Output_segment::output_section_count_list(const Output_data_list* pdl) const | |
1750 | { | |
1751 | unsigned int count = 0; | |
1752 | for (Output_data_list::const_iterator p = pdl->begin(); | |
1753 | p != pdl->end(); | |
1754 | ++p) | |
1755 | { | |
1756 | if ((*p)->is_section()) | |
1757 | ++count; | |
1758 | } | |
1759 | return count; | |
a2fb1b05 ILT |
1760 | } |
1761 | ||
61ba1cf9 ILT |
1762 | // Write the segment data into *OPHDR. |
1763 | ||
1764 | template<int size, bool big_endian> | |
1765 | void | |
ead1e424 | 1766 | Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr) |
61ba1cf9 ILT |
1767 | { |
1768 | ophdr->put_p_type(this->type_); | |
1769 | ophdr->put_p_offset(this->offset_); | |
1770 | ophdr->put_p_vaddr(this->vaddr_); | |
1771 | ophdr->put_p_paddr(this->paddr_); | |
1772 | ophdr->put_p_filesz(this->filesz_); | |
1773 | ophdr->put_p_memsz(this->memsz_); | |
1774 | ophdr->put_p_flags(this->flags_); | |
ead1e424 | 1775 | ophdr->put_p_align(this->addralign()); |
61ba1cf9 ILT |
1776 | } |
1777 | ||
1778 | // Write the section headers into V. | |
1779 | ||
1780 | template<int size, bool big_endian> | |
1781 | unsigned char* | |
16649710 ILT |
1782 | Output_segment::write_section_headers(const Layout* layout, |
1783 | const Stringpool* secnamepool, | |
ead1e424 ILT |
1784 | unsigned char* v, |
1785 | unsigned int *pshndx | |
5482377d ILT |
1786 | ACCEPT_SIZE_ENDIAN) const |
1787 | { | |
ead1e424 ILT |
1788 | // Every section that is attached to a segment must be attached to a |
1789 | // PT_LOAD segment, so we only write out section headers for PT_LOAD | |
1790 | // segments. | |
1791 | if (this->type_ != elfcpp::PT_LOAD) | |
1792 | return v; | |
1793 | ||
593f47df ILT |
1794 | v = this->write_section_headers_list |
1795 | SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( | |
16649710 | 1796 | layout, secnamepool, &this->output_data_, v, pshndx |
593f47df ILT |
1797 | SELECT_SIZE_ENDIAN(size, big_endian)); |
1798 | v = this->write_section_headers_list | |
1799 | SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( | |
16649710 | 1800 | layout, secnamepool, &this->output_bss_, v, pshndx |
593f47df | 1801 | SELECT_SIZE_ENDIAN(size, big_endian)); |
61ba1cf9 ILT |
1802 | return v; |
1803 | } | |
1804 | ||
1805 | template<int size, bool big_endian> | |
1806 | unsigned char* | |
16649710 ILT |
1807 | Output_segment::write_section_headers_list(const Layout* layout, |
1808 | const Stringpool* secnamepool, | |
61ba1cf9 | 1809 | const Output_data_list* pdl, |
ead1e424 ILT |
1810 | unsigned char* v, |
1811 | unsigned int* pshndx | |
5482377d | 1812 | ACCEPT_SIZE_ENDIAN) const |
61ba1cf9 ILT |
1813 | { |
1814 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
1815 | for (Output_data_list::const_iterator p = pdl->begin(); | |
1816 | p != pdl->end(); | |
1817 | ++p) | |
1818 | { | |
1819 | if ((*p)->is_section()) | |
1820 | { | |
5482377d | 1821 | const Output_section* ps = static_cast<const Output_section*>(*p); |
a3ad94ed | 1822 | gold_assert(*pshndx == ps->out_shndx()); |
61ba1cf9 | 1823 | elfcpp::Shdr_write<size, big_endian> oshdr(v); |
16649710 | 1824 | ps->write_header(layout, secnamepool, &oshdr); |
61ba1cf9 | 1825 | v += shdr_size; |
ead1e424 | 1826 | ++*pshndx; |
61ba1cf9 ILT |
1827 | } |
1828 | } | |
1829 | return v; | |
1830 | } | |
1831 | ||
a2fb1b05 ILT |
1832 | // Output_file methods. |
1833 | ||
c51e6221 | 1834 | Output_file::Output_file(const General_options& options, Target* target) |
61ba1cf9 | 1835 | : options_(options), |
c51e6221 | 1836 | target_(target), |
61ba1cf9 ILT |
1837 | name_(options.output_file_name()), |
1838 | o_(-1), | |
1839 | file_size_(0), | |
1840 | base_(NULL) | |
1841 | { | |
1842 | } | |
1843 | ||
1844 | // Open the output file. | |
1845 | ||
a2fb1b05 | 1846 | void |
61ba1cf9 | 1847 | Output_file::open(off_t file_size) |
a2fb1b05 | 1848 | { |
61ba1cf9 ILT |
1849 | this->file_size_ = file_size; |
1850 | ||
4e9d8586 ILT |
1851 | // Unlink the file first; otherwise the open() may fail if the file |
1852 | // is busy (e.g. it's an executable that's currently being executed). | |
1853 | // | |
1854 | // However, the linker may be part of a system where a zero-length | |
1855 | // file is created for it to write to, with tight permissions (gcc | |
1856 | // 2.95 did something like this). Unlinking the file would work | |
1857 | // around those permission controls, so we only unlink if the file | |
1858 | // has a non-zero size. We also unlink only regular files to avoid | |
1859 | // trouble with directories/etc. | |
1860 | // | |
1861 | // If we fail, continue; this command is merely a best-effort attempt | |
1862 | // to improve the odds for open(). | |
1863 | ||
4e9d8586 | 1864 | struct stat s; |
5ffcaa86 ILT |
1865 | if (::stat(this->name_, &s) == 0 && s.st_size != 0) |
1866 | unlink_if_ordinary(this->name_); | |
4e9d8586 | 1867 | |
7e1edb90 | 1868 | int mode = parameters->output_is_object() ? 0666 : 0777; |
61ba1cf9 ILT |
1869 | int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode); |
1870 | if (o < 0) | |
75f2446e | 1871 | gold_fatal(_("%s: open: %s"), this->name_, strerror(errno)); |
61ba1cf9 ILT |
1872 | this->o_ = o; |
1873 | ||
27bc2bce ILT |
1874 | this->map(); |
1875 | } | |
1876 | ||
1877 | // Resize the output file. | |
1878 | ||
1879 | void | |
1880 | Output_file::resize(off_t file_size) | |
1881 | { | |
1882 | if (::munmap(this->base_, this->file_size_) < 0) | |
1883 | gold_error(_("%s: munmap: %s"), this->name_, strerror(errno)); | |
1884 | this->file_size_ = file_size; | |
1885 | this->map(); | |
1886 | } | |
1887 | ||
1888 | // Map the file into memory. | |
1889 | ||
1890 | void | |
1891 | Output_file::map() | |
1892 | { | |
1893 | int o = this->o_; | |
1894 | ||
61ba1cf9 | 1895 | // Write out one byte to make the file the right size. |
27bc2bce | 1896 | if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0) |
75f2446e | 1897 | gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno)); |
61ba1cf9 ILT |
1898 | char b = 0; |
1899 | if (::write(o, &b, 1) != 1) | |
75f2446e | 1900 | gold_fatal(_("%s: write: %s"), this->name_, strerror(errno)); |
61ba1cf9 ILT |
1901 | |
1902 | // Map the file into memory. | |
27bc2bce | 1903 | void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE, |
61ba1cf9 ILT |
1904 | MAP_SHARED, o, 0); |
1905 | if (base == MAP_FAILED) | |
75f2446e | 1906 | gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno)); |
61ba1cf9 ILT |
1907 | this->base_ = static_cast<unsigned char*>(base); |
1908 | } | |
1909 | ||
1910 | // Close the output file. | |
1911 | ||
1912 | void | |
1913 | Output_file::close() | |
1914 | { | |
1915 | if (::munmap(this->base_, this->file_size_) < 0) | |
a0c4fb0a | 1916 | gold_error(_("%s: munmap: %s"), this->name_, strerror(errno)); |
61ba1cf9 ILT |
1917 | this->base_ = NULL; |
1918 | ||
1919 | if (::close(this->o_) < 0) | |
75f2446e | 1920 | gold_error(_("%s: close: %s"), this->name_, strerror(errno)); |
61ba1cf9 | 1921 | this->o_ = -1; |
a2fb1b05 ILT |
1922 | } |
1923 | ||
1924 | // Instantiate the templates we need. We could use the configure | |
1925 | // script to restrict this to only the ones for implemented targets. | |
1926 | ||
193a53d9 | 1927 | #ifdef HAVE_TARGET_32_LITTLE |
a2fb1b05 ILT |
1928 | template |
1929 | off_t | |
1930 | Output_section::add_input_section<32, false>( | |
730cdc88 | 1931 | Sized_relobj<32, false>* object, |
ead1e424 | 1932 | unsigned int shndx, |
a2fb1b05 | 1933 | const char* secname, |
730cdc88 ILT |
1934 | const elfcpp::Shdr<32, false>& shdr, |
1935 | unsigned int reloc_shndx); | |
193a53d9 | 1936 | #endif |
a2fb1b05 | 1937 | |
193a53d9 | 1938 | #ifdef HAVE_TARGET_32_BIG |
a2fb1b05 ILT |
1939 | template |
1940 | off_t | |
1941 | Output_section::add_input_section<32, true>( | |
730cdc88 | 1942 | Sized_relobj<32, true>* object, |
ead1e424 | 1943 | unsigned int shndx, |
a2fb1b05 | 1944 | const char* secname, |
730cdc88 ILT |
1945 | const elfcpp::Shdr<32, true>& shdr, |
1946 | unsigned int reloc_shndx); | |
193a53d9 | 1947 | #endif |
a2fb1b05 | 1948 | |
193a53d9 | 1949 | #ifdef HAVE_TARGET_64_LITTLE |
a2fb1b05 ILT |
1950 | template |
1951 | off_t | |
1952 | Output_section::add_input_section<64, false>( | |
730cdc88 | 1953 | Sized_relobj<64, false>* object, |
ead1e424 | 1954 | unsigned int shndx, |
a2fb1b05 | 1955 | const char* secname, |
730cdc88 ILT |
1956 | const elfcpp::Shdr<64, false>& shdr, |
1957 | unsigned int reloc_shndx); | |
193a53d9 | 1958 | #endif |
a2fb1b05 | 1959 | |
193a53d9 | 1960 | #ifdef HAVE_TARGET_64_BIG |
a2fb1b05 ILT |
1961 | template |
1962 | off_t | |
1963 | Output_section::add_input_section<64, true>( | |
730cdc88 | 1964 | Sized_relobj<64, true>* object, |
ead1e424 | 1965 | unsigned int shndx, |
a2fb1b05 | 1966 | const char* secname, |
730cdc88 ILT |
1967 | const elfcpp::Shdr<64, true>& shdr, |
1968 | unsigned int reloc_shndx); | |
193a53d9 | 1969 | #endif |
a2fb1b05 | 1970 | |
193a53d9 | 1971 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
1972 | template |
1973 | class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>; | |
193a53d9 | 1974 | #endif |
c06b7b0b | 1975 | |
193a53d9 | 1976 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
1977 | template |
1978 | class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>; | |
193a53d9 | 1979 | #endif |
c06b7b0b | 1980 | |
193a53d9 | 1981 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
1982 | template |
1983 | class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>; | |
193a53d9 | 1984 | #endif |
c06b7b0b | 1985 | |
193a53d9 | 1986 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
1987 | template |
1988 | class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>; | |
193a53d9 | 1989 | #endif |
c06b7b0b | 1990 | |
193a53d9 | 1991 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
1992 | template |
1993 | class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>; | |
193a53d9 | 1994 | #endif |
c06b7b0b | 1995 | |
193a53d9 | 1996 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
1997 | template |
1998 | class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>; | |
193a53d9 | 1999 | #endif |
c06b7b0b | 2000 | |
193a53d9 | 2001 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
2002 | template |
2003 | class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>; | |
193a53d9 | 2004 | #endif |
c06b7b0b | 2005 | |
193a53d9 | 2006 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
2007 | template |
2008 | class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>; | |
193a53d9 | 2009 | #endif |
c06b7b0b | 2010 | |
193a53d9 | 2011 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
2012 | template |
2013 | class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>; | |
193a53d9 | 2014 | #endif |
c06b7b0b | 2015 | |
193a53d9 | 2016 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
2017 | template |
2018 | class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>; | |
193a53d9 | 2019 | #endif |
c06b7b0b | 2020 | |
193a53d9 | 2021 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
2022 | template |
2023 | class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>; | |
193a53d9 | 2024 | #endif |
c06b7b0b | 2025 | |
193a53d9 | 2026 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
2027 | template |
2028 | class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>; | |
193a53d9 | 2029 | #endif |
c06b7b0b | 2030 | |
193a53d9 | 2031 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
2032 | template |
2033 | class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>; | |
193a53d9 | 2034 | #endif |
c06b7b0b | 2035 | |
193a53d9 | 2036 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
2037 | template |
2038 | class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>; | |
193a53d9 | 2039 | #endif |
c06b7b0b | 2040 | |
193a53d9 | 2041 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
2042 | template |
2043 | class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>; | |
193a53d9 | 2044 | #endif |
c06b7b0b | 2045 | |
193a53d9 | 2046 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
2047 | template |
2048 | class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>; | |
193a53d9 | 2049 | #endif |
c06b7b0b | 2050 | |
193a53d9 | 2051 | #ifdef HAVE_TARGET_32_LITTLE |
ead1e424 | 2052 | template |
dbe717ef | 2053 | class Output_data_got<32, false>; |
193a53d9 | 2054 | #endif |
ead1e424 | 2055 | |
193a53d9 | 2056 | #ifdef HAVE_TARGET_32_BIG |
ead1e424 | 2057 | template |
dbe717ef | 2058 | class Output_data_got<32, true>; |
193a53d9 | 2059 | #endif |
ead1e424 | 2060 | |
193a53d9 | 2061 | #ifdef HAVE_TARGET_64_LITTLE |
ead1e424 | 2062 | template |
dbe717ef | 2063 | class Output_data_got<64, false>; |
193a53d9 | 2064 | #endif |
ead1e424 | 2065 | |
193a53d9 | 2066 | #ifdef HAVE_TARGET_64_BIG |
ead1e424 | 2067 | template |
dbe717ef | 2068 | class Output_data_got<64, true>; |
193a53d9 | 2069 | #endif |
ead1e424 | 2070 | |
a2fb1b05 | 2071 | } // End namespace gold. |