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