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