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