Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // output.cc -- manage the output file for gold |
2 | ||
e29e076a | 3 | // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
6cb15b7f ILT |
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> | |
04bf7072 | 26 | #include <cstring> |
61ba1cf9 ILT |
27 | #include <cerrno> |
28 | #include <fcntl.h> | |
29 | #include <unistd.h> | |
30 | #include <sys/mman.h> | |
4e9d8586 | 31 | #include <sys/stat.h> |
75f65a3e | 32 | #include <algorithm> |
6a89f575 | 33 | #include "libiberty.h" |
a2fb1b05 | 34 | |
7e1edb90 | 35 | #include "parameters.h" |
a2fb1b05 | 36 | #include "object.h" |
ead1e424 ILT |
37 | #include "symtab.h" |
38 | #include "reloc.h" | |
b8e6aad9 | 39 | #include "merge.h" |
2a00e4fb | 40 | #include "descriptors.h" |
a2fb1b05 ILT |
41 | #include "output.h" |
42 | ||
c420411f ILT |
43 | // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS |
44 | #ifndef MAP_ANONYMOUS | |
45 | # define MAP_ANONYMOUS MAP_ANON | |
46 | #endif | |
47 | ||
9201d894 ILT |
48 | #ifndef HAVE_POSIX_FALLOCATE |
49 | // A dummy, non general, version of posix_fallocate. Here we just set | |
50 | // the file size and hope that there is enough disk space. FIXME: We | |
51 | // could allocate disk space by walking block by block and writing a | |
52 | // zero byte into each block. | |
53 | static int | |
54 | posix_fallocate(int o, off_t offset, off_t len) | |
55 | { | |
56 | return ftruncate(o, offset + len); | |
57 | } | |
58 | #endif // !defined(HAVE_POSIX_FALLOCATE) | |
59 | ||
a2fb1b05 ILT |
60 | namespace gold |
61 | { | |
62 | ||
a3ad94ed ILT |
63 | // Output_data variables. |
64 | ||
27bc2bce | 65 | bool Output_data::allocated_sizes_are_fixed; |
a3ad94ed | 66 | |
a2fb1b05 ILT |
67 | // Output_data methods. |
68 | ||
69 | Output_data::~Output_data() | |
70 | { | |
71 | } | |
72 | ||
730cdc88 ILT |
73 | // Return the default alignment for the target size. |
74 | ||
75 | uint64_t | |
76 | Output_data::default_alignment() | |
77 | { | |
8851ecca ILT |
78 | return Output_data::default_alignment_for_size( |
79 | parameters->target().get_size()); | |
730cdc88 ILT |
80 | } |
81 | ||
75f65a3e ILT |
82 | // Return the default alignment for a size--32 or 64. |
83 | ||
84 | uint64_t | |
730cdc88 | 85 | Output_data::default_alignment_for_size(int size) |
75f65a3e ILT |
86 | { |
87 | if (size == 32) | |
88 | return 4; | |
89 | else if (size == 64) | |
90 | return 8; | |
91 | else | |
a3ad94ed | 92 | gold_unreachable(); |
75f65a3e ILT |
93 | } |
94 | ||
75f65a3e ILT |
95 | // Output_section_header methods. This currently assumes that the |
96 | // segment and section lists are complete at construction time. | |
97 | ||
98 | Output_section_headers::Output_section_headers( | |
16649710 ILT |
99 | const Layout* layout, |
100 | const Layout::Segment_list* segment_list, | |
6a74a719 | 101 | const Layout::Section_list* section_list, |
16649710 | 102 | const Layout::Section_list* unattached_section_list, |
d491d34e ILT |
103 | const Stringpool* secnamepool, |
104 | const Output_section* shstrtab_section) | |
9025d29d | 105 | : layout_(layout), |
75f65a3e | 106 | segment_list_(segment_list), |
6a74a719 | 107 | section_list_(section_list), |
a3ad94ed | 108 | unattached_section_list_(unattached_section_list), |
d491d34e ILT |
109 | secnamepool_(secnamepool), |
110 | shstrtab_section_(shstrtab_section) | |
20e6d0d6 DK |
111 | { |
112 | } | |
113 | ||
114 | // Compute the current data size. | |
115 | ||
116 | off_t | |
117 | Output_section_headers::do_size() const | |
75f65a3e | 118 | { |
61ba1cf9 ILT |
119 | // Count all the sections. Start with 1 for the null section. |
120 | off_t count = 1; | |
8851ecca | 121 | if (!parameters->options().relocatable()) |
6a74a719 | 122 | { |
20e6d0d6 DK |
123 | for (Layout::Segment_list::const_iterator p = |
124 | this->segment_list_->begin(); | |
125 | p != this->segment_list_->end(); | |
6a74a719 ILT |
126 | ++p) |
127 | if ((*p)->type() == elfcpp::PT_LOAD) | |
128 | count += (*p)->output_section_count(); | |
129 | } | |
130 | else | |
131 | { | |
20e6d0d6 DK |
132 | for (Layout::Section_list::const_iterator p = |
133 | this->section_list_->begin(); | |
134 | p != this->section_list_->end(); | |
6a74a719 ILT |
135 | ++p) |
136 | if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0) | |
137 | ++count; | |
138 | } | |
20e6d0d6 | 139 | count += this->unattached_section_list_->size(); |
75f65a3e | 140 | |
8851ecca | 141 | const int size = parameters->target().get_size(); |
75f65a3e ILT |
142 | int shdr_size; |
143 | if (size == 32) | |
144 | shdr_size = elfcpp::Elf_sizes<32>::shdr_size; | |
145 | else if (size == 64) | |
146 | shdr_size = elfcpp::Elf_sizes<64>::shdr_size; | |
147 | else | |
a3ad94ed | 148 | gold_unreachable(); |
75f65a3e | 149 | |
20e6d0d6 | 150 | return count * shdr_size; |
75f65a3e ILT |
151 | } |
152 | ||
61ba1cf9 ILT |
153 | // Write out the section headers. |
154 | ||
75f65a3e | 155 | void |
61ba1cf9 | 156 | Output_section_headers::do_write(Output_file* of) |
a2fb1b05 | 157 | { |
8851ecca | 158 | switch (parameters->size_and_endianness()) |
61ba1cf9 | 159 | { |
9025d29d | 160 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
161 | case Parameters::TARGET_32_LITTLE: |
162 | this->do_sized_write<32, false>(of); | |
163 | break; | |
9025d29d | 164 | #endif |
8851ecca ILT |
165 | #ifdef HAVE_TARGET_32_BIG |
166 | case Parameters::TARGET_32_BIG: | |
167 | this->do_sized_write<32, true>(of); | |
168 | break; | |
9025d29d | 169 | #endif |
9025d29d | 170 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
171 | case Parameters::TARGET_64_LITTLE: |
172 | this->do_sized_write<64, false>(of); | |
173 | break; | |
9025d29d | 174 | #endif |
8851ecca ILT |
175 | #ifdef HAVE_TARGET_64_BIG |
176 | case Parameters::TARGET_64_BIG: | |
177 | this->do_sized_write<64, true>(of); | |
178 | break; | |
179 | #endif | |
180 | default: | |
181 | gold_unreachable(); | |
61ba1cf9 | 182 | } |
61ba1cf9 ILT |
183 | } |
184 | ||
185 | template<int size, bool big_endian> | |
186 | void | |
187 | Output_section_headers::do_sized_write(Output_file* of) | |
188 | { | |
189 | off_t all_shdrs_size = this->data_size(); | |
190 | unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size); | |
191 | ||
192 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
193 | unsigned char* v = view; | |
194 | ||
195 | { | |
196 | typename elfcpp::Shdr_write<size, big_endian> oshdr(v); | |
197 | oshdr.put_sh_name(0); | |
198 | oshdr.put_sh_type(elfcpp::SHT_NULL); | |
199 | oshdr.put_sh_flags(0); | |
200 | oshdr.put_sh_addr(0); | |
201 | oshdr.put_sh_offset(0); | |
d491d34e ILT |
202 | |
203 | size_t section_count = (this->data_size() | |
204 | / elfcpp::Elf_sizes<size>::shdr_size); | |
205 | if (section_count < elfcpp::SHN_LORESERVE) | |
206 | oshdr.put_sh_size(0); | |
207 | else | |
208 | oshdr.put_sh_size(section_count); | |
209 | ||
210 | unsigned int shstrndx = this->shstrtab_section_->out_shndx(); | |
211 | if (shstrndx < elfcpp::SHN_LORESERVE) | |
212 | oshdr.put_sh_link(0); | |
213 | else | |
214 | oshdr.put_sh_link(shstrndx); | |
215 | ||
61ba1cf9 ILT |
216 | oshdr.put_sh_info(0); |
217 | oshdr.put_sh_addralign(0); | |
218 | oshdr.put_sh_entsize(0); | |
219 | } | |
220 | ||
221 | v += shdr_size; | |
222 | ||
6a74a719 | 223 | unsigned int shndx = 1; |
8851ecca | 224 | if (!parameters->options().relocatable()) |
6a74a719 ILT |
225 | { |
226 | for (Layout::Segment_list::const_iterator p = | |
227 | this->segment_list_->begin(); | |
228 | p != this->segment_list_->end(); | |
229 | ++p) | |
230 | v = (*p)->write_section_headers<size, big_endian>(this->layout_, | |
231 | this->secnamepool_, | |
232 | v, | |
233 | &shndx); | |
234 | } | |
235 | else | |
236 | { | |
237 | for (Layout::Section_list::const_iterator p = | |
238 | this->section_list_->begin(); | |
239 | p != this->section_list_->end(); | |
240 | ++p) | |
241 | { | |
242 | // We do unallocated sections below, except that group | |
243 | // sections have to come first. | |
244 | if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0 | |
245 | && (*p)->type() != elfcpp::SHT_GROUP) | |
246 | continue; | |
247 | gold_assert(shndx == (*p)->out_shndx()); | |
248 | elfcpp::Shdr_write<size, big_endian> oshdr(v); | |
249 | (*p)->write_header(this->layout_, this->secnamepool_, &oshdr); | |
250 | v += shdr_size; | |
251 | ++shndx; | |
252 | } | |
253 | } | |
254 | ||
a3ad94ed | 255 | for (Layout::Section_list::const_iterator p = |
16649710 ILT |
256 | this->unattached_section_list_->begin(); |
257 | p != this->unattached_section_list_->end(); | |
61ba1cf9 ILT |
258 | ++p) |
259 | { | |
6a74a719 ILT |
260 | // For a relocatable link, we did unallocated group sections |
261 | // above, since they have to come first. | |
262 | if ((*p)->type() == elfcpp::SHT_GROUP | |
8851ecca | 263 | && parameters->options().relocatable()) |
6a74a719 | 264 | continue; |
a3ad94ed | 265 | gold_assert(shndx == (*p)->out_shndx()); |
61ba1cf9 | 266 | elfcpp::Shdr_write<size, big_endian> oshdr(v); |
16649710 | 267 | (*p)->write_header(this->layout_, this->secnamepool_, &oshdr); |
61ba1cf9 | 268 | v += shdr_size; |
ead1e424 | 269 | ++shndx; |
61ba1cf9 ILT |
270 | } |
271 | ||
272 | of->write_output_view(this->offset(), all_shdrs_size, view); | |
a2fb1b05 ILT |
273 | } |
274 | ||
54dc6425 ILT |
275 | // Output_segment_header methods. |
276 | ||
61ba1cf9 | 277 | Output_segment_headers::Output_segment_headers( |
61ba1cf9 | 278 | const Layout::Segment_list& segment_list) |
9025d29d | 279 | : segment_list_(segment_list) |
61ba1cf9 | 280 | { |
61ba1cf9 ILT |
281 | } |
282 | ||
54dc6425 | 283 | void |
61ba1cf9 | 284 | Output_segment_headers::do_write(Output_file* of) |
75f65a3e | 285 | { |
8851ecca | 286 | switch (parameters->size_and_endianness()) |
61ba1cf9 | 287 | { |
9025d29d | 288 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
289 | case Parameters::TARGET_32_LITTLE: |
290 | this->do_sized_write<32, false>(of); | |
291 | break; | |
9025d29d | 292 | #endif |
8851ecca ILT |
293 | #ifdef HAVE_TARGET_32_BIG |
294 | case Parameters::TARGET_32_BIG: | |
295 | this->do_sized_write<32, true>(of); | |
296 | break; | |
9025d29d | 297 | #endif |
9025d29d | 298 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
299 | case Parameters::TARGET_64_LITTLE: |
300 | this->do_sized_write<64, false>(of); | |
301 | break; | |
9025d29d | 302 | #endif |
8851ecca ILT |
303 | #ifdef HAVE_TARGET_64_BIG |
304 | case Parameters::TARGET_64_BIG: | |
305 | this->do_sized_write<64, true>(of); | |
306 | break; | |
307 | #endif | |
308 | default: | |
309 | gold_unreachable(); | |
61ba1cf9 | 310 | } |
61ba1cf9 ILT |
311 | } |
312 | ||
313 | template<int size, bool big_endian> | |
314 | void | |
315 | Output_segment_headers::do_sized_write(Output_file* of) | |
316 | { | |
317 | const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size; | |
318 | off_t all_phdrs_size = this->segment_list_.size() * phdr_size; | |
a445fddf | 319 | gold_assert(all_phdrs_size == this->data_size()); |
61ba1cf9 ILT |
320 | unsigned char* view = of->get_output_view(this->offset(), |
321 | all_phdrs_size); | |
322 | unsigned char* v = view; | |
323 | for (Layout::Segment_list::const_iterator p = this->segment_list_.begin(); | |
324 | p != this->segment_list_.end(); | |
325 | ++p) | |
326 | { | |
327 | elfcpp::Phdr_write<size, big_endian> ophdr(v); | |
328 | (*p)->write_header(&ophdr); | |
329 | v += phdr_size; | |
330 | } | |
331 | ||
a445fddf ILT |
332 | gold_assert(v - view == all_phdrs_size); |
333 | ||
61ba1cf9 | 334 | of->write_output_view(this->offset(), all_phdrs_size, view); |
75f65a3e ILT |
335 | } |
336 | ||
20e6d0d6 DK |
337 | off_t |
338 | Output_segment_headers::do_size() const | |
339 | { | |
340 | const int size = parameters->target().get_size(); | |
341 | int phdr_size; | |
342 | if (size == 32) | |
343 | phdr_size = elfcpp::Elf_sizes<32>::phdr_size; | |
344 | else if (size == 64) | |
345 | phdr_size = elfcpp::Elf_sizes<64>::phdr_size; | |
346 | else | |
347 | gold_unreachable(); | |
348 | ||
349 | return this->segment_list_.size() * phdr_size; | |
350 | } | |
351 | ||
75f65a3e ILT |
352 | // Output_file_header methods. |
353 | ||
9025d29d | 354 | Output_file_header::Output_file_header(const Target* target, |
75f65a3e | 355 | const Symbol_table* symtab, |
d391083d ILT |
356 | const Output_segment_headers* osh, |
357 | const char* entry) | |
9025d29d | 358 | : target_(target), |
75f65a3e | 359 | symtab_(symtab), |
61ba1cf9 | 360 | segment_header_(osh), |
75f65a3e | 361 | section_header_(NULL), |
d391083d ILT |
362 | shstrtab_(NULL), |
363 | entry_(entry) | |
75f65a3e | 364 | { |
20e6d0d6 | 365 | this->set_data_size(this->do_size()); |
75f65a3e ILT |
366 | } |
367 | ||
368 | // Set the section table information for a file header. | |
369 | ||
370 | void | |
371 | Output_file_header::set_section_info(const Output_section_headers* shdrs, | |
372 | const Output_section* shstrtab) | |
373 | { | |
374 | this->section_header_ = shdrs; | |
375 | this->shstrtab_ = shstrtab; | |
376 | } | |
377 | ||
378 | // Write out the file header. | |
379 | ||
380 | void | |
61ba1cf9 | 381 | Output_file_header::do_write(Output_file* of) |
54dc6425 | 382 | { |
27bc2bce ILT |
383 | gold_assert(this->offset() == 0); |
384 | ||
8851ecca | 385 | switch (parameters->size_and_endianness()) |
61ba1cf9 | 386 | { |
9025d29d | 387 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
388 | case Parameters::TARGET_32_LITTLE: |
389 | this->do_sized_write<32, false>(of); | |
390 | break; | |
9025d29d | 391 | #endif |
8851ecca ILT |
392 | #ifdef HAVE_TARGET_32_BIG |
393 | case Parameters::TARGET_32_BIG: | |
394 | this->do_sized_write<32, true>(of); | |
395 | break; | |
9025d29d | 396 | #endif |
9025d29d | 397 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
398 | case Parameters::TARGET_64_LITTLE: |
399 | this->do_sized_write<64, false>(of); | |
400 | break; | |
9025d29d | 401 | #endif |
8851ecca ILT |
402 | #ifdef HAVE_TARGET_64_BIG |
403 | case Parameters::TARGET_64_BIG: | |
404 | this->do_sized_write<64, true>(of); | |
405 | break; | |
406 | #endif | |
407 | default: | |
408 | gold_unreachable(); | |
61ba1cf9 | 409 | } |
61ba1cf9 ILT |
410 | } |
411 | ||
412 | // Write out the file header with appropriate size and endianess. | |
413 | ||
414 | template<int size, bool big_endian> | |
415 | void | |
416 | Output_file_header::do_sized_write(Output_file* of) | |
417 | { | |
a3ad94ed | 418 | gold_assert(this->offset() == 0); |
61ba1cf9 ILT |
419 | |
420 | int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; | |
421 | unsigned char* view = of->get_output_view(0, ehdr_size); | |
422 | elfcpp::Ehdr_write<size, big_endian> oehdr(view); | |
423 | ||
424 | unsigned char e_ident[elfcpp::EI_NIDENT]; | |
425 | memset(e_ident, 0, elfcpp::EI_NIDENT); | |
426 | e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0; | |
427 | e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1; | |
428 | e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2; | |
429 | e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3; | |
430 | if (size == 32) | |
431 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32; | |
432 | else if (size == 64) | |
433 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64; | |
434 | else | |
a3ad94ed | 435 | gold_unreachable(); |
61ba1cf9 ILT |
436 | e_ident[elfcpp::EI_DATA] = (big_endian |
437 | ? elfcpp::ELFDATA2MSB | |
438 | : elfcpp::ELFDATA2LSB); | |
439 | e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT; | |
61ba1cf9 ILT |
440 | oehdr.put_e_ident(e_ident); |
441 | ||
442 | elfcpp::ET e_type; | |
8851ecca | 443 | if (parameters->options().relocatable()) |
61ba1cf9 | 444 | e_type = elfcpp::ET_REL; |
8851ecca | 445 | else if (parameters->options().shared()) |
436ca963 | 446 | e_type = elfcpp::ET_DYN; |
61ba1cf9 ILT |
447 | else |
448 | e_type = elfcpp::ET_EXEC; | |
449 | oehdr.put_e_type(e_type); | |
450 | ||
451 | oehdr.put_e_machine(this->target_->machine_code()); | |
452 | oehdr.put_e_version(elfcpp::EV_CURRENT); | |
453 | ||
d391083d | 454 | oehdr.put_e_entry(this->entry<size>()); |
61ba1cf9 | 455 | |
6a74a719 ILT |
456 | if (this->segment_header_ == NULL) |
457 | oehdr.put_e_phoff(0); | |
458 | else | |
459 | oehdr.put_e_phoff(this->segment_header_->offset()); | |
460 | ||
61ba1cf9 ILT |
461 | oehdr.put_e_shoff(this->section_header_->offset()); |
462 | ||
463 | // FIXME: The target needs to set the flags. | |
464 | oehdr.put_e_flags(0); | |
465 | ||
466 | oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size); | |
6a74a719 ILT |
467 | |
468 | if (this->segment_header_ == NULL) | |
469 | { | |
470 | oehdr.put_e_phentsize(0); | |
471 | oehdr.put_e_phnum(0); | |
472 | } | |
473 | else | |
474 | { | |
475 | oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size); | |
476 | oehdr.put_e_phnum(this->segment_header_->data_size() | |
477 | / elfcpp::Elf_sizes<size>::phdr_size); | |
478 | } | |
479 | ||
61ba1cf9 | 480 | oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size); |
d491d34e ILT |
481 | size_t section_count = (this->section_header_->data_size() |
482 | / elfcpp::Elf_sizes<size>::shdr_size); | |
483 | ||
484 | if (section_count < elfcpp::SHN_LORESERVE) | |
485 | oehdr.put_e_shnum(this->section_header_->data_size() | |
486 | / elfcpp::Elf_sizes<size>::shdr_size); | |
487 | else | |
488 | oehdr.put_e_shnum(0); | |
489 | ||
490 | unsigned int shstrndx = this->shstrtab_->out_shndx(); | |
491 | if (shstrndx < elfcpp::SHN_LORESERVE) | |
492 | oehdr.put_e_shstrndx(this->shstrtab_->out_shndx()); | |
493 | else | |
494 | oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX); | |
61ba1cf9 | 495 | |
36959681 ILT |
496 | // Let the target adjust the ELF header, e.g., to set EI_OSABI in |
497 | // the e_ident field. | |
498 | parameters->target().adjust_elf_header(view, ehdr_size); | |
499 | ||
61ba1cf9 | 500 | of->write_output_view(0, ehdr_size, view); |
54dc6425 ILT |
501 | } |
502 | ||
d391083d ILT |
503 | // Return the value to use for the entry address. THIS->ENTRY_ is the |
504 | // symbol specified on the command line, if any. | |
505 | ||
506 | template<int size> | |
507 | typename elfcpp::Elf_types<size>::Elf_Addr | |
508 | Output_file_header::entry() | |
509 | { | |
510 | const bool should_issue_warning = (this->entry_ != NULL | |
8851ecca ILT |
511 | && !parameters->options().relocatable() |
512 | && !parameters->options().shared()); | |
d391083d ILT |
513 | |
514 | // FIXME: Need to support target specific entry symbol. | |
515 | const char* entry = this->entry_; | |
516 | if (entry == NULL) | |
517 | entry = "_start"; | |
518 | ||
519 | Symbol* sym = this->symtab_->lookup(entry); | |
520 | ||
521 | typename Sized_symbol<size>::Value_type v; | |
522 | if (sym != NULL) | |
523 | { | |
524 | Sized_symbol<size>* ssym; | |
525 | ssym = this->symtab_->get_sized_symbol<size>(sym); | |
526 | if (!ssym->is_defined() && should_issue_warning) | |
527 | gold_warning("entry symbol '%s' exists but is not defined", entry); | |
528 | v = ssym->value(); | |
529 | } | |
530 | else | |
531 | { | |
532 | // We couldn't find the entry symbol. See if we can parse it as | |
533 | // a number. This supports, e.g., -e 0x1000. | |
534 | char* endptr; | |
535 | v = strtoull(entry, &endptr, 0); | |
536 | if (*endptr != '\0') | |
537 | { | |
538 | if (should_issue_warning) | |
539 | gold_warning("cannot find entry symbol '%s'", entry); | |
540 | v = 0; | |
541 | } | |
542 | } | |
543 | ||
544 | return v; | |
545 | } | |
546 | ||
20e6d0d6 DK |
547 | // Compute the current data size. |
548 | ||
549 | off_t | |
550 | Output_file_header::do_size() const | |
551 | { | |
552 | const int size = parameters->target().get_size(); | |
553 | if (size == 32) | |
554 | return elfcpp::Elf_sizes<32>::ehdr_size; | |
555 | else if (size == 64) | |
556 | return elfcpp::Elf_sizes<64>::ehdr_size; | |
557 | else | |
558 | gold_unreachable(); | |
559 | } | |
560 | ||
dbe717ef ILT |
561 | // Output_data_const methods. |
562 | ||
563 | void | |
a3ad94ed | 564 | Output_data_const::do_write(Output_file* of) |
dbe717ef | 565 | { |
a3ad94ed ILT |
566 | of->write(this->offset(), this->data_.data(), this->data_.size()); |
567 | } | |
568 | ||
569 | // Output_data_const_buffer methods. | |
570 | ||
571 | void | |
572 | Output_data_const_buffer::do_write(Output_file* of) | |
573 | { | |
574 | of->write(this->offset(), this->p_, this->data_size()); | |
dbe717ef ILT |
575 | } |
576 | ||
577 | // Output_section_data methods. | |
578 | ||
16649710 ILT |
579 | // Record the output section, and set the entry size and such. |
580 | ||
581 | void | |
582 | Output_section_data::set_output_section(Output_section* os) | |
583 | { | |
584 | gold_assert(this->output_section_ == NULL); | |
585 | this->output_section_ = os; | |
586 | this->do_adjust_output_section(os); | |
587 | } | |
588 | ||
589 | // Return the section index of the output section. | |
590 | ||
dbe717ef ILT |
591 | unsigned int |
592 | Output_section_data::do_out_shndx() const | |
593 | { | |
a3ad94ed | 594 | gold_assert(this->output_section_ != NULL); |
dbe717ef ILT |
595 | return this->output_section_->out_shndx(); |
596 | } | |
597 | ||
759b1a24 ILT |
598 | // Set the alignment, which means we may need to update the alignment |
599 | // of the output section. | |
600 | ||
601 | void | |
602 | Output_section_data::set_addralign(uint64_t addralign) | |
603 | { | |
604 | this->addralign_ = addralign; | |
605 | if (this->output_section_ != NULL | |
606 | && this->output_section_->addralign() < addralign) | |
607 | this->output_section_->set_addralign(addralign); | |
608 | } | |
609 | ||
a3ad94ed ILT |
610 | // Output_data_strtab methods. |
611 | ||
27bc2bce | 612 | // Set the final data size. |
a3ad94ed ILT |
613 | |
614 | void | |
27bc2bce | 615 | Output_data_strtab::set_final_data_size() |
a3ad94ed ILT |
616 | { |
617 | this->strtab_->set_string_offsets(); | |
618 | this->set_data_size(this->strtab_->get_strtab_size()); | |
619 | } | |
620 | ||
621 | // Write out a string table. | |
622 | ||
623 | void | |
624 | Output_data_strtab::do_write(Output_file* of) | |
625 | { | |
626 | this->strtab_->write(of, this->offset()); | |
627 | } | |
628 | ||
c06b7b0b ILT |
629 | // Output_reloc methods. |
630 | ||
7bf1f802 ILT |
631 | // A reloc against a global symbol. |
632 | ||
633 | template<bool dynamic, int size, bool big_endian> | |
634 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
635 | Symbol* gsym, | |
636 | unsigned int type, | |
637 | Output_data* od, | |
e8c846c3 ILT |
638 | Address address, |
639 | bool is_relative) | |
7bf1f802 | 640 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), |
dceae3c1 | 641 | is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE) |
7bf1f802 | 642 | { |
dceae3c1 ILT |
643 | // this->type_ is a bitfield; make sure TYPE fits. |
644 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
645 | this->u1_.gsym = gsym; |
646 | this->u2_.od = od; | |
dceae3c1 ILT |
647 | if (dynamic) |
648 | this->set_needs_dynsym_index(); | |
7bf1f802 ILT |
649 | } |
650 | ||
651 | template<bool dynamic, int size, bool big_endian> | |
652 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
653 | Symbol* gsym, | |
654 | unsigned int type, | |
ef9beddf | 655 | Sized_relobj<size, big_endian>* relobj, |
7bf1f802 | 656 | unsigned int shndx, |
e8c846c3 ILT |
657 | Address address, |
658 | bool is_relative) | |
7bf1f802 | 659 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), |
dceae3c1 | 660 | is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx) |
7bf1f802 ILT |
661 | { |
662 | gold_assert(shndx != INVALID_CODE); | |
dceae3c1 ILT |
663 | // this->type_ is a bitfield; make sure TYPE fits. |
664 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
665 | this->u1_.gsym = gsym; |
666 | this->u2_.relobj = relobj; | |
dceae3c1 ILT |
667 | if (dynamic) |
668 | this->set_needs_dynsym_index(); | |
7bf1f802 ILT |
669 | } |
670 | ||
671 | // A reloc against a local symbol. | |
672 | ||
673 | template<bool dynamic, int size, bool big_endian> | |
674 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
675 | Sized_relobj<size, big_endian>* relobj, | |
676 | unsigned int local_sym_index, | |
677 | unsigned int type, | |
678 | Output_data* od, | |
e8c846c3 | 679 | Address address, |
dceae3c1 ILT |
680 | bool is_relative, |
681 | bool is_section_symbol) | |
7bf1f802 | 682 | : address_(address), local_sym_index_(local_sym_index), type_(type), |
dceae3c1 ILT |
683 | is_relative_(is_relative), is_section_symbol_(is_section_symbol), |
684 | shndx_(INVALID_CODE) | |
7bf1f802 ILT |
685 | { |
686 | gold_assert(local_sym_index != GSYM_CODE | |
687 | && local_sym_index != INVALID_CODE); | |
dceae3c1 ILT |
688 | // this->type_ is a bitfield; make sure TYPE fits. |
689 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
690 | this->u1_.relobj = relobj; |
691 | this->u2_.od = od; | |
dceae3c1 ILT |
692 | if (dynamic) |
693 | this->set_needs_dynsym_index(); | |
7bf1f802 ILT |
694 | } |
695 | ||
696 | template<bool dynamic, int size, bool big_endian> | |
697 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
698 | Sized_relobj<size, big_endian>* relobj, | |
699 | unsigned int local_sym_index, | |
700 | unsigned int type, | |
701 | unsigned int shndx, | |
e8c846c3 | 702 | Address address, |
dceae3c1 ILT |
703 | bool is_relative, |
704 | bool is_section_symbol) | |
7bf1f802 | 705 | : address_(address), local_sym_index_(local_sym_index), type_(type), |
dceae3c1 ILT |
706 | is_relative_(is_relative), is_section_symbol_(is_section_symbol), |
707 | shndx_(shndx) | |
7bf1f802 ILT |
708 | { |
709 | gold_assert(local_sym_index != GSYM_CODE | |
710 | && local_sym_index != INVALID_CODE); | |
711 | gold_assert(shndx != INVALID_CODE); | |
dceae3c1 ILT |
712 | // this->type_ is a bitfield; make sure TYPE fits. |
713 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
714 | this->u1_.relobj = relobj; |
715 | this->u2_.relobj = relobj; | |
dceae3c1 ILT |
716 | if (dynamic) |
717 | this->set_needs_dynsym_index(); | |
7bf1f802 ILT |
718 | } |
719 | ||
720 | // A reloc against the STT_SECTION symbol of an output section. | |
721 | ||
722 | template<bool dynamic, int size, bool big_endian> | |
723 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
724 | Output_section* os, | |
725 | unsigned int type, | |
726 | Output_data* od, | |
727 | Address address) | |
728 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), | |
dceae3c1 | 729 | is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE) |
7bf1f802 | 730 | { |
dceae3c1 ILT |
731 | // this->type_ is a bitfield; make sure TYPE fits. |
732 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
733 | this->u1_.os = os; |
734 | this->u2_.od = od; | |
735 | if (dynamic) | |
dceae3c1 ILT |
736 | this->set_needs_dynsym_index(); |
737 | else | |
738 | os->set_needs_symtab_index(); | |
7bf1f802 ILT |
739 | } |
740 | ||
741 | template<bool dynamic, int size, bool big_endian> | |
742 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
743 | Output_section* os, | |
744 | unsigned int type, | |
ef9beddf | 745 | Sized_relobj<size, big_endian>* relobj, |
7bf1f802 ILT |
746 | unsigned int shndx, |
747 | Address address) | |
748 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), | |
dceae3c1 | 749 | is_relative_(false), is_section_symbol_(true), shndx_(shndx) |
7bf1f802 ILT |
750 | { |
751 | gold_assert(shndx != INVALID_CODE); | |
dceae3c1 ILT |
752 | // this->type_ is a bitfield; make sure TYPE fits. |
753 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
754 | this->u1_.os = os; |
755 | this->u2_.relobj = relobj; | |
756 | if (dynamic) | |
dceae3c1 ILT |
757 | this->set_needs_dynsym_index(); |
758 | else | |
759 | os->set_needs_symtab_index(); | |
760 | } | |
761 | ||
762 | // Record that we need a dynamic symbol index for this relocation. | |
763 | ||
764 | template<bool dynamic, int size, bool big_endian> | |
765 | void | |
766 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>:: | |
767 | set_needs_dynsym_index() | |
768 | { | |
769 | if (this->is_relative_) | |
770 | return; | |
771 | switch (this->local_sym_index_) | |
772 | { | |
773 | case INVALID_CODE: | |
774 | gold_unreachable(); | |
775 | ||
776 | case GSYM_CODE: | |
777 | this->u1_.gsym->set_needs_dynsym_entry(); | |
778 | break; | |
779 | ||
780 | case SECTION_CODE: | |
781 | this->u1_.os->set_needs_dynsym_index(); | |
782 | break; | |
783 | ||
784 | case 0: | |
785 | break; | |
786 | ||
787 | default: | |
788 | { | |
789 | const unsigned int lsi = this->local_sym_index_; | |
790 | if (!this->is_section_symbol_) | |
791 | this->u1_.relobj->set_needs_output_dynsym_entry(lsi); | |
792 | else | |
ef9beddf | 793 | this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index(); |
dceae3c1 ILT |
794 | } |
795 | break; | |
796 | } | |
7bf1f802 ILT |
797 | } |
798 | ||
c06b7b0b ILT |
799 | // Get the symbol index of a relocation. |
800 | ||
801 | template<bool dynamic, int size, bool big_endian> | |
802 | unsigned int | |
803 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index() | |
804 | const | |
805 | { | |
806 | unsigned int index; | |
807 | switch (this->local_sym_index_) | |
808 | { | |
809 | case INVALID_CODE: | |
a3ad94ed | 810 | gold_unreachable(); |
c06b7b0b ILT |
811 | |
812 | case GSYM_CODE: | |
5a6f7e2d | 813 | if (this->u1_.gsym == NULL) |
c06b7b0b ILT |
814 | index = 0; |
815 | else if (dynamic) | |
5a6f7e2d | 816 | index = this->u1_.gsym->dynsym_index(); |
c06b7b0b | 817 | else |
5a6f7e2d | 818 | index = this->u1_.gsym->symtab_index(); |
c06b7b0b ILT |
819 | break; |
820 | ||
821 | case SECTION_CODE: | |
822 | if (dynamic) | |
5a6f7e2d | 823 | index = this->u1_.os->dynsym_index(); |
c06b7b0b | 824 | else |
5a6f7e2d | 825 | index = this->u1_.os->symtab_index(); |
c06b7b0b ILT |
826 | break; |
827 | ||
436ca963 ILT |
828 | case 0: |
829 | // Relocations without symbols use a symbol index of 0. | |
830 | index = 0; | |
831 | break; | |
832 | ||
c06b7b0b | 833 | default: |
dceae3c1 ILT |
834 | { |
835 | const unsigned int lsi = this->local_sym_index_; | |
836 | if (!this->is_section_symbol_) | |
837 | { | |
838 | if (dynamic) | |
839 | index = this->u1_.relobj->dynsym_index(lsi); | |
840 | else | |
841 | index = this->u1_.relobj->symtab_index(lsi); | |
842 | } | |
843 | else | |
844 | { | |
ef9beddf | 845 | Output_section* os = this->u1_.relobj->output_section(lsi); |
dceae3c1 ILT |
846 | gold_assert(os != NULL); |
847 | if (dynamic) | |
848 | index = os->dynsym_index(); | |
849 | else | |
850 | index = os->symtab_index(); | |
851 | } | |
852 | } | |
c06b7b0b ILT |
853 | break; |
854 | } | |
a3ad94ed | 855 | gold_assert(index != -1U); |
c06b7b0b ILT |
856 | return index; |
857 | } | |
858 | ||
624f8810 ILT |
859 | // For a local section symbol, get the address of the offset ADDEND |
860 | // within the input section. | |
dceae3c1 ILT |
861 | |
862 | template<bool dynamic, int size, bool big_endian> | |
ef9beddf | 863 | typename elfcpp::Elf_types<size>::Elf_Addr |
dceae3c1 | 864 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>:: |
624f8810 | 865 | local_section_offset(Addend addend) const |
dceae3c1 | 866 | { |
624f8810 ILT |
867 | gold_assert(this->local_sym_index_ != GSYM_CODE |
868 | && this->local_sym_index_ != SECTION_CODE | |
869 | && this->local_sym_index_ != INVALID_CODE | |
870 | && this->is_section_symbol_); | |
dceae3c1 | 871 | const unsigned int lsi = this->local_sym_index_; |
ef9beddf | 872 | Output_section* os = this->u1_.relobj->output_section(lsi); |
624f8810 | 873 | gold_assert(os != NULL); |
ef9beddf | 874 | Address offset = this->u1_.relobj->get_output_section_offset(lsi); |
eff45813 | 875 | if (offset != invalid_address) |
624f8810 ILT |
876 | return offset + addend; |
877 | // This is a merge section. | |
878 | offset = os->output_address(this->u1_.relobj, lsi, addend); | |
eff45813 | 879 | gold_assert(offset != invalid_address); |
dceae3c1 ILT |
880 | return offset; |
881 | } | |
882 | ||
d98bc257 | 883 | // Get the output address of a relocation. |
c06b7b0b ILT |
884 | |
885 | template<bool dynamic, int size, bool big_endian> | |
a984ee1d | 886 | typename elfcpp::Elf_types<size>::Elf_Addr |
d98bc257 | 887 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const |
c06b7b0b | 888 | { |
a3ad94ed | 889 | Address address = this->address_; |
5a6f7e2d ILT |
890 | if (this->shndx_ != INVALID_CODE) |
891 | { | |
ef9beddf | 892 | Output_section* os = this->u2_.relobj->output_section(this->shndx_); |
5a6f7e2d | 893 | gold_assert(os != NULL); |
ef9beddf | 894 | Address off = this->u2_.relobj->get_output_section_offset(this->shndx_); |
eff45813 | 895 | if (off != invalid_address) |
730cdc88 ILT |
896 | address += os->address() + off; |
897 | else | |
898 | { | |
899 | address = os->output_address(this->u2_.relobj, this->shndx_, | |
900 | address); | |
eff45813 | 901 | gold_assert(address != invalid_address); |
730cdc88 | 902 | } |
5a6f7e2d ILT |
903 | } |
904 | else if (this->u2_.od != NULL) | |
905 | address += this->u2_.od->address(); | |
d98bc257 ILT |
906 | return address; |
907 | } | |
908 | ||
909 | // Write out the offset and info fields of a Rel or Rela relocation | |
910 | // entry. | |
911 | ||
912 | template<bool dynamic, int size, bool big_endian> | |
913 | template<typename Write_rel> | |
914 | void | |
915 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel( | |
916 | Write_rel* wr) const | |
917 | { | |
918 | wr->put_r_offset(this->get_address()); | |
e8c846c3 ILT |
919 | unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index(); |
920 | wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_)); | |
c06b7b0b ILT |
921 | } |
922 | ||
923 | // Write out a Rel relocation. | |
924 | ||
925 | template<bool dynamic, int size, bool big_endian> | |
926 | void | |
927 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write( | |
928 | unsigned char* pov) const | |
929 | { | |
930 | elfcpp::Rel_write<size, big_endian> orel(pov); | |
931 | this->write_rel(&orel); | |
932 | } | |
933 | ||
e8c846c3 ILT |
934 | // Get the value of the symbol referred to by a Rel relocation. |
935 | ||
936 | template<bool dynamic, int size, bool big_endian> | |
937 | typename elfcpp::Elf_types<size>::Elf_Addr | |
d1f003c6 | 938 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value( |
624f8810 | 939 | Addend addend) const |
e8c846c3 ILT |
940 | { |
941 | if (this->local_sym_index_ == GSYM_CODE) | |
942 | { | |
943 | const Sized_symbol<size>* sym; | |
944 | sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym); | |
d1f003c6 | 945 | return sym->value() + addend; |
e8c846c3 ILT |
946 | } |
947 | gold_assert(this->local_sym_index_ != SECTION_CODE | |
d1f003c6 ILT |
948 | && this->local_sym_index_ != INVALID_CODE |
949 | && !this->is_section_symbol_); | |
950 | const unsigned int lsi = this->local_sym_index_; | |
951 | const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi); | |
952 | return symval->value(this->u1_.relobj, addend); | |
e8c846c3 ILT |
953 | } |
954 | ||
d98bc257 ILT |
955 | // Reloc comparison. This function sorts the dynamic relocs for the |
956 | // benefit of the dynamic linker. First we sort all relative relocs | |
957 | // to the front. Among relative relocs, we sort by output address. | |
958 | // Among non-relative relocs, we sort by symbol index, then by output | |
959 | // address. | |
960 | ||
961 | template<bool dynamic, int size, bool big_endian> | |
962 | int | |
963 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>:: | |
964 | compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2) | |
965 | const | |
966 | { | |
967 | if (this->is_relative_) | |
968 | { | |
969 | if (!r2.is_relative_) | |
970 | return -1; | |
971 | // Otherwise sort by reloc address below. | |
972 | } | |
973 | else if (r2.is_relative_) | |
974 | return 1; | |
975 | else | |
976 | { | |
977 | unsigned int sym1 = this->get_symbol_index(); | |
978 | unsigned int sym2 = r2.get_symbol_index(); | |
979 | if (sym1 < sym2) | |
980 | return -1; | |
981 | else if (sym1 > sym2) | |
982 | return 1; | |
983 | // Otherwise sort by reloc address. | |
984 | } | |
985 | ||
986 | section_offset_type addr1 = this->get_address(); | |
987 | section_offset_type addr2 = r2.get_address(); | |
988 | if (addr1 < addr2) | |
989 | return -1; | |
990 | else if (addr1 > addr2) | |
991 | return 1; | |
992 | ||
993 | // Final tie breaker, in order to generate the same output on any | |
994 | // host: reloc type. | |
995 | unsigned int type1 = this->type_; | |
996 | unsigned int type2 = r2.type_; | |
997 | if (type1 < type2) | |
998 | return -1; | |
999 | else if (type1 > type2) | |
1000 | return 1; | |
1001 | ||
1002 | // These relocs appear to be exactly the same. | |
1003 | return 0; | |
1004 | } | |
1005 | ||
c06b7b0b ILT |
1006 | // Write out a Rela relocation. |
1007 | ||
1008 | template<bool dynamic, int size, bool big_endian> | |
1009 | void | |
1010 | Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write( | |
1011 | unsigned char* pov) const | |
1012 | { | |
1013 | elfcpp::Rela_write<size, big_endian> orel(pov); | |
1014 | this->rel_.write_rel(&orel); | |
e8c846c3 | 1015 | Addend addend = this->addend_; |
dceae3c1 | 1016 | if (this->rel_.is_relative()) |
d1f003c6 ILT |
1017 | addend = this->rel_.symbol_value(addend); |
1018 | else if (this->rel_.is_local_section_symbol()) | |
624f8810 | 1019 | addend = this->rel_.local_section_offset(addend); |
e8c846c3 | 1020 | orel.put_r_addend(addend); |
c06b7b0b ILT |
1021 | } |
1022 | ||
1023 | // Output_data_reloc_base methods. | |
1024 | ||
16649710 ILT |
1025 | // Adjust the output section. |
1026 | ||
1027 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
1028 | void | |
1029 | Output_data_reloc_base<sh_type, dynamic, size, big_endian> | |
1030 | ::do_adjust_output_section(Output_section* os) | |
1031 | { | |
1032 | if (sh_type == elfcpp::SHT_REL) | |
1033 | os->set_entsize(elfcpp::Elf_sizes<size>::rel_size); | |
1034 | else if (sh_type == elfcpp::SHT_RELA) | |
1035 | os->set_entsize(elfcpp::Elf_sizes<size>::rela_size); | |
1036 | else | |
1037 | gold_unreachable(); | |
1038 | if (dynamic) | |
1039 | os->set_should_link_to_dynsym(); | |
1040 | else | |
1041 | os->set_should_link_to_symtab(); | |
1042 | } | |
1043 | ||
c06b7b0b ILT |
1044 | // Write out relocation data. |
1045 | ||
1046 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
1047 | void | |
1048 | Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write( | |
1049 | Output_file* of) | |
1050 | { | |
1051 | const off_t off = this->offset(); | |
1052 | const off_t oview_size = this->data_size(); | |
1053 | unsigned char* const oview = of->get_output_view(off, oview_size); | |
1054 | ||
d98bc257 ILT |
1055 | if (this->sort_relocs_) |
1056 | { | |
1057 | gold_assert(dynamic); | |
1058 | std::sort(this->relocs_.begin(), this->relocs_.end(), | |
1059 | Sort_relocs_comparison()); | |
1060 | } | |
1061 | ||
c06b7b0b ILT |
1062 | unsigned char* pov = oview; |
1063 | for (typename Relocs::const_iterator p = this->relocs_.begin(); | |
1064 | p != this->relocs_.end(); | |
1065 | ++p) | |
1066 | { | |
1067 | p->write(pov); | |
1068 | pov += reloc_size; | |
1069 | } | |
1070 | ||
a3ad94ed | 1071 | gold_assert(pov - oview == oview_size); |
c06b7b0b ILT |
1072 | |
1073 | of->write_output_view(off, oview_size, oview); | |
1074 | ||
1075 | // We no longer need the relocation entries. | |
1076 | this->relocs_.clear(); | |
1077 | } | |
1078 | ||
6a74a719 ILT |
1079 | // Class Output_relocatable_relocs. |
1080 | ||
1081 | template<int sh_type, int size, bool big_endian> | |
1082 | void | |
1083 | Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size() | |
1084 | { | |
1085 | this->set_data_size(this->rr_->output_reloc_count() | |
1086 | * Reloc_types<sh_type, size, big_endian>::reloc_size); | |
1087 | } | |
1088 | ||
1089 | // class Output_data_group. | |
1090 | ||
1091 | template<int size, bool big_endian> | |
1092 | Output_data_group<size, big_endian>::Output_data_group( | |
1093 | Sized_relobj<size, big_endian>* relobj, | |
1094 | section_size_type entry_count, | |
8825ac63 ILT |
1095 | elfcpp::Elf_Word flags, |
1096 | std::vector<unsigned int>* input_shndxes) | |
20e6d0d6 | 1097 | : Output_section_data(entry_count * 4, 4, false), |
8825ac63 ILT |
1098 | relobj_(relobj), |
1099 | flags_(flags) | |
6a74a719 | 1100 | { |
8825ac63 | 1101 | this->input_shndxes_.swap(*input_shndxes); |
6a74a719 ILT |
1102 | } |
1103 | ||
1104 | // Write out the section group, which means translating the section | |
1105 | // indexes to apply to the output file. | |
1106 | ||
1107 | template<int size, bool big_endian> | |
1108 | void | |
1109 | Output_data_group<size, big_endian>::do_write(Output_file* of) | |
1110 | { | |
1111 | const off_t off = this->offset(); | |
1112 | const section_size_type oview_size = | |
1113 | convert_to_section_size_type(this->data_size()); | |
1114 | unsigned char* const oview = of->get_output_view(off, oview_size); | |
1115 | ||
1116 | elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview); | |
1117 | elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_); | |
1118 | ++contents; | |
1119 | ||
1120 | for (std::vector<unsigned int>::const_iterator p = | |
8825ac63 ILT |
1121 | this->input_shndxes_.begin(); |
1122 | p != this->input_shndxes_.end(); | |
6a74a719 ILT |
1123 | ++p, ++contents) |
1124 | { | |
ef9beddf | 1125 | Output_section* os = this->relobj_->output_section(*p); |
6a74a719 ILT |
1126 | |
1127 | unsigned int output_shndx; | |
1128 | if (os != NULL) | |
1129 | output_shndx = os->out_shndx(); | |
1130 | else | |
1131 | { | |
1132 | this->relobj_->error(_("section group retained but " | |
1133 | "group element discarded")); | |
1134 | output_shndx = 0; | |
1135 | } | |
1136 | ||
1137 | elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx); | |
1138 | } | |
1139 | ||
1140 | size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview; | |
1141 | gold_assert(wrote == oview_size); | |
1142 | ||
1143 | of->write_output_view(off, oview_size, oview); | |
1144 | ||
1145 | // We no longer need this information. | |
8825ac63 | 1146 | this->input_shndxes_.clear(); |
6a74a719 ILT |
1147 | } |
1148 | ||
dbe717ef | 1149 | // Output_data_got::Got_entry methods. |
ead1e424 ILT |
1150 | |
1151 | // Write out the entry. | |
1152 | ||
1153 | template<int size, bool big_endian> | |
1154 | void | |
7e1edb90 | 1155 | Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const |
ead1e424 ILT |
1156 | { |
1157 | Valtype val = 0; | |
1158 | ||
1159 | switch (this->local_sym_index_) | |
1160 | { | |
1161 | case GSYM_CODE: | |
1162 | { | |
e8c846c3 ILT |
1163 | // If the symbol is resolved locally, we need to write out the |
1164 | // link-time value, which will be relocated dynamically by a | |
1165 | // RELATIVE relocation. | |
ead1e424 | 1166 | Symbol* gsym = this->u_.gsym; |
e8c846c3 ILT |
1167 | Sized_symbol<size>* sgsym; |
1168 | // This cast is a bit ugly. We don't want to put a | |
1169 | // virtual method in Symbol, because we want Symbol to be | |
1170 | // as small as possible. | |
1171 | sgsym = static_cast<Sized_symbol<size>*>(gsym); | |
1172 | val = sgsym->value(); | |
ead1e424 ILT |
1173 | } |
1174 | break; | |
1175 | ||
1176 | case CONSTANT_CODE: | |
1177 | val = this->u_.constant; | |
1178 | break; | |
1179 | ||
1180 | default: | |
d1f003c6 ILT |
1181 | { |
1182 | const unsigned int lsi = this->local_sym_index_; | |
1183 | const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi); | |
1184 | val = symval->value(this->u_.object, 0); | |
1185 | } | |
e727fa71 | 1186 | break; |
ead1e424 ILT |
1187 | } |
1188 | ||
a3ad94ed | 1189 | elfcpp::Swap<size, big_endian>::writeval(pov, val); |
ead1e424 ILT |
1190 | } |
1191 | ||
dbe717ef | 1192 | // Output_data_got methods. |
ead1e424 | 1193 | |
dbe717ef ILT |
1194 | // Add an entry for a global symbol to the GOT. This returns true if |
1195 | // this is a new GOT entry, false if the symbol already had a GOT | |
1196 | // entry. | |
1197 | ||
1198 | template<int size, bool big_endian> | |
1199 | bool | |
0a65a3a7 CC |
1200 | Output_data_got<size, big_endian>::add_global( |
1201 | Symbol* gsym, | |
1202 | unsigned int got_type) | |
ead1e424 | 1203 | { |
0a65a3a7 | 1204 | if (gsym->has_got_offset(got_type)) |
dbe717ef | 1205 | return false; |
ead1e424 | 1206 | |
dbe717ef ILT |
1207 | this->entries_.push_back(Got_entry(gsym)); |
1208 | this->set_got_size(); | |
0a65a3a7 | 1209 | gsym->set_got_offset(got_type, this->last_got_offset()); |
dbe717ef ILT |
1210 | return true; |
1211 | } | |
ead1e424 | 1212 | |
7bf1f802 ILT |
1213 | // Add an entry for a global symbol to the GOT, and add a dynamic |
1214 | // relocation of type R_TYPE for the GOT entry. | |
1215 | template<int size, bool big_endian> | |
1216 | void | |
1217 | Output_data_got<size, big_endian>::add_global_with_rel( | |
1218 | Symbol* gsym, | |
0a65a3a7 | 1219 | unsigned int got_type, |
7bf1f802 ILT |
1220 | Rel_dyn* rel_dyn, |
1221 | unsigned int r_type) | |
1222 | { | |
0a65a3a7 | 1223 | if (gsym->has_got_offset(got_type)) |
7bf1f802 ILT |
1224 | return; |
1225 | ||
1226 | this->entries_.push_back(Got_entry()); | |
1227 | this->set_got_size(); | |
1228 | unsigned int got_offset = this->last_got_offset(); | |
0a65a3a7 | 1229 | gsym->set_got_offset(got_type, got_offset); |
7bf1f802 ILT |
1230 | rel_dyn->add_global(gsym, r_type, this, got_offset); |
1231 | } | |
1232 | ||
1233 | template<int size, bool big_endian> | |
1234 | void | |
1235 | Output_data_got<size, big_endian>::add_global_with_rela( | |
1236 | Symbol* gsym, | |
0a65a3a7 | 1237 | unsigned int got_type, |
7bf1f802 ILT |
1238 | Rela_dyn* rela_dyn, |
1239 | unsigned int r_type) | |
1240 | { | |
0a65a3a7 | 1241 | if (gsym->has_got_offset(got_type)) |
7bf1f802 ILT |
1242 | return; |
1243 | ||
1244 | this->entries_.push_back(Got_entry()); | |
1245 | this->set_got_size(); | |
1246 | unsigned int got_offset = this->last_got_offset(); | |
0a65a3a7 | 1247 | gsym->set_got_offset(got_type, got_offset); |
7bf1f802 ILT |
1248 | rela_dyn->add_global(gsym, r_type, this, got_offset, 0); |
1249 | } | |
1250 | ||
0a65a3a7 CC |
1251 | // Add a pair of entries for a global symbol to the GOT, and add |
1252 | // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively. | |
1253 | // If R_TYPE_2 == 0, add the second entry with no relocation. | |
7bf1f802 ILT |
1254 | template<int size, bool big_endian> |
1255 | void | |
0a65a3a7 CC |
1256 | Output_data_got<size, big_endian>::add_global_pair_with_rel( |
1257 | Symbol* gsym, | |
1258 | unsigned int got_type, | |
7bf1f802 | 1259 | Rel_dyn* rel_dyn, |
0a65a3a7 CC |
1260 | unsigned int r_type_1, |
1261 | unsigned int r_type_2) | |
7bf1f802 | 1262 | { |
0a65a3a7 | 1263 | if (gsym->has_got_offset(got_type)) |
7bf1f802 ILT |
1264 | return; |
1265 | ||
1266 | this->entries_.push_back(Got_entry()); | |
7bf1f802 | 1267 | unsigned int got_offset = this->last_got_offset(); |
0a65a3a7 CC |
1268 | gsym->set_got_offset(got_type, got_offset); |
1269 | rel_dyn->add_global(gsym, r_type_1, this, got_offset); | |
1270 | ||
1271 | this->entries_.push_back(Got_entry()); | |
1272 | if (r_type_2 != 0) | |
1273 | { | |
1274 | got_offset = this->last_got_offset(); | |
1275 | rel_dyn->add_global(gsym, r_type_2, this, got_offset); | |
1276 | } | |
1277 | ||
1278 | this->set_got_size(); | |
7bf1f802 ILT |
1279 | } |
1280 | ||
1281 | template<int size, bool big_endian> | |
1282 | void | |
0a65a3a7 CC |
1283 | Output_data_got<size, big_endian>::add_global_pair_with_rela( |
1284 | Symbol* gsym, | |
1285 | unsigned int got_type, | |
7bf1f802 | 1286 | Rela_dyn* rela_dyn, |
0a65a3a7 CC |
1287 | unsigned int r_type_1, |
1288 | unsigned int r_type_2) | |
7bf1f802 | 1289 | { |
0a65a3a7 | 1290 | if (gsym->has_got_offset(got_type)) |
7bf1f802 ILT |
1291 | return; |
1292 | ||
1293 | this->entries_.push_back(Got_entry()); | |
7bf1f802 | 1294 | unsigned int got_offset = this->last_got_offset(); |
0a65a3a7 CC |
1295 | gsym->set_got_offset(got_type, got_offset); |
1296 | rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0); | |
1297 | ||
1298 | this->entries_.push_back(Got_entry()); | |
1299 | if (r_type_2 != 0) | |
1300 | { | |
1301 | got_offset = this->last_got_offset(); | |
1302 | rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0); | |
1303 | } | |
1304 | ||
1305 | this->set_got_size(); | |
7bf1f802 ILT |
1306 | } |
1307 | ||
0a65a3a7 CC |
1308 | // Add an entry for a local symbol to the GOT. This returns true if |
1309 | // this is a new GOT entry, false if the symbol already has a GOT | |
1310 | // entry. | |
07f397ab ILT |
1311 | |
1312 | template<int size, bool big_endian> | |
1313 | bool | |
0a65a3a7 CC |
1314 | Output_data_got<size, big_endian>::add_local( |
1315 | Sized_relobj<size, big_endian>* object, | |
1316 | unsigned int symndx, | |
1317 | unsigned int got_type) | |
07f397ab | 1318 | { |
0a65a3a7 | 1319 | if (object->local_has_got_offset(symndx, got_type)) |
07f397ab ILT |
1320 | return false; |
1321 | ||
0a65a3a7 | 1322 | this->entries_.push_back(Got_entry(object, symndx)); |
07f397ab | 1323 | this->set_got_size(); |
0a65a3a7 | 1324 | object->set_local_got_offset(symndx, got_type, this->last_got_offset()); |
07f397ab ILT |
1325 | return true; |
1326 | } | |
1327 | ||
0a65a3a7 CC |
1328 | // Add an entry for a local symbol to the GOT, and add a dynamic |
1329 | // relocation of type R_TYPE for the GOT entry. | |
7bf1f802 ILT |
1330 | template<int size, bool big_endian> |
1331 | void | |
0a65a3a7 CC |
1332 | Output_data_got<size, big_endian>::add_local_with_rel( |
1333 | Sized_relobj<size, big_endian>* object, | |
1334 | unsigned int symndx, | |
1335 | unsigned int got_type, | |
7bf1f802 ILT |
1336 | Rel_dyn* rel_dyn, |
1337 | unsigned int r_type) | |
1338 | { | |
0a65a3a7 | 1339 | if (object->local_has_got_offset(symndx, got_type)) |
7bf1f802 ILT |
1340 | return; |
1341 | ||
1342 | this->entries_.push_back(Got_entry()); | |
1343 | this->set_got_size(); | |
1344 | unsigned int got_offset = this->last_got_offset(); | |
0a65a3a7 CC |
1345 | object->set_local_got_offset(symndx, got_type, got_offset); |
1346 | rel_dyn->add_local(object, symndx, r_type, this, got_offset); | |
7bf1f802 ILT |
1347 | } |
1348 | ||
1349 | template<int size, bool big_endian> | |
1350 | void | |
0a65a3a7 CC |
1351 | Output_data_got<size, big_endian>::add_local_with_rela( |
1352 | Sized_relobj<size, big_endian>* object, | |
1353 | unsigned int symndx, | |
1354 | unsigned int got_type, | |
7bf1f802 ILT |
1355 | Rela_dyn* rela_dyn, |
1356 | unsigned int r_type) | |
1357 | { | |
0a65a3a7 | 1358 | if (object->local_has_got_offset(symndx, got_type)) |
7bf1f802 ILT |
1359 | return; |
1360 | ||
1361 | this->entries_.push_back(Got_entry()); | |
1362 | this->set_got_size(); | |
1363 | unsigned int got_offset = this->last_got_offset(); | |
0a65a3a7 CC |
1364 | object->set_local_got_offset(symndx, got_type, got_offset); |
1365 | rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0); | |
07f397ab ILT |
1366 | } |
1367 | ||
0a65a3a7 CC |
1368 | // Add a pair of entries for a local symbol to the GOT, and add |
1369 | // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively. | |
1370 | // If R_TYPE_2 == 0, add the second entry with no relocation. | |
7bf1f802 ILT |
1371 | template<int size, bool big_endian> |
1372 | void | |
0a65a3a7 | 1373 | Output_data_got<size, big_endian>::add_local_pair_with_rel( |
7bf1f802 ILT |
1374 | Sized_relobj<size, big_endian>* object, |
1375 | unsigned int symndx, | |
1376 | unsigned int shndx, | |
0a65a3a7 | 1377 | unsigned int got_type, |
7bf1f802 | 1378 | Rel_dyn* rel_dyn, |
0a65a3a7 CC |
1379 | unsigned int r_type_1, |
1380 | unsigned int r_type_2) | |
7bf1f802 | 1381 | { |
0a65a3a7 | 1382 | if (object->local_has_got_offset(symndx, got_type)) |
7bf1f802 ILT |
1383 | return; |
1384 | ||
1385 | this->entries_.push_back(Got_entry()); | |
1386 | unsigned int got_offset = this->last_got_offset(); | |
0a65a3a7 | 1387 | object->set_local_got_offset(symndx, got_type, got_offset); |
ef9beddf | 1388 | Output_section* os = object->output_section(shndx); |
0a65a3a7 | 1389 | rel_dyn->add_output_section(os, r_type_1, this, got_offset); |
7bf1f802 | 1390 | |
0a65a3a7 CC |
1391 | this->entries_.push_back(Got_entry(object, symndx)); |
1392 | if (r_type_2 != 0) | |
1393 | { | |
1394 | got_offset = this->last_got_offset(); | |
1395 | rel_dyn->add_output_section(os, r_type_2, this, got_offset); | |
1396 | } | |
7bf1f802 ILT |
1397 | |
1398 | this->set_got_size(); | |
1399 | } | |
1400 | ||
1401 | template<int size, bool big_endian> | |
1402 | void | |
0a65a3a7 | 1403 | Output_data_got<size, big_endian>::add_local_pair_with_rela( |
7bf1f802 ILT |
1404 | Sized_relobj<size, big_endian>* object, |
1405 | unsigned int symndx, | |
1406 | unsigned int shndx, | |
0a65a3a7 | 1407 | unsigned int got_type, |
7bf1f802 | 1408 | Rela_dyn* rela_dyn, |
0a65a3a7 CC |
1409 | unsigned int r_type_1, |
1410 | unsigned int r_type_2) | |
7bf1f802 | 1411 | { |
0a65a3a7 | 1412 | if (object->local_has_got_offset(symndx, got_type)) |
7bf1f802 ILT |
1413 | return; |
1414 | ||
1415 | this->entries_.push_back(Got_entry()); | |
1416 | unsigned int got_offset = this->last_got_offset(); | |
0a65a3a7 | 1417 | object->set_local_got_offset(symndx, got_type, got_offset); |
ef9beddf | 1418 | Output_section* os = object->output_section(shndx); |
0a65a3a7 | 1419 | rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0); |
7bf1f802 | 1420 | |
0a65a3a7 CC |
1421 | this->entries_.push_back(Got_entry(object, symndx)); |
1422 | if (r_type_2 != 0) | |
1423 | { | |
1424 | got_offset = this->last_got_offset(); | |
1425 | rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0); | |
1426 | } | |
7bf1f802 ILT |
1427 | |
1428 | this->set_got_size(); | |
1429 | } | |
1430 | ||
ead1e424 ILT |
1431 | // Write out the GOT. |
1432 | ||
1433 | template<int size, bool big_endian> | |
1434 | void | |
dbe717ef | 1435 | Output_data_got<size, big_endian>::do_write(Output_file* of) |
ead1e424 ILT |
1436 | { |
1437 | const int add = size / 8; | |
1438 | ||
1439 | const off_t off = this->offset(); | |
c06b7b0b | 1440 | const off_t oview_size = this->data_size(); |
ead1e424 ILT |
1441 | unsigned char* const oview = of->get_output_view(off, oview_size); |
1442 | ||
1443 | unsigned char* pov = oview; | |
1444 | for (typename Got_entries::const_iterator p = this->entries_.begin(); | |
1445 | p != this->entries_.end(); | |
1446 | ++p) | |
1447 | { | |
7e1edb90 | 1448 | p->write(pov); |
ead1e424 ILT |
1449 | pov += add; |
1450 | } | |
1451 | ||
a3ad94ed | 1452 | gold_assert(pov - oview == oview_size); |
c06b7b0b | 1453 | |
ead1e424 ILT |
1454 | of->write_output_view(off, oview_size, oview); |
1455 | ||
1456 | // We no longer need the GOT entries. | |
1457 | this->entries_.clear(); | |
1458 | } | |
1459 | ||
a3ad94ed ILT |
1460 | // Output_data_dynamic::Dynamic_entry methods. |
1461 | ||
1462 | // Write out the entry. | |
1463 | ||
1464 | template<int size, bool big_endian> | |
1465 | void | |
1466 | Output_data_dynamic::Dynamic_entry::write( | |
1467 | unsigned char* pov, | |
7d1a9ebb | 1468 | const Stringpool* pool) const |
a3ad94ed ILT |
1469 | { |
1470 | typename elfcpp::Elf_types<size>::Elf_WXword val; | |
c2b45e22 | 1471 | switch (this->offset_) |
a3ad94ed ILT |
1472 | { |
1473 | case DYNAMIC_NUMBER: | |
1474 | val = this->u_.val; | |
1475 | break; | |
1476 | ||
a3ad94ed | 1477 | case DYNAMIC_SECTION_SIZE: |
16649710 | 1478 | val = this->u_.od->data_size(); |
a3ad94ed ILT |
1479 | break; |
1480 | ||
1481 | case DYNAMIC_SYMBOL: | |
1482 | { | |
16649710 ILT |
1483 | const Sized_symbol<size>* s = |
1484 | static_cast<const Sized_symbol<size>*>(this->u_.sym); | |
a3ad94ed ILT |
1485 | val = s->value(); |
1486 | } | |
1487 | break; | |
1488 | ||
1489 | case DYNAMIC_STRING: | |
1490 | val = pool->get_offset(this->u_.str); | |
1491 | break; | |
1492 | ||
1493 | default: | |
c2b45e22 CC |
1494 | val = this->u_.od->address() + this->offset_; |
1495 | break; | |
a3ad94ed ILT |
1496 | } |
1497 | ||
1498 | elfcpp::Dyn_write<size, big_endian> dw(pov); | |
1499 | dw.put_d_tag(this->tag_); | |
1500 | dw.put_d_val(val); | |
1501 | } | |
1502 | ||
1503 | // Output_data_dynamic methods. | |
1504 | ||
16649710 ILT |
1505 | // Adjust the output section to set the entry size. |
1506 | ||
1507 | void | |
1508 | Output_data_dynamic::do_adjust_output_section(Output_section* os) | |
1509 | { | |
8851ecca | 1510 | if (parameters->target().get_size() == 32) |
16649710 | 1511 | os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size); |
8851ecca | 1512 | else if (parameters->target().get_size() == 64) |
16649710 ILT |
1513 | os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size); |
1514 | else | |
1515 | gold_unreachable(); | |
1516 | } | |
1517 | ||
a3ad94ed ILT |
1518 | // Set the final data size. |
1519 | ||
1520 | void | |
27bc2bce | 1521 | Output_data_dynamic::set_final_data_size() |
a3ad94ed | 1522 | { |
20e6d0d6 DK |
1523 | // Add the terminating entry if it hasn't been added. |
1524 | // Because of relaxation, we can run this multiple times. | |
1525 | if (this->entries_.empty() | |
1526 | || this->entries_.rbegin()->tag() != elfcpp::DT_NULL) | |
1527 | this->add_constant(elfcpp::DT_NULL, 0); | |
a3ad94ed ILT |
1528 | |
1529 | int dyn_size; | |
8851ecca | 1530 | if (parameters->target().get_size() == 32) |
a3ad94ed | 1531 | dyn_size = elfcpp::Elf_sizes<32>::dyn_size; |
8851ecca | 1532 | else if (parameters->target().get_size() == 64) |
a3ad94ed ILT |
1533 | dyn_size = elfcpp::Elf_sizes<64>::dyn_size; |
1534 | else | |
1535 | gold_unreachable(); | |
1536 | this->set_data_size(this->entries_.size() * dyn_size); | |
1537 | } | |
1538 | ||
1539 | // Write out the dynamic entries. | |
1540 | ||
1541 | void | |
1542 | Output_data_dynamic::do_write(Output_file* of) | |
1543 | { | |
8851ecca | 1544 | switch (parameters->size_and_endianness()) |
a3ad94ed | 1545 | { |
9025d29d | 1546 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
1547 | case Parameters::TARGET_32_LITTLE: |
1548 | this->sized_write<32, false>(of); | |
1549 | break; | |
9025d29d | 1550 | #endif |
8851ecca ILT |
1551 | #ifdef HAVE_TARGET_32_BIG |
1552 | case Parameters::TARGET_32_BIG: | |
1553 | this->sized_write<32, true>(of); | |
1554 | break; | |
9025d29d | 1555 | #endif |
9025d29d | 1556 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
1557 | case Parameters::TARGET_64_LITTLE: |
1558 | this->sized_write<64, false>(of); | |
1559 | break; | |
9025d29d | 1560 | #endif |
8851ecca ILT |
1561 | #ifdef HAVE_TARGET_64_BIG |
1562 | case Parameters::TARGET_64_BIG: | |
1563 | this->sized_write<64, true>(of); | |
1564 | break; | |
1565 | #endif | |
1566 | default: | |
1567 | gold_unreachable(); | |
a3ad94ed | 1568 | } |
a3ad94ed ILT |
1569 | } |
1570 | ||
1571 | template<int size, bool big_endian> | |
1572 | void | |
1573 | Output_data_dynamic::sized_write(Output_file* of) | |
1574 | { | |
1575 | const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size; | |
1576 | ||
1577 | const off_t offset = this->offset(); | |
1578 | const off_t oview_size = this->data_size(); | |
1579 | unsigned char* const oview = of->get_output_view(offset, oview_size); | |
1580 | ||
1581 | unsigned char* pov = oview; | |
1582 | for (typename Dynamic_entries::const_iterator p = this->entries_.begin(); | |
1583 | p != this->entries_.end(); | |
1584 | ++p) | |
1585 | { | |
7d1a9ebb | 1586 | p->write<size, big_endian>(pov, this->pool_); |
a3ad94ed ILT |
1587 | pov += dyn_size; |
1588 | } | |
1589 | ||
1590 | gold_assert(pov - oview == oview_size); | |
1591 | ||
1592 | of->write_output_view(offset, oview_size, oview); | |
1593 | ||
1594 | // We no longer need the dynamic entries. | |
1595 | this->entries_.clear(); | |
1596 | } | |
1597 | ||
d491d34e ILT |
1598 | // Class Output_symtab_xindex. |
1599 | ||
1600 | void | |
1601 | Output_symtab_xindex::do_write(Output_file* of) | |
1602 | { | |
1603 | const off_t offset = this->offset(); | |
1604 | const off_t oview_size = this->data_size(); | |
1605 | unsigned char* const oview = of->get_output_view(offset, oview_size); | |
1606 | ||
1607 | memset(oview, 0, oview_size); | |
1608 | ||
1609 | if (parameters->target().is_big_endian()) | |
1610 | this->endian_do_write<true>(oview); | |
1611 | else | |
1612 | this->endian_do_write<false>(oview); | |
1613 | ||
1614 | of->write_output_view(offset, oview_size, oview); | |
1615 | ||
1616 | // We no longer need the data. | |
1617 | this->entries_.clear(); | |
1618 | } | |
1619 | ||
1620 | template<bool big_endian> | |
1621 | void | |
1622 | Output_symtab_xindex::endian_do_write(unsigned char* const oview) | |
1623 | { | |
1624 | for (Xindex_entries::const_iterator p = this->entries_.begin(); | |
1625 | p != this->entries_.end(); | |
1626 | ++p) | |
20e6d0d6 DK |
1627 | { |
1628 | unsigned int symndx = p->first; | |
1629 | gold_assert(symndx * 4 < this->data_size()); | |
1630 | elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second); | |
1631 | } | |
d491d34e ILT |
1632 | } |
1633 | ||
ead1e424 ILT |
1634 | // Output_section::Input_section methods. |
1635 | ||
1636 | // Return the data size. For an input section we store the size here. | |
1637 | // For an Output_section_data, we have to ask it for the size. | |
1638 | ||
1639 | off_t | |
1640 | Output_section::Input_section::data_size() const | |
1641 | { | |
1642 | if (this->is_input_section()) | |
b8e6aad9 | 1643 | return this->u1_.data_size; |
ead1e424 | 1644 | else |
b8e6aad9 | 1645 | return this->u2_.posd->data_size(); |
ead1e424 ILT |
1646 | } |
1647 | ||
1648 | // Set the address and file offset. | |
1649 | ||
1650 | void | |
96803768 ILT |
1651 | Output_section::Input_section::set_address_and_file_offset( |
1652 | uint64_t address, | |
1653 | off_t file_offset, | |
1654 | off_t section_file_offset) | |
ead1e424 ILT |
1655 | { |
1656 | if (this->is_input_section()) | |
96803768 ILT |
1657 | this->u2_.object->set_section_offset(this->shndx_, |
1658 | file_offset - section_file_offset); | |
ead1e424 | 1659 | else |
96803768 ILT |
1660 | this->u2_.posd->set_address_and_file_offset(address, file_offset); |
1661 | } | |
1662 | ||
a445fddf ILT |
1663 | // Reset the address and file offset. |
1664 | ||
1665 | void | |
1666 | Output_section::Input_section::reset_address_and_file_offset() | |
1667 | { | |
1668 | if (!this->is_input_section()) | |
1669 | this->u2_.posd->reset_address_and_file_offset(); | |
1670 | } | |
1671 | ||
96803768 ILT |
1672 | // Finalize the data size. |
1673 | ||
1674 | void | |
1675 | Output_section::Input_section::finalize_data_size() | |
1676 | { | |
1677 | if (!this->is_input_section()) | |
1678 | this->u2_.posd->finalize_data_size(); | |
b8e6aad9 ILT |
1679 | } |
1680 | ||
1e983657 ILT |
1681 | // Try to turn an input offset into an output offset. We want to |
1682 | // return the output offset relative to the start of this | |
1683 | // Input_section in the output section. | |
b8e6aad9 | 1684 | |
8f00aeb8 | 1685 | inline bool |
8383303e ILT |
1686 | Output_section::Input_section::output_offset( |
1687 | const Relobj* object, | |
1688 | unsigned int shndx, | |
1689 | section_offset_type offset, | |
1690 | section_offset_type *poutput) const | |
b8e6aad9 ILT |
1691 | { |
1692 | if (!this->is_input_section()) | |
730cdc88 | 1693 | return this->u2_.posd->output_offset(object, shndx, offset, poutput); |
b8e6aad9 ILT |
1694 | else |
1695 | { | |
730cdc88 | 1696 | if (this->shndx_ != shndx || this->u2_.object != object) |
b8e6aad9 | 1697 | return false; |
1e983657 | 1698 | *poutput = offset; |
b8e6aad9 ILT |
1699 | return true; |
1700 | } | |
ead1e424 ILT |
1701 | } |
1702 | ||
a9a60db6 ILT |
1703 | // Return whether this is the merge section for the input section |
1704 | // SHNDX in OBJECT. | |
1705 | ||
1706 | inline bool | |
1707 | Output_section::Input_section::is_merge_section_for(const Relobj* object, | |
1708 | unsigned int shndx) const | |
1709 | { | |
1710 | if (this->is_input_section()) | |
1711 | return false; | |
1712 | return this->u2_.posd->is_merge_section_for(object, shndx); | |
1713 | } | |
1714 | ||
ead1e424 ILT |
1715 | // Write out the data. We don't have to do anything for an input |
1716 | // section--they are handled via Object::relocate--but this is where | |
1717 | // we write out the data for an Output_section_data. | |
1718 | ||
1719 | void | |
1720 | Output_section::Input_section::write(Output_file* of) | |
1721 | { | |
1722 | if (!this->is_input_section()) | |
b8e6aad9 | 1723 | this->u2_.posd->write(of); |
ead1e424 ILT |
1724 | } |
1725 | ||
96803768 ILT |
1726 | // Write the data to a buffer. As for write(), we don't have to do |
1727 | // anything for an input section. | |
1728 | ||
1729 | void | |
1730 | Output_section::Input_section::write_to_buffer(unsigned char* buffer) | |
1731 | { | |
1732 | if (!this->is_input_section()) | |
1733 | this->u2_.posd->write_to_buffer(buffer); | |
1734 | } | |
1735 | ||
7d9e3d98 ILT |
1736 | // Print to a map file. |
1737 | ||
1738 | void | |
1739 | Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const | |
1740 | { | |
1741 | switch (this->shndx_) | |
1742 | { | |
1743 | case OUTPUT_SECTION_CODE: | |
1744 | case MERGE_DATA_SECTION_CODE: | |
1745 | case MERGE_STRING_SECTION_CODE: | |
1746 | this->u2_.posd->print_to_mapfile(mapfile); | |
1747 | break; | |
1748 | ||
20e6d0d6 DK |
1749 | case RELAXED_INPUT_SECTION_CODE: |
1750 | { | |
1751 | Output_relaxed_input_section* relaxed_section = | |
1752 | this->relaxed_input_section(); | |
1753 | mapfile->print_input_section(relaxed_section->relobj(), | |
1754 | relaxed_section->shndx()); | |
1755 | } | |
1756 | break; | |
7d9e3d98 ILT |
1757 | default: |
1758 | mapfile->print_input_section(this->u2_.object, this->shndx_); | |
1759 | break; | |
1760 | } | |
1761 | } | |
1762 | ||
a2fb1b05 ILT |
1763 | // Output_section methods. |
1764 | ||
1765 | // Construct an Output_section. NAME will point into a Stringpool. | |
1766 | ||
96803768 | 1767 | Output_section::Output_section(const char* name, elfcpp::Elf_Word type, |
b8e6aad9 | 1768 | elfcpp::Elf_Xword flags) |
96803768 | 1769 | : name_(name), |
a2fb1b05 ILT |
1770 | addralign_(0), |
1771 | entsize_(0), | |
a445fddf | 1772 | load_address_(0), |
16649710 | 1773 | link_section_(NULL), |
a2fb1b05 | 1774 | link_(0), |
16649710 | 1775 | info_section_(NULL), |
6a74a719 | 1776 | info_symndx_(NULL), |
a2fb1b05 ILT |
1777 | info_(0), |
1778 | type_(type), | |
61ba1cf9 | 1779 | flags_(flags), |
91ea499d | 1780 | out_shndx_(-1U), |
c06b7b0b ILT |
1781 | symtab_index_(0), |
1782 | dynsym_index_(0), | |
ead1e424 ILT |
1783 | input_sections_(), |
1784 | first_input_offset_(0), | |
c51e6221 | 1785 | fills_(), |
96803768 | 1786 | postprocessing_buffer_(NULL), |
a3ad94ed | 1787 | needs_symtab_index_(false), |
16649710 ILT |
1788 | needs_dynsym_index_(false), |
1789 | should_link_to_symtab_(false), | |
730cdc88 | 1790 | should_link_to_dynsym_(false), |
27bc2bce | 1791 | after_input_sections_(false), |
7bf1f802 | 1792 | requires_postprocessing_(false), |
a445fddf ILT |
1793 | found_in_sections_clause_(false), |
1794 | has_load_address_(false), | |
755ab8af | 1795 | info_uses_section_index_(false), |
2fd32231 ILT |
1796 | may_sort_attached_input_sections_(false), |
1797 | must_sort_attached_input_sections_(false), | |
1798 | attached_input_sections_are_sorted_(false), | |
9f1d377b ILT |
1799 | is_relro_(false), |
1800 | is_relro_local_(false), | |
8a5e3e08 ILT |
1801 | is_small_section_(false), |
1802 | is_large_section_(false), | |
20e6d0d6 DK |
1803 | tls_offset_(0), |
1804 | checkpoint_(NULL) | |
a2fb1b05 | 1805 | { |
27bc2bce ILT |
1806 | // An unallocated section has no address. Forcing this means that |
1807 | // we don't need special treatment for symbols defined in debug | |
1808 | // sections. | |
1809 | if ((flags & elfcpp::SHF_ALLOC) == 0) | |
1810 | this->set_address(0); | |
a2fb1b05 ILT |
1811 | } |
1812 | ||
54dc6425 ILT |
1813 | Output_section::~Output_section() |
1814 | { | |
20e6d0d6 | 1815 | delete this->checkpoint_; |
54dc6425 ILT |
1816 | } |
1817 | ||
16649710 ILT |
1818 | // Set the entry size. |
1819 | ||
1820 | void | |
1821 | Output_section::set_entsize(uint64_t v) | |
1822 | { | |
1823 | if (this->entsize_ == 0) | |
1824 | this->entsize_ = v; | |
1825 | else | |
1826 | gold_assert(this->entsize_ == v); | |
1827 | } | |
1828 | ||
ead1e424 | 1829 | // Add the input section SHNDX, with header SHDR, named SECNAME, in |
730cdc88 ILT |
1830 | // OBJECT, to the Output_section. RELOC_SHNDX is the index of a |
1831 | // relocation section which applies to this section, or 0 if none, or | |
1832 | // -1U if more than one. Return the offset of the input section | |
1833 | // within the output section. Return -1 if the input section will | |
1834 | // receive special handling. In the normal case we don't always keep | |
1835 | // track of input sections for an Output_section. Instead, each | |
1836 | // Object keeps track of the Output_section for each of its input | |
a445fddf ILT |
1837 | // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep |
1838 | // track of input sections here; this is used when SECTIONS appears in | |
1839 | // a linker script. | |
a2fb1b05 ILT |
1840 | |
1841 | template<int size, bool big_endian> | |
1842 | off_t | |
730cdc88 ILT |
1843 | Output_section::add_input_section(Sized_relobj<size, big_endian>* object, |
1844 | unsigned int shndx, | |
ead1e424 | 1845 | const char* secname, |
730cdc88 | 1846 | const elfcpp::Shdr<size, big_endian>& shdr, |
a445fddf ILT |
1847 | unsigned int reloc_shndx, |
1848 | bool have_sections_script) | |
a2fb1b05 ILT |
1849 | { |
1850 | elfcpp::Elf_Xword addralign = shdr.get_sh_addralign(); | |
1851 | if ((addralign & (addralign - 1)) != 0) | |
1852 | { | |
75f2446e ILT |
1853 | object->error(_("invalid alignment %lu for section \"%s\""), |
1854 | static_cast<unsigned long>(addralign), secname); | |
1855 | addralign = 1; | |
a2fb1b05 | 1856 | } |
a2fb1b05 ILT |
1857 | |
1858 | if (addralign > this->addralign_) | |
1859 | this->addralign_ = addralign; | |
1860 | ||
44a43cf9 | 1861 | typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags(); |
154e0e9a | 1862 | this->update_flags_for_input_section(sh_flags); |
a445fddf | 1863 | |
4f833eee | 1864 | uint64_t entsize = shdr.get_sh_entsize(); |
44a43cf9 ILT |
1865 | |
1866 | // .debug_str is a mergeable string section, but is not always so | |
1867 | // marked by compilers. Mark manually here so we can optimize. | |
1868 | if (strcmp(secname, ".debug_str") == 0) | |
4f833eee ILT |
1869 | { |
1870 | sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS); | |
1871 | entsize = 1; | |
1872 | } | |
44a43cf9 | 1873 | |
b8e6aad9 | 1874 | // If this is a SHF_MERGE section, we pass all the input sections to |
730cdc88 | 1875 | // a Output_data_merge. We don't try to handle relocations for such |
e0b64032 ILT |
1876 | // a section. We don't try to handle empty merge sections--they |
1877 | // mess up the mappings, and are useless anyhow. | |
44a43cf9 | 1878 | if ((sh_flags & elfcpp::SHF_MERGE) != 0 |
e0b64032 ILT |
1879 | && reloc_shndx == 0 |
1880 | && shdr.get_sh_size() > 0) | |
b8e6aad9 | 1881 | { |
44a43cf9 | 1882 | if (this->add_merge_input_section(object, shndx, sh_flags, |
96803768 | 1883 | entsize, addralign)) |
b8e6aad9 ILT |
1884 | { |
1885 | // Tell the relocation routines that they need to call the | |
730cdc88 | 1886 | // output_offset method to determine the final address. |
b8e6aad9 ILT |
1887 | return -1; |
1888 | } | |
1889 | } | |
1890 | ||
27bc2bce | 1891 | off_t offset_in_section = this->current_data_size_for_child(); |
c51e6221 ILT |
1892 | off_t aligned_offset_in_section = align_address(offset_in_section, |
1893 | addralign); | |
1894 | ||
1895 | if (aligned_offset_in_section > offset_in_section | |
a445fddf | 1896 | && !have_sections_script |
44a43cf9 | 1897 | && (sh_flags & elfcpp::SHF_EXECINSTR) != 0 |
029ba973 | 1898 | && parameters->target().has_code_fill()) |
c51e6221 ILT |
1899 | { |
1900 | // We need to add some fill data. Using fill_list_ when | |
1901 | // possible is an optimization, since we will often have fill | |
1902 | // sections without input sections. | |
1903 | off_t fill_len = aligned_offset_in_section - offset_in_section; | |
1904 | if (this->input_sections_.empty()) | |
1905 | this->fills_.push_back(Fill(offset_in_section, fill_len)); | |
1906 | else | |
1907 | { | |
1908 | // FIXME: When relaxing, the size needs to adjust to | |
1909 | // maintain a constant alignment. | |
029ba973 | 1910 | std::string fill_data(parameters->target().code_fill(fill_len)); |
c51e6221 ILT |
1911 | Output_data_const* odc = new Output_data_const(fill_data, 1); |
1912 | this->input_sections_.push_back(Input_section(odc)); | |
1913 | } | |
1914 | } | |
1915 | ||
27bc2bce ILT |
1916 | this->set_current_data_size_for_child(aligned_offset_in_section |
1917 | + shdr.get_sh_size()); | |
a2fb1b05 | 1918 | |
ead1e424 | 1919 | // We need to keep track of this section if we are already keeping |
2fd32231 ILT |
1920 | // track of sections, or if we are relaxing. Also, if this is a |
1921 | // section which requires sorting, or which may require sorting in | |
20e6d0d6 | 1922 | // the future, we keep track of the sections. |
2fd32231 ILT |
1923 | if (have_sections_script |
1924 | || !this->input_sections_.empty() | |
1925 | || this->may_sort_attached_input_sections() | |
7d9e3d98 | 1926 | || this->must_sort_attached_input_sections() |
20e6d0d6 | 1927 | || parameters->options().user_set_Map() |
029ba973 | 1928 | || parameters->target().may_relax()) |
ead1e424 ILT |
1929 | this->input_sections_.push_back(Input_section(object, shndx, |
1930 | shdr.get_sh_size(), | |
1931 | addralign)); | |
54dc6425 | 1932 | |
c51e6221 | 1933 | return aligned_offset_in_section; |
61ba1cf9 ILT |
1934 | } |
1935 | ||
ead1e424 ILT |
1936 | // Add arbitrary data to an output section. |
1937 | ||
1938 | void | |
1939 | Output_section::add_output_section_data(Output_section_data* posd) | |
1940 | { | |
b8e6aad9 ILT |
1941 | Input_section inp(posd); |
1942 | this->add_output_section_data(&inp); | |
a445fddf ILT |
1943 | |
1944 | if (posd->is_data_size_valid()) | |
1945 | { | |
1946 | off_t offset_in_section = this->current_data_size_for_child(); | |
1947 | off_t aligned_offset_in_section = align_address(offset_in_section, | |
1948 | posd->addralign()); | |
1949 | this->set_current_data_size_for_child(aligned_offset_in_section | |
1950 | + posd->data_size()); | |
1951 | } | |
b8e6aad9 ILT |
1952 | } |
1953 | ||
1954 | // Add arbitrary data to an output section by Input_section. | |
c06b7b0b | 1955 | |
b8e6aad9 ILT |
1956 | void |
1957 | Output_section::add_output_section_data(Input_section* inp) | |
1958 | { | |
ead1e424 | 1959 | if (this->input_sections_.empty()) |
27bc2bce | 1960 | this->first_input_offset_ = this->current_data_size_for_child(); |
c06b7b0b | 1961 | |
b8e6aad9 | 1962 | this->input_sections_.push_back(*inp); |
c06b7b0b | 1963 | |
b8e6aad9 | 1964 | uint64_t addralign = inp->addralign(); |
ead1e424 ILT |
1965 | if (addralign > this->addralign_) |
1966 | this->addralign_ = addralign; | |
c06b7b0b | 1967 | |
b8e6aad9 ILT |
1968 | inp->set_output_section(this); |
1969 | } | |
1970 | ||
1971 | // Add a merge section to an output section. | |
1972 | ||
1973 | void | |
1974 | Output_section::add_output_merge_section(Output_section_data* posd, | |
1975 | bool is_string, uint64_t entsize) | |
1976 | { | |
1977 | Input_section inp(posd, is_string, entsize); | |
1978 | this->add_output_section_data(&inp); | |
1979 | } | |
1980 | ||
1981 | // Add an input section to a SHF_MERGE section. | |
1982 | ||
1983 | bool | |
1984 | Output_section::add_merge_input_section(Relobj* object, unsigned int shndx, | |
1985 | uint64_t flags, uint64_t entsize, | |
96803768 | 1986 | uint64_t addralign) |
b8e6aad9 | 1987 | { |
87f95776 ILT |
1988 | bool is_string = (flags & elfcpp::SHF_STRINGS) != 0; |
1989 | ||
1990 | // We only merge strings if the alignment is not more than the | |
1991 | // character size. This could be handled, but it's unusual. | |
1992 | if (is_string && addralign > entsize) | |
b8e6aad9 ILT |
1993 | return false; |
1994 | ||
20e6d0d6 DK |
1995 | // We cannot restore merged input section states. |
1996 | gold_assert(this->checkpoint_ == NULL); | |
1997 | ||
b8e6aad9 ILT |
1998 | Input_section_list::iterator p; |
1999 | for (p = this->input_sections_.begin(); | |
2000 | p != this->input_sections_.end(); | |
2001 | ++p) | |
87f95776 | 2002 | if (p->is_merge_section(is_string, entsize, addralign)) |
9a0910c3 ILT |
2003 | { |
2004 | p->add_input_section(object, shndx); | |
2005 | return true; | |
2006 | } | |
b8e6aad9 ILT |
2007 | |
2008 | // We handle the actual constant merging in Output_merge_data or | |
2009 | // Output_merge_string_data. | |
9a0910c3 ILT |
2010 | Output_section_data* posd; |
2011 | if (!is_string) | |
2012 | posd = new Output_merge_data(entsize, addralign); | |
b8e6aad9 ILT |
2013 | else |
2014 | { | |
9a0910c3 ILT |
2015 | switch (entsize) |
2016 | { | |
2017 | case 1: | |
2018 | posd = new Output_merge_string<char>(addralign); | |
2019 | break; | |
2020 | case 2: | |
2021 | posd = new Output_merge_string<uint16_t>(addralign); | |
2022 | break; | |
2023 | case 4: | |
2024 | posd = new Output_merge_string<uint32_t>(addralign); | |
2025 | break; | |
2026 | default: | |
2027 | return false; | |
2028 | } | |
b8e6aad9 ILT |
2029 | } |
2030 | ||
9a0910c3 ILT |
2031 | this->add_output_merge_section(posd, is_string, entsize); |
2032 | posd->add_input_section(object, shndx); | |
2033 | ||
b8e6aad9 ILT |
2034 | return true; |
2035 | } | |
2036 | ||
20e6d0d6 DK |
2037 | // Relax an existing input section. |
2038 | void | |
2039 | Output_section::relax_input_section(Output_relaxed_input_section *psection) | |
2040 | { | |
2041 | Relobj* relobj = psection->relobj(); | |
2042 | unsigned int shndx = psection->shndx(); | |
2043 | ||
029ba973 | 2044 | gold_assert(parameters->target().may_relax()); |
20e6d0d6 DK |
2045 | |
2046 | // This is not very efficient if we a going to relax a number of sections | |
2047 | // in an Output_section with lot of Input_sections. | |
2048 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
2049 | p != this->input_sections_.end(); | |
2050 | ++p) | |
2051 | { | |
2052 | if (p->is_input_section()) | |
2053 | { | |
2054 | if (p->relobj() == relobj && p->shndx() == shndx) | |
2055 | { | |
2056 | gold_assert(p->addralign() == psection->addralign()); | |
2057 | *p = Input_section(psection); | |
2058 | return; | |
2059 | } | |
2060 | } | |
2061 | else if (p->is_relaxed_input_section()) | |
2062 | gold_assert(p->relobj() != relobj || p->shndx() != shndx); | |
2063 | ||
2064 | } | |
2065 | } | |
2066 | ||
9c547ec3 ILT |
2067 | // Update the output section flags based on input section flags. |
2068 | ||
2069 | void | |
2070 | Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags) | |
2071 | { | |
2072 | // If we created the section with SHF_ALLOC clear, we set the | |
2073 | // address. If we are now setting the SHF_ALLOC flag, we need to | |
2074 | // undo that. | |
2075 | if ((this->flags_ & elfcpp::SHF_ALLOC) == 0 | |
2076 | && (flags & elfcpp::SHF_ALLOC) != 0) | |
2077 | this->mark_address_invalid(); | |
2078 | ||
2079 | this->flags_ |= (flags | |
2080 | & (elfcpp::SHF_WRITE | |
2081 | | elfcpp::SHF_ALLOC | |
2082 | | elfcpp::SHF_EXECINSTR)); | |
2083 | } | |
2084 | ||
730cdc88 ILT |
2085 | // Given an address OFFSET relative to the start of input section |
2086 | // SHNDX in OBJECT, return whether this address is being included in | |
2087 | // the final link. This should only be called if SHNDX in OBJECT has | |
2088 | // a special mapping. | |
2089 | ||
2090 | bool | |
2091 | Output_section::is_input_address_mapped(const Relobj* object, | |
2092 | unsigned int shndx, | |
2093 | off_t offset) const | |
2094 | { | |
730cdc88 ILT |
2095 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); |
2096 | p != this->input_sections_.end(); | |
2097 | ++p) | |
2098 | { | |
8383303e | 2099 | section_offset_type output_offset; |
730cdc88 ILT |
2100 | if (p->output_offset(object, shndx, offset, &output_offset)) |
2101 | return output_offset != -1; | |
2102 | } | |
2103 | ||
2104 | // By default we assume that the address is mapped. This should | |
2105 | // only be called after we have passed all sections to Layout. At | |
2106 | // that point we should know what we are discarding. | |
2107 | return true; | |
2108 | } | |
2109 | ||
2110 | // Given an address OFFSET relative to the start of input section | |
2111 | // SHNDX in object OBJECT, return the output offset relative to the | |
1e983657 ILT |
2112 | // start of the input section in the output section. This should only |
2113 | // be called if SHNDX in OBJECT has a special mapping. | |
730cdc88 | 2114 | |
8383303e | 2115 | section_offset_type |
730cdc88 | 2116 | Output_section::output_offset(const Relobj* object, unsigned int shndx, |
8383303e | 2117 | section_offset_type offset) const |
730cdc88 | 2118 | { |
730cdc88 ILT |
2119 | // This can only be called meaningfully when layout is complete. |
2120 | gold_assert(Output_data::is_layout_complete()); | |
2121 | ||
2122 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); | |
2123 | p != this->input_sections_.end(); | |
2124 | ++p) | |
2125 | { | |
8383303e | 2126 | section_offset_type output_offset; |
730cdc88 ILT |
2127 | if (p->output_offset(object, shndx, offset, &output_offset)) |
2128 | return output_offset; | |
2129 | } | |
2130 | gold_unreachable(); | |
2131 | } | |
2132 | ||
b8e6aad9 ILT |
2133 | // Return the output virtual address of OFFSET relative to the start |
2134 | // of input section SHNDX in object OBJECT. | |
2135 | ||
2136 | uint64_t | |
2137 | Output_section::output_address(const Relobj* object, unsigned int shndx, | |
2138 | off_t offset) const | |
2139 | { | |
2140 | uint64_t addr = this->address() + this->first_input_offset_; | |
2141 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); | |
2142 | p != this->input_sections_.end(); | |
2143 | ++p) | |
2144 | { | |
2145 | addr = align_address(addr, p->addralign()); | |
8383303e | 2146 | section_offset_type output_offset; |
730cdc88 ILT |
2147 | if (p->output_offset(object, shndx, offset, &output_offset)) |
2148 | { | |
2149 | if (output_offset == -1) | |
eff45813 | 2150 | return -1ULL; |
730cdc88 ILT |
2151 | return addr + output_offset; |
2152 | } | |
b8e6aad9 ILT |
2153 | addr += p->data_size(); |
2154 | } | |
2155 | ||
2156 | // If we get here, it means that we don't know the mapping for this | |
2157 | // input section. This might happen in principle if | |
2158 | // add_input_section were called before add_output_section_data. | |
2159 | // But it should never actually happen. | |
2160 | ||
2161 | gold_unreachable(); | |
ead1e424 ILT |
2162 | } |
2163 | ||
e29e076a | 2164 | // Find the output address of the start of the merged section for |
a9a60db6 ILT |
2165 | // input section SHNDX in object OBJECT. |
2166 | ||
e29e076a ILT |
2167 | bool |
2168 | Output_section::find_starting_output_address(const Relobj* object, | |
2169 | unsigned int shndx, | |
2170 | uint64_t* paddr) const | |
a9a60db6 | 2171 | { |
a9a60db6 ILT |
2172 | uint64_t addr = this->address() + this->first_input_offset_; |
2173 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); | |
2174 | p != this->input_sections_.end(); | |
2175 | ++p) | |
2176 | { | |
2177 | addr = align_address(addr, p->addralign()); | |
2178 | ||
2179 | // It would be nice if we could use the existing output_offset | |
2180 | // method to get the output offset of input offset 0. | |
2181 | // Unfortunately we don't know for sure that input offset 0 is | |
2182 | // mapped at all. | |
2183 | if (p->is_merge_section_for(object, shndx)) | |
e29e076a ILT |
2184 | { |
2185 | *paddr = addr; | |
2186 | return true; | |
2187 | } | |
a9a60db6 ILT |
2188 | |
2189 | addr += p->data_size(); | |
2190 | } | |
e29e076a ILT |
2191 | |
2192 | // We couldn't find a merge output section for this input section. | |
2193 | return false; | |
a9a60db6 ILT |
2194 | } |
2195 | ||
27bc2bce | 2196 | // Set the data size of an Output_section. This is where we handle |
ead1e424 ILT |
2197 | // setting the addresses of any Output_section_data objects. |
2198 | ||
2199 | void | |
27bc2bce | 2200 | Output_section::set_final_data_size() |
ead1e424 ILT |
2201 | { |
2202 | if (this->input_sections_.empty()) | |
27bc2bce ILT |
2203 | { |
2204 | this->set_data_size(this->current_data_size_for_child()); | |
2205 | return; | |
2206 | } | |
ead1e424 | 2207 | |
2fd32231 ILT |
2208 | if (this->must_sort_attached_input_sections()) |
2209 | this->sort_attached_input_sections(); | |
2210 | ||
27bc2bce ILT |
2211 | uint64_t address = this->address(); |
2212 | off_t startoff = this->offset(); | |
ead1e424 ILT |
2213 | off_t off = startoff + this->first_input_offset_; |
2214 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
2215 | p != this->input_sections_.end(); | |
2216 | ++p) | |
2217 | { | |
2218 | off = align_address(off, p->addralign()); | |
96803768 ILT |
2219 | p->set_address_and_file_offset(address + (off - startoff), off, |
2220 | startoff); | |
ead1e424 ILT |
2221 | off += p->data_size(); |
2222 | } | |
2223 | ||
2224 | this->set_data_size(off - startoff); | |
2225 | } | |
9a0910c3 | 2226 | |
a445fddf ILT |
2227 | // Reset the address and file offset. |
2228 | ||
2229 | void | |
2230 | Output_section::do_reset_address_and_file_offset() | |
2231 | { | |
20e6d0d6 DK |
2232 | // An unallocated section has no address. Forcing this means that |
2233 | // we don't need special treatment for symbols defined in debug | |
2234 | // sections. We do the same in the constructor. | |
2235 | if ((this->flags_ & elfcpp::SHF_ALLOC) == 0) | |
2236 | this->set_address(0); | |
2237 | ||
a445fddf ILT |
2238 | for (Input_section_list::iterator p = this->input_sections_.begin(); |
2239 | p != this->input_sections_.end(); | |
2240 | ++p) | |
2241 | p->reset_address_and_file_offset(); | |
2242 | } | |
20e6d0d6 DK |
2243 | |
2244 | // Return true if address and file offset have the values after reset. | |
2245 | ||
2246 | bool | |
2247 | Output_section::do_address_and_file_offset_have_reset_values() const | |
2248 | { | |
2249 | if (this->is_offset_valid()) | |
2250 | return false; | |
2251 | ||
2252 | // An unallocated section has address 0 after its construction or a reset. | |
2253 | if ((this->flags_ & elfcpp::SHF_ALLOC) == 0) | |
2254 | return this->is_address_valid() && this->address() == 0; | |
2255 | else | |
2256 | return !this->is_address_valid(); | |
2257 | } | |
a445fddf | 2258 | |
7bf1f802 ILT |
2259 | // Set the TLS offset. Called only for SHT_TLS sections. |
2260 | ||
2261 | void | |
2262 | Output_section::do_set_tls_offset(uint64_t tls_base) | |
2263 | { | |
2264 | this->tls_offset_ = this->address() - tls_base; | |
2265 | } | |
2266 | ||
2fd32231 ILT |
2267 | // In a few cases we need to sort the input sections attached to an |
2268 | // output section. This is used to implement the type of constructor | |
2269 | // priority ordering implemented by the GNU linker, in which the | |
2270 | // priority becomes part of the section name and the sections are | |
2271 | // sorted by name. We only do this for an output section if we see an | |
2272 | // attached input section matching ".ctor.*", ".dtor.*", | |
2273 | // ".init_array.*" or ".fini_array.*". | |
2274 | ||
2275 | class Output_section::Input_section_sort_entry | |
2276 | { | |
2277 | public: | |
2278 | Input_section_sort_entry() | |
2279 | : input_section_(), index_(-1U), section_has_name_(false), | |
2280 | section_name_() | |
2281 | { } | |
2282 | ||
2283 | Input_section_sort_entry(const Input_section& input_section, | |
2284 | unsigned int index) | |
2285 | : input_section_(input_section), index_(index), | |
20e6d0d6 DK |
2286 | section_has_name_(input_section.is_input_section() |
2287 | || input_section.is_relaxed_input_section()) | |
2fd32231 ILT |
2288 | { |
2289 | if (this->section_has_name_) | |
2290 | { | |
2291 | // This is only called single-threaded from Layout::finalize, | |
2292 | // so it is OK to lock. Unfortunately we have no way to pass | |
2293 | // in a Task token. | |
2294 | const Task* dummy_task = reinterpret_cast<const Task*>(-1); | |
20e6d0d6 DK |
2295 | Object* obj = (input_section.is_input_section() |
2296 | ? input_section.relobj() | |
2297 | : input_section.relaxed_input_section()->relobj()); | |
2fd32231 ILT |
2298 | Task_lock_obj<Object> tl(dummy_task, obj); |
2299 | ||
2300 | // This is a slow operation, which should be cached in | |
2301 | // Layout::layout if this becomes a speed problem. | |
2302 | this->section_name_ = obj->section_name(input_section.shndx()); | |
2303 | } | |
2304 | } | |
2305 | ||
2306 | // Return the Input_section. | |
2307 | const Input_section& | |
2308 | input_section() const | |
2309 | { | |
2310 | gold_assert(this->index_ != -1U); | |
2311 | return this->input_section_; | |
2312 | } | |
2313 | ||
2314 | // The index of this entry in the original list. This is used to | |
2315 | // make the sort stable. | |
2316 | unsigned int | |
2317 | index() const | |
2318 | { | |
2319 | gold_assert(this->index_ != -1U); | |
2320 | return this->index_; | |
2321 | } | |
2322 | ||
2323 | // Whether there is a section name. | |
2324 | bool | |
2325 | section_has_name() const | |
2326 | { return this->section_has_name_; } | |
2327 | ||
2328 | // The section name. | |
2329 | const std::string& | |
2330 | section_name() const | |
2331 | { | |
2332 | gold_assert(this->section_has_name_); | |
2333 | return this->section_name_; | |
2334 | } | |
2335 | ||
ab794b6b ILT |
2336 | // Return true if the section name has a priority. This is assumed |
2337 | // to be true if it has a dot after the initial dot. | |
2fd32231 | 2338 | bool |
ab794b6b | 2339 | has_priority() const |
2fd32231 ILT |
2340 | { |
2341 | gold_assert(this->section_has_name_); | |
ab794b6b | 2342 | return this->section_name_.find('.', 1); |
2fd32231 ILT |
2343 | } |
2344 | ||
ab794b6b ILT |
2345 | // Return true if this an input file whose base name matches |
2346 | // FILE_NAME. The base name must have an extension of ".o", and | |
2347 | // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o". | |
2348 | // This is to match crtbegin.o as well as crtbeginS.o without | |
2349 | // getting confused by other possibilities. Overall matching the | |
2350 | // file name this way is a dreadful hack, but the GNU linker does it | |
2351 | // in order to better support gcc, and we need to be compatible. | |
2fd32231 | 2352 | bool |
ab794b6b | 2353 | match_file_name(const char* match_file_name) const |
2fd32231 | 2354 | { |
2fd32231 ILT |
2355 | const std::string& file_name(this->input_section_.relobj()->name()); |
2356 | const char* base_name = lbasename(file_name.c_str()); | |
2357 | size_t match_len = strlen(match_file_name); | |
2358 | if (strncmp(base_name, match_file_name, match_len) != 0) | |
2359 | return false; | |
2360 | size_t base_len = strlen(base_name); | |
2361 | if (base_len != match_len + 2 && base_len != match_len + 3) | |
2362 | return false; | |
2363 | return memcmp(base_name + base_len - 2, ".o", 2) == 0; | |
2364 | } | |
2365 | ||
2366 | private: | |
2367 | // The Input_section we are sorting. | |
2368 | Input_section input_section_; | |
2369 | // The index of this Input_section in the original list. | |
2370 | unsigned int index_; | |
2371 | // Whether this Input_section has a section name--it won't if this | |
2372 | // is some random Output_section_data. | |
2373 | bool section_has_name_; | |
2374 | // The section name if there is one. | |
2375 | std::string section_name_; | |
2376 | }; | |
2377 | ||
2378 | // Return true if S1 should come before S2 in the output section. | |
2379 | ||
2380 | bool | |
2381 | Output_section::Input_section_sort_compare::operator()( | |
2382 | const Output_section::Input_section_sort_entry& s1, | |
2383 | const Output_section::Input_section_sort_entry& s2) const | |
2384 | { | |
ab794b6b ILT |
2385 | // crtbegin.o must come first. |
2386 | bool s1_begin = s1.match_file_name("crtbegin"); | |
2387 | bool s2_begin = s2.match_file_name("crtbegin"); | |
2fd32231 ILT |
2388 | if (s1_begin || s2_begin) |
2389 | { | |
2390 | if (!s1_begin) | |
2391 | return false; | |
2392 | if (!s2_begin) | |
2393 | return true; | |
2394 | return s1.index() < s2.index(); | |
2395 | } | |
2396 | ||
ab794b6b ILT |
2397 | // crtend.o must come last. |
2398 | bool s1_end = s1.match_file_name("crtend"); | |
2399 | bool s2_end = s2.match_file_name("crtend"); | |
2fd32231 ILT |
2400 | if (s1_end || s2_end) |
2401 | { | |
2402 | if (!s1_end) | |
2403 | return true; | |
2404 | if (!s2_end) | |
2405 | return false; | |
2406 | return s1.index() < s2.index(); | |
2407 | } | |
2408 | ||
ab794b6b ILT |
2409 | // We sort all the sections with no names to the end. |
2410 | if (!s1.section_has_name() || !s2.section_has_name()) | |
2411 | { | |
2412 | if (s1.section_has_name()) | |
2413 | return true; | |
2414 | if (s2.section_has_name()) | |
2415 | return false; | |
2416 | return s1.index() < s2.index(); | |
2417 | } | |
2fd32231 | 2418 | |
ab794b6b ILT |
2419 | // A section with a priority follows a section without a priority. |
2420 | // The GNU linker does this for all but .init_array sections; until | |
2421 | // further notice we'll assume that that is an mistake. | |
2422 | bool s1_has_priority = s1.has_priority(); | |
2423 | bool s2_has_priority = s2.has_priority(); | |
2424 | if (s1_has_priority && !s2_has_priority) | |
2fd32231 | 2425 | return false; |
ab794b6b | 2426 | if (!s1_has_priority && s2_has_priority) |
2fd32231 ILT |
2427 | return true; |
2428 | ||
2429 | // Otherwise we sort by name. | |
2430 | int compare = s1.section_name().compare(s2.section_name()); | |
2431 | if (compare != 0) | |
2432 | return compare < 0; | |
2433 | ||
2434 | // Otherwise we keep the input order. | |
2435 | return s1.index() < s2.index(); | |
2436 | } | |
2437 | ||
2438 | // Sort the input sections attached to an output section. | |
2439 | ||
2440 | void | |
2441 | Output_section::sort_attached_input_sections() | |
2442 | { | |
2443 | if (this->attached_input_sections_are_sorted_) | |
2444 | return; | |
2445 | ||
20e6d0d6 DK |
2446 | if (this->checkpoint_ != NULL |
2447 | && !this->checkpoint_->input_sections_saved()) | |
2448 | this->checkpoint_->save_input_sections(); | |
2449 | ||
2fd32231 ILT |
2450 | // The only thing we know about an input section is the object and |
2451 | // the section index. We need the section name. Recomputing this | |
2452 | // is slow but this is an unusual case. If this becomes a speed | |
2453 | // problem we can cache the names as required in Layout::layout. | |
2454 | ||
2455 | // We start by building a larger vector holding a copy of each | |
2456 | // Input_section, plus its current index in the list and its name. | |
2457 | std::vector<Input_section_sort_entry> sort_list; | |
2458 | ||
2459 | unsigned int i = 0; | |
2460 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
2461 | p != this->input_sections_.end(); | |
2462 | ++p, ++i) | |
2463 | sort_list.push_back(Input_section_sort_entry(*p, i)); | |
2464 | ||
2465 | // Sort the input sections. | |
2466 | std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare()); | |
2467 | ||
2468 | // Copy the sorted input sections back to our list. | |
2469 | this->input_sections_.clear(); | |
2470 | for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin(); | |
2471 | p != sort_list.end(); | |
2472 | ++p) | |
2473 | this->input_sections_.push_back(p->input_section()); | |
2474 | ||
2475 | // Remember that we sorted the input sections, since we might get | |
2476 | // called again. | |
2477 | this->attached_input_sections_are_sorted_ = true; | |
2478 | } | |
2479 | ||
61ba1cf9 ILT |
2480 | // Write the section header to *OSHDR. |
2481 | ||
2482 | template<int size, bool big_endian> | |
2483 | void | |
16649710 ILT |
2484 | Output_section::write_header(const Layout* layout, |
2485 | const Stringpool* secnamepool, | |
61ba1cf9 ILT |
2486 | elfcpp::Shdr_write<size, big_endian>* oshdr) const |
2487 | { | |
2488 | oshdr->put_sh_name(secnamepool->get_offset(this->name_)); | |
2489 | oshdr->put_sh_type(this->type_); | |
6a74a719 ILT |
2490 | |
2491 | elfcpp::Elf_Xword flags = this->flags_; | |
755ab8af | 2492 | if (this->info_section_ != NULL && this->info_uses_section_index_) |
6a74a719 ILT |
2493 | flags |= elfcpp::SHF_INFO_LINK; |
2494 | oshdr->put_sh_flags(flags); | |
2495 | ||
61ba1cf9 ILT |
2496 | oshdr->put_sh_addr(this->address()); |
2497 | oshdr->put_sh_offset(this->offset()); | |
2498 | oshdr->put_sh_size(this->data_size()); | |
16649710 ILT |
2499 | if (this->link_section_ != NULL) |
2500 | oshdr->put_sh_link(this->link_section_->out_shndx()); | |
2501 | else if (this->should_link_to_symtab_) | |
2502 | oshdr->put_sh_link(layout->symtab_section()->out_shndx()); | |
2503 | else if (this->should_link_to_dynsym_) | |
2504 | oshdr->put_sh_link(layout->dynsym_section()->out_shndx()); | |
2505 | else | |
2506 | oshdr->put_sh_link(this->link_); | |
755ab8af ILT |
2507 | |
2508 | elfcpp::Elf_Word info; | |
16649710 | 2509 | if (this->info_section_ != NULL) |
755ab8af ILT |
2510 | { |
2511 | if (this->info_uses_section_index_) | |
2512 | info = this->info_section_->out_shndx(); | |
2513 | else | |
2514 | info = this->info_section_->symtab_index(); | |
2515 | } | |
6a74a719 | 2516 | else if (this->info_symndx_ != NULL) |
755ab8af | 2517 | info = this->info_symndx_->symtab_index(); |
16649710 | 2518 | else |
755ab8af ILT |
2519 | info = this->info_; |
2520 | oshdr->put_sh_info(info); | |
2521 | ||
61ba1cf9 ILT |
2522 | oshdr->put_sh_addralign(this->addralign_); |
2523 | oshdr->put_sh_entsize(this->entsize_); | |
a2fb1b05 ILT |
2524 | } |
2525 | ||
ead1e424 ILT |
2526 | // Write out the data. For input sections the data is written out by |
2527 | // Object::relocate, but we have to handle Output_section_data objects | |
2528 | // here. | |
2529 | ||
2530 | void | |
2531 | Output_section::do_write(Output_file* of) | |
2532 | { | |
96803768 ILT |
2533 | gold_assert(!this->requires_postprocessing()); |
2534 | ||
c51e6221 ILT |
2535 | off_t output_section_file_offset = this->offset(); |
2536 | for (Fill_list::iterator p = this->fills_.begin(); | |
2537 | p != this->fills_.end(); | |
2538 | ++p) | |
2539 | { | |
8851ecca | 2540 | std::string fill_data(parameters->target().code_fill(p->length())); |
c51e6221 | 2541 | of->write(output_section_file_offset + p->section_offset(), |
a445fddf | 2542 | fill_data.data(), fill_data.size()); |
c51e6221 ILT |
2543 | } |
2544 | ||
ead1e424 ILT |
2545 | for (Input_section_list::iterator p = this->input_sections_.begin(); |
2546 | p != this->input_sections_.end(); | |
2547 | ++p) | |
2548 | p->write(of); | |
2549 | } | |
2550 | ||
96803768 ILT |
2551 | // If a section requires postprocessing, create the buffer to use. |
2552 | ||
2553 | void | |
2554 | Output_section::create_postprocessing_buffer() | |
2555 | { | |
2556 | gold_assert(this->requires_postprocessing()); | |
1bedcac5 ILT |
2557 | |
2558 | if (this->postprocessing_buffer_ != NULL) | |
2559 | return; | |
96803768 ILT |
2560 | |
2561 | if (!this->input_sections_.empty()) | |
2562 | { | |
2563 | off_t off = this->first_input_offset_; | |
2564 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
2565 | p != this->input_sections_.end(); | |
2566 | ++p) | |
2567 | { | |
2568 | off = align_address(off, p->addralign()); | |
2569 | p->finalize_data_size(); | |
2570 | off += p->data_size(); | |
2571 | } | |
2572 | this->set_current_data_size_for_child(off); | |
2573 | } | |
2574 | ||
2575 | off_t buffer_size = this->current_data_size_for_child(); | |
2576 | this->postprocessing_buffer_ = new unsigned char[buffer_size]; | |
2577 | } | |
2578 | ||
2579 | // Write all the data of an Output_section into the postprocessing | |
2580 | // buffer. This is used for sections which require postprocessing, | |
2581 | // such as compression. Input sections are handled by | |
2582 | // Object::Relocate. | |
2583 | ||
2584 | void | |
2585 | Output_section::write_to_postprocessing_buffer() | |
2586 | { | |
2587 | gold_assert(this->requires_postprocessing()); | |
2588 | ||
96803768 ILT |
2589 | unsigned char* buffer = this->postprocessing_buffer(); |
2590 | for (Fill_list::iterator p = this->fills_.begin(); | |
2591 | p != this->fills_.end(); | |
2592 | ++p) | |
2593 | { | |
8851ecca | 2594 | std::string fill_data(parameters->target().code_fill(p->length())); |
a445fddf ILT |
2595 | memcpy(buffer + p->section_offset(), fill_data.data(), |
2596 | fill_data.size()); | |
96803768 ILT |
2597 | } |
2598 | ||
2599 | off_t off = this->first_input_offset_; | |
2600 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
2601 | p != this->input_sections_.end(); | |
2602 | ++p) | |
2603 | { | |
2604 | off = align_address(off, p->addralign()); | |
2605 | p->write_to_buffer(buffer + off); | |
2606 | off += p->data_size(); | |
2607 | } | |
2608 | } | |
2609 | ||
a445fddf ILT |
2610 | // Get the input sections for linker script processing. We leave |
2611 | // behind the Output_section_data entries. Note that this may be | |
2612 | // slightly incorrect for merge sections. We will leave them behind, | |
2613 | // but it is possible that the script says that they should follow | |
2614 | // some other input sections, as in: | |
2615 | // .rodata { *(.rodata) *(.rodata.cst*) } | |
2616 | // For that matter, we don't handle this correctly: | |
2617 | // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) } | |
2618 | // With luck this will never matter. | |
2619 | ||
2620 | uint64_t | |
2621 | Output_section::get_input_sections( | |
2622 | uint64_t address, | |
2623 | const std::string& fill, | |
20e6d0d6 | 2624 | std::list<Simple_input_section>* input_sections) |
a445fddf | 2625 | { |
20e6d0d6 DK |
2626 | if (this->checkpoint_ != NULL |
2627 | && !this->checkpoint_->input_sections_saved()) | |
2628 | this->checkpoint_->save_input_sections(); | |
2629 | ||
a445fddf ILT |
2630 | uint64_t orig_address = address; |
2631 | ||
2632 | address = align_address(address, this->addralign()); | |
2633 | ||
2634 | Input_section_list remaining; | |
2635 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
2636 | p != this->input_sections_.end(); | |
2637 | ++p) | |
2638 | { | |
2639 | if (p->is_input_section()) | |
20e6d0d6 DK |
2640 | input_sections->push_back(Simple_input_section(p->relobj(), |
2641 | p->shndx())); | |
2642 | else if (p->is_relaxed_input_section()) | |
2643 | input_sections->push_back( | |
2644 | Simple_input_section(p->relaxed_input_section())); | |
a445fddf ILT |
2645 | else |
2646 | { | |
2647 | uint64_t aligned_address = align_address(address, p->addralign()); | |
2648 | if (aligned_address != address && !fill.empty()) | |
2649 | { | |
2650 | section_size_type length = | |
2651 | convert_to_section_size_type(aligned_address - address); | |
2652 | std::string this_fill; | |
2653 | this_fill.reserve(length); | |
2654 | while (this_fill.length() + fill.length() <= length) | |
2655 | this_fill += fill; | |
2656 | if (this_fill.length() < length) | |
2657 | this_fill.append(fill, 0, length - this_fill.length()); | |
2658 | ||
2659 | Output_section_data* posd = new Output_data_const(this_fill, 0); | |
2660 | remaining.push_back(Input_section(posd)); | |
2661 | } | |
2662 | address = aligned_address; | |
2663 | ||
2664 | remaining.push_back(*p); | |
2665 | ||
2666 | p->finalize_data_size(); | |
2667 | address += p->data_size(); | |
2668 | } | |
2669 | } | |
2670 | ||
2671 | this->input_sections_.swap(remaining); | |
2672 | this->first_input_offset_ = 0; | |
2673 | ||
2674 | uint64_t data_size = address - orig_address; | |
2675 | this->set_current_data_size_for_child(data_size); | |
2676 | return data_size; | |
2677 | } | |
2678 | ||
2679 | // Add an input section from a script. | |
2680 | ||
2681 | void | |
20e6d0d6 | 2682 | Output_section::add_input_section_for_script(const Simple_input_section& sis, |
a445fddf ILT |
2683 | off_t data_size, |
2684 | uint64_t addralign) | |
2685 | { | |
2686 | if (addralign > this->addralign_) | |
2687 | this->addralign_ = addralign; | |
2688 | ||
2689 | off_t offset_in_section = this->current_data_size_for_child(); | |
2690 | off_t aligned_offset_in_section = align_address(offset_in_section, | |
2691 | addralign); | |
2692 | ||
2693 | this->set_current_data_size_for_child(aligned_offset_in_section | |
2694 | + data_size); | |
2695 | ||
20e6d0d6 DK |
2696 | Input_section is = |
2697 | (sis.is_relaxed_input_section() | |
2698 | ? Input_section(sis.relaxed_input_section()) | |
2699 | : Input_section(sis.relobj(), sis.shndx(), data_size, addralign)); | |
2700 | this->input_sections_.push_back(is); | |
2701 | } | |
2702 | ||
2703 | // | |
2704 | ||
2705 | void | |
2706 | Output_section::save_states() | |
2707 | { | |
2708 | gold_assert(this->checkpoint_ == NULL); | |
2709 | Checkpoint_output_section* checkpoint = | |
2710 | new Checkpoint_output_section(this->addralign_, this->flags_, | |
2711 | this->input_sections_, | |
2712 | this->first_input_offset_, | |
2713 | this->attached_input_sections_are_sorted_); | |
2714 | this->checkpoint_ = checkpoint; | |
2715 | gold_assert(this->fills_.empty()); | |
2716 | } | |
2717 | ||
2718 | void | |
2719 | Output_section::restore_states() | |
2720 | { | |
2721 | gold_assert(this->checkpoint_ != NULL); | |
2722 | Checkpoint_output_section* checkpoint = this->checkpoint_; | |
2723 | ||
2724 | this->addralign_ = checkpoint->addralign(); | |
2725 | this->flags_ = checkpoint->flags(); | |
2726 | this->first_input_offset_ = checkpoint->first_input_offset(); | |
2727 | ||
2728 | if (!checkpoint->input_sections_saved()) | |
2729 | { | |
2730 | // If we have not copied the input sections, just resize it. | |
2731 | size_t old_size = checkpoint->input_sections_size(); | |
2732 | gold_assert(this->input_sections_.size() >= old_size); | |
2733 | this->input_sections_.resize(old_size); | |
2734 | } | |
2735 | else | |
2736 | { | |
2737 | // We need to copy the whole list. This is not efficient for | |
2738 | // extremely large output with hundreads of thousands of input | |
2739 | // objects. We may need to re-think how we should pass sections | |
2740 | // to scripts. | |
2741 | this->input_sections_ = checkpoint->input_sections(); | |
2742 | } | |
2743 | ||
2744 | this->attached_input_sections_are_sorted_ = | |
2745 | checkpoint->attached_input_sections_are_sorted(); | |
a445fddf ILT |
2746 | } |
2747 | ||
7d9e3d98 ILT |
2748 | // Print to the map file. |
2749 | ||
2750 | void | |
2751 | Output_section::do_print_to_mapfile(Mapfile* mapfile) const | |
2752 | { | |
2753 | mapfile->print_output_section(this); | |
2754 | ||
2755 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); | |
2756 | p != this->input_sections_.end(); | |
2757 | ++p) | |
2758 | p->print_to_mapfile(mapfile); | |
2759 | } | |
2760 | ||
38c5e8b4 ILT |
2761 | // Print stats for merge sections to stderr. |
2762 | ||
2763 | void | |
2764 | Output_section::print_merge_stats() | |
2765 | { | |
2766 | Input_section_list::iterator p; | |
2767 | for (p = this->input_sections_.begin(); | |
2768 | p != this->input_sections_.end(); | |
2769 | ++p) | |
2770 | p->print_merge_stats(this->name_); | |
2771 | } | |
2772 | ||
a2fb1b05 ILT |
2773 | // Output segment methods. |
2774 | ||
2775 | Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags) | |
54dc6425 | 2776 | : output_data_(), |
75f65a3e | 2777 | output_bss_(), |
a2fb1b05 ILT |
2778 | vaddr_(0), |
2779 | paddr_(0), | |
2780 | memsz_(0), | |
a445fddf ILT |
2781 | max_align_(0), |
2782 | min_p_align_(0), | |
a2fb1b05 ILT |
2783 | offset_(0), |
2784 | filesz_(0), | |
2785 | type_(type), | |
ead1e424 | 2786 | flags_(flags), |
a445fddf | 2787 | is_max_align_known_(false), |
8a5e3e08 ILT |
2788 | are_addresses_set_(false), |
2789 | is_large_data_segment_(false) | |
a2fb1b05 ILT |
2790 | { |
2791 | } | |
2792 | ||
2793 | // Add an Output_section to an Output_segment. | |
2794 | ||
2795 | void | |
75f65a3e | 2796 | Output_segment::add_output_section(Output_section* os, |
01676dcd | 2797 | elfcpp::Elf_Word seg_flags) |
a2fb1b05 | 2798 | { |
a3ad94ed | 2799 | gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0); |
a445fddf | 2800 | gold_assert(!this->is_max_align_known_); |
8a5e3e08 | 2801 | gold_assert(os->is_large_data_section() == this->is_large_data_segment()); |
75f65a3e | 2802 | |
ead1e424 | 2803 | // Update the segment flags. |
75f65a3e | 2804 | this->flags_ |= seg_flags; |
75f65a3e ILT |
2805 | |
2806 | Output_segment::Output_data_list* pdl; | |
2807 | if (os->type() == elfcpp::SHT_NOBITS) | |
2808 | pdl = &this->output_bss_; | |
2809 | else | |
2810 | pdl = &this->output_data_; | |
54dc6425 | 2811 | |
a2fb1b05 ILT |
2812 | // So that PT_NOTE segments will work correctly, we need to ensure |
2813 | // that all SHT_NOTE sections are adjacent. This will normally | |
2814 | // happen automatically, because all the SHT_NOTE input sections | |
2815 | // will wind up in the same output section. However, it is possible | |
2816 | // for multiple SHT_NOTE input sections to have different section | |
2817 | // flags, and thus be in different output sections, but for the | |
2818 | // different section flags to map into the same segment flags and | |
2819 | // thus the same output segment. | |
54dc6425 ILT |
2820 | |
2821 | // Note that while there may be many input sections in an output | |
2822 | // section, there are normally only a few output sections in an | |
2823 | // output segment. This loop is expected to be fast. | |
2824 | ||
61ba1cf9 | 2825 | if (os->type() == elfcpp::SHT_NOTE && !pdl->empty()) |
a2fb1b05 | 2826 | { |
a3ad94ed | 2827 | Output_segment::Output_data_list::iterator p = pdl->end(); |
75f65a3e | 2828 | do |
54dc6425 | 2829 | { |
75f65a3e | 2830 | --p; |
54dc6425 ILT |
2831 | if ((*p)->is_section_type(elfcpp::SHT_NOTE)) |
2832 | { | |
2833 | ++p; | |
75f65a3e | 2834 | pdl->insert(p, os); |
54dc6425 ILT |
2835 | return; |
2836 | } | |
2837 | } | |
75f65a3e | 2838 | while (p != pdl->begin()); |
54dc6425 ILT |
2839 | } |
2840 | ||
2841 | // Similarly, so that PT_TLS segments will work, we need to group | |
75f65a3e ILT |
2842 | // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special |
2843 | // case: we group the SHF_TLS/SHT_NOBITS sections right after the | |
2844 | // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS | |
07f397ab ILT |
2845 | // correctly. SHF_TLS sections get added to both a PT_LOAD segment |
2846 | // and the PT_TLS segment -- we do this grouping only for the | |
2847 | // PT_LOAD segment. | |
2848 | if (this->type_ != elfcpp::PT_TLS | |
2d924fd9 | 2849 | && (os->flags() & elfcpp::SHF_TLS) != 0) |
54dc6425 | 2850 | { |
75f65a3e ILT |
2851 | pdl = &this->output_data_; |
2852 | bool nobits = os->type() == elfcpp::SHT_NOBITS; | |
ead1e424 | 2853 | bool sawtls = false; |
a3ad94ed | 2854 | Output_segment::Output_data_list::iterator p = pdl->end(); |
75f65a3e | 2855 | do |
a2fb1b05 | 2856 | { |
75f65a3e | 2857 | --p; |
ead1e424 ILT |
2858 | bool insert; |
2859 | if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)) | |
2860 | { | |
2861 | sawtls = true; | |
2862 | // Put a NOBITS section after the first TLS section. | |
9f1d377b | 2863 | // Put a PROGBITS section after the first TLS/PROGBITS |
ead1e424 ILT |
2864 | // section. |
2865 | insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS); | |
2866 | } | |
2867 | else | |
2868 | { | |
2869 | // If we've gone past the TLS sections, but we've seen a | |
2870 | // TLS section, then we need to insert this section now. | |
2871 | insert = sawtls; | |
2872 | } | |
2873 | ||
2874 | if (insert) | |
a2fb1b05 ILT |
2875 | { |
2876 | ++p; | |
75f65a3e | 2877 | pdl->insert(p, os); |
a2fb1b05 ILT |
2878 | return; |
2879 | } | |
2880 | } | |
75f65a3e | 2881 | while (p != pdl->begin()); |
ead1e424 | 2882 | |
dbe717ef ILT |
2883 | // There are no TLS sections yet; put this one at the requested |
2884 | // location in the section list. | |
a2fb1b05 ILT |
2885 | } |
2886 | ||
9f1d377b ILT |
2887 | // For the PT_GNU_RELRO segment, we need to group relro sections, |
2888 | // and we need to put them before any non-relro sections. Also, | |
2889 | // relro local sections go before relro non-local sections. | |
2890 | if (parameters->options().relro() && os->is_relro()) | |
2891 | { | |
2892 | gold_assert(pdl == &this->output_data_); | |
2893 | Output_segment::Output_data_list::iterator p; | |
2894 | for (p = pdl->begin(); p != pdl->end(); ++p) | |
2895 | { | |
2896 | if (!(*p)->is_section()) | |
2897 | break; | |
2898 | ||
2899 | Output_section* pos = (*p)->output_section(); | |
2900 | if (!pos->is_relro() | |
2901 | || (os->is_relro_local() && !pos->is_relro_local())) | |
2902 | break; | |
2903 | } | |
2904 | ||
2905 | pdl->insert(p, os); | |
2906 | return; | |
2907 | } | |
2908 | ||
8a5e3e08 ILT |
2909 | // Small data sections go at the end of the list of data sections. |
2910 | // If OS is not small, and there are small sections, we have to | |
2911 | // insert it before the first small section. | |
2912 | if (os->type() != elfcpp::SHT_NOBITS | |
2913 | && !os->is_small_section() | |
2914 | && !pdl->empty() | |
2915 | && pdl->back()->is_section() | |
2916 | && pdl->back()->output_section()->is_small_section()) | |
2917 | { | |
2918 | for (Output_segment::Output_data_list::iterator p = pdl->begin(); | |
2919 | p != pdl->end(); | |
2920 | ++p) | |
2921 | { | |
2922 | if ((*p)->is_section() | |
2923 | && (*p)->output_section()->is_small_section()) | |
2924 | { | |
2925 | pdl->insert(p, os); | |
2926 | return; | |
2927 | } | |
2928 | } | |
2929 | gold_unreachable(); | |
2930 | } | |
2931 | ||
2932 | // A small BSS section goes at the start of the BSS sections, after | |
2933 | // other small BSS sections. | |
2934 | if (os->type() == elfcpp::SHT_NOBITS && os->is_small_section()) | |
2935 | { | |
2936 | for (Output_segment::Output_data_list::iterator p = pdl->begin(); | |
2937 | p != pdl->end(); | |
2938 | ++p) | |
2939 | { | |
2940 | if (!(*p)->is_section() | |
2941 | || !(*p)->output_section()->is_small_section()) | |
2942 | { | |
2943 | pdl->insert(p, os); | |
2944 | return; | |
2945 | } | |
2946 | } | |
2947 | } | |
2948 | ||
2949 | // A large BSS section goes at the end of the BSS sections, which | |
2950 | // means that one that is not large must come before the first large | |
2951 | // one. | |
2952 | if (os->type() == elfcpp::SHT_NOBITS | |
2953 | && !os->is_large_section() | |
2954 | && !pdl->empty() | |
2955 | && pdl->back()->is_section() | |
2956 | && pdl->back()->output_section()->is_large_section()) | |
2957 | { | |
2958 | for (Output_segment::Output_data_list::iterator p = pdl->begin(); | |
2959 | p != pdl->end(); | |
2960 | ++p) | |
2961 | { | |
2962 | if ((*p)->is_section() | |
2963 | && (*p)->output_section()->is_large_section()) | |
2964 | { | |
2965 | pdl->insert(p, os); | |
2966 | return; | |
2967 | } | |
2968 | } | |
2969 | gold_unreachable(); | |
2970 | } | |
2971 | ||
01676dcd | 2972 | pdl->push_back(os); |
75f65a3e ILT |
2973 | } |
2974 | ||
1650c4ff ILT |
2975 | // Remove an Output_section from this segment. It is an error if it |
2976 | // is not present. | |
2977 | ||
2978 | void | |
2979 | Output_segment::remove_output_section(Output_section* os) | |
2980 | { | |
2981 | // We only need this for SHT_PROGBITS. | |
2982 | gold_assert(os->type() == elfcpp::SHT_PROGBITS); | |
2983 | for (Output_data_list::iterator p = this->output_data_.begin(); | |
2984 | p != this->output_data_.end(); | |
2985 | ++p) | |
2986 | { | |
2987 | if (*p == os) | |
2988 | { | |
2989 | this->output_data_.erase(p); | |
2990 | return; | |
2991 | } | |
2992 | } | |
2993 | gold_unreachable(); | |
2994 | } | |
2995 | ||
75f65a3e ILT |
2996 | // Add an Output_data (which is not an Output_section) to the start of |
2997 | // a segment. | |
2998 | ||
2999 | void | |
3000 | Output_segment::add_initial_output_data(Output_data* od) | |
3001 | { | |
a445fddf | 3002 | gold_assert(!this->is_max_align_known_); |
75f65a3e ILT |
3003 | this->output_data_.push_front(od); |
3004 | } | |
3005 | ||
9f1d377b ILT |
3006 | // Return whether the first data section is a relro section. |
3007 | ||
3008 | bool | |
3009 | Output_segment::is_first_section_relro() const | |
3010 | { | |
3011 | return (!this->output_data_.empty() | |
3012 | && this->output_data_.front()->is_section() | |
3013 | && this->output_data_.front()->output_section()->is_relro()); | |
3014 | } | |
3015 | ||
75f65a3e | 3016 | // Return the maximum alignment of the Output_data in Output_segment. |
75f65a3e ILT |
3017 | |
3018 | uint64_t | |
a445fddf | 3019 | Output_segment::maximum_alignment() |
75f65a3e | 3020 | { |
a445fddf | 3021 | if (!this->is_max_align_known_) |
ead1e424 ILT |
3022 | { |
3023 | uint64_t addralign; | |
3024 | ||
a445fddf ILT |
3025 | addralign = Output_segment::maximum_alignment_list(&this->output_data_); |
3026 | if (addralign > this->max_align_) | |
3027 | this->max_align_ = addralign; | |
ead1e424 | 3028 | |
a445fddf ILT |
3029 | addralign = Output_segment::maximum_alignment_list(&this->output_bss_); |
3030 | if (addralign > this->max_align_) | |
3031 | this->max_align_ = addralign; | |
ead1e424 | 3032 | |
9f1d377b ILT |
3033 | // If -z relro is in effect, and the first section in this |
3034 | // segment is a relro section, then the segment must be aligned | |
3035 | // to at least the common page size. This ensures that the | |
3036 | // PT_GNU_RELRO segment will start at a page boundary. | |
2d924fd9 ILT |
3037 | if (this->type_ == elfcpp::PT_LOAD |
3038 | && parameters->options().relro() | |
3039 | && this->is_first_section_relro()) | |
9f1d377b ILT |
3040 | { |
3041 | addralign = parameters->target().common_pagesize(); | |
3042 | if (addralign > this->max_align_) | |
3043 | this->max_align_ = addralign; | |
3044 | } | |
3045 | ||
a445fddf | 3046 | this->is_max_align_known_ = true; |
ead1e424 ILT |
3047 | } |
3048 | ||
a445fddf | 3049 | return this->max_align_; |
75f65a3e ILT |
3050 | } |
3051 | ||
ead1e424 ILT |
3052 | // Return the maximum alignment of a list of Output_data. |
3053 | ||
3054 | uint64_t | |
a445fddf | 3055 | Output_segment::maximum_alignment_list(const Output_data_list* pdl) |
ead1e424 ILT |
3056 | { |
3057 | uint64_t ret = 0; | |
3058 | for (Output_data_list::const_iterator p = pdl->begin(); | |
3059 | p != pdl->end(); | |
3060 | ++p) | |
3061 | { | |
3062 | uint64_t addralign = (*p)->addralign(); | |
3063 | if (addralign > ret) | |
3064 | ret = addralign; | |
3065 | } | |
3066 | return ret; | |
3067 | } | |
3068 | ||
4f4c5f80 ILT |
3069 | // Return the number of dynamic relocs applied to this segment. |
3070 | ||
3071 | unsigned int | |
3072 | Output_segment::dynamic_reloc_count() const | |
3073 | { | |
3074 | return (this->dynamic_reloc_count_list(&this->output_data_) | |
3075 | + this->dynamic_reloc_count_list(&this->output_bss_)); | |
3076 | } | |
3077 | ||
3078 | // Return the number of dynamic relocs applied to an Output_data_list. | |
3079 | ||
3080 | unsigned int | |
3081 | Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const | |
3082 | { | |
3083 | unsigned int count = 0; | |
3084 | for (Output_data_list::const_iterator p = pdl->begin(); | |
3085 | p != pdl->end(); | |
3086 | ++p) | |
3087 | count += (*p)->dynamic_reloc_count(); | |
3088 | return count; | |
3089 | } | |
3090 | ||
a445fddf ILT |
3091 | // Set the section addresses for an Output_segment. If RESET is true, |
3092 | // reset the addresses first. ADDR is the address and *POFF is the | |
3093 | // file offset. Set the section indexes starting with *PSHNDX. | |
3094 | // Return the address of the immediately following segment. Update | |
3095 | // *POFF and *PSHNDX. | |
75f65a3e ILT |
3096 | |
3097 | uint64_t | |
96a2b4e4 ILT |
3098 | Output_segment::set_section_addresses(const Layout* layout, bool reset, |
3099 | uint64_t addr, off_t* poff, | |
ead1e424 | 3100 | unsigned int* pshndx) |
75f65a3e | 3101 | { |
a3ad94ed | 3102 | gold_assert(this->type_ == elfcpp::PT_LOAD); |
75f65a3e | 3103 | |
a445fddf ILT |
3104 | if (!reset && this->are_addresses_set_) |
3105 | { | |
3106 | gold_assert(this->paddr_ == addr); | |
3107 | addr = this->vaddr_; | |
3108 | } | |
3109 | else | |
3110 | { | |
3111 | this->vaddr_ = addr; | |
3112 | this->paddr_ = addr; | |
3113 | this->are_addresses_set_ = true; | |
3114 | } | |
75f65a3e | 3115 | |
96a2b4e4 ILT |
3116 | bool in_tls = false; |
3117 | ||
9f1d377b ILT |
3118 | bool in_relro = (parameters->options().relro() |
3119 | && this->is_first_section_relro()); | |
3120 | ||
75f65a3e ILT |
3121 | off_t orig_off = *poff; |
3122 | this->offset_ = orig_off; | |
3123 | ||
96a2b4e4 | 3124 | addr = this->set_section_list_addresses(layout, reset, &this->output_data_, |
9f1d377b ILT |
3125 | addr, poff, pshndx, &in_tls, |
3126 | &in_relro); | |
75f65a3e ILT |
3127 | this->filesz_ = *poff - orig_off; |
3128 | ||
3129 | off_t off = *poff; | |
3130 | ||
96a2b4e4 ILT |
3131 | uint64_t ret = this->set_section_list_addresses(layout, reset, |
3132 | &this->output_bss_, | |
3133 | addr, poff, pshndx, | |
9f1d377b | 3134 | &in_tls, &in_relro); |
96a2b4e4 ILT |
3135 | |
3136 | // If the last section was a TLS section, align upward to the | |
3137 | // alignment of the TLS segment, so that the overall size of the TLS | |
3138 | // segment is aligned. | |
3139 | if (in_tls) | |
3140 | { | |
3141 | uint64_t segment_align = layout->tls_segment()->maximum_alignment(); | |
3142 | *poff = align_address(*poff, segment_align); | |
3143 | } | |
3144 | ||
9f1d377b ILT |
3145 | // If all the sections were relro sections, align upward to the |
3146 | // common page size. | |
3147 | if (in_relro) | |
3148 | { | |
3149 | uint64_t page_align = parameters->target().common_pagesize(); | |
3150 | *poff = align_address(*poff, page_align); | |
3151 | } | |
3152 | ||
75f65a3e ILT |
3153 | this->memsz_ = *poff - orig_off; |
3154 | ||
3155 | // Ignore the file offset adjustments made by the BSS Output_data | |
3156 | // objects. | |
3157 | *poff = off; | |
61ba1cf9 ILT |
3158 | |
3159 | return ret; | |
75f65a3e ILT |
3160 | } |
3161 | ||
b8e6aad9 ILT |
3162 | // Set the addresses and file offsets in a list of Output_data |
3163 | // structures. | |
75f65a3e ILT |
3164 | |
3165 | uint64_t | |
96a2b4e4 ILT |
3166 | Output_segment::set_section_list_addresses(const Layout* layout, bool reset, |
3167 | Output_data_list* pdl, | |
ead1e424 | 3168 | uint64_t addr, off_t* poff, |
96a2b4e4 | 3169 | unsigned int* pshndx, |
9f1d377b | 3170 | bool* in_tls, bool* in_relro) |
75f65a3e | 3171 | { |
ead1e424 | 3172 | off_t startoff = *poff; |
75f65a3e | 3173 | |
ead1e424 | 3174 | off_t off = startoff; |
75f65a3e ILT |
3175 | for (Output_data_list::iterator p = pdl->begin(); |
3176 | p != pdl->end(); | |
3177 | ++p) | |
3178 | { | |
a445fddf ILT |
3179 | if (reset) |
3180 | (*p)->reset_address_and_file_offset(); | |
3181 | ||
3182 | // When using a linker script the section will most likely | |
3183 | // already have an address. | |
3184 | if (!(*p)->is_address_valid()) | |
3802b2dd | 3185 | { |
96a2b4e4 ILT |
3186 | uint64_t align = (*p)->addralign(); |
3187 | ||
3188 | if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)) | |
3189 | { | |
3190 | // Give the first TLS section the alignment of the | |
3191 | // entire TLS segment. Otherwise the TLS segment as a | |
3192 | // whole may be misaligned. | |
3193 | if (!*in_tls) | |
3194 | { | |
3195 | Output_segment* tls_segment = layout->tls_segment(); | |
3196 | gold_assert(tls_segment != NULL); | |
3197 | uint64_t segment_align = tls_segment->maximum_alignment(); | |
3198 | gold_assert(segment_align >= align); | |
3199 | align = segment_align; | |
3200 | ||
3201 | *in_tls = true; | |
3202 | } | |
3203 | } | |
3204 | else | |
3205 | { | |
3206 | // If this is the first section after the TLS segment, | |
3207 | // align it to at least the alignment of the TLS | |
3208 | // segment, so that the size of the overall TLS segment | |
3209 | // is aligned. | |
3210 | if (*in_tls) | |
3211 | { | |
3212 | uint64_t segment_align = | |
3213 | layout->tls_segment()->maximum_alignment(); | |
3214 | if (segment_align > align) | |
3215 | align = segment_align; | |
3216 | ||
3217 | *in_tls = false; | |
3218 | } | |
3219 | } | |
3220 | ||
9f1d377b ILT |
3221 | // If this is a non-relro section after a relro section, |
3222 | // align it to a common page boundary so that the dynamic | |
3223 | // linker has a page to mark as read-only. | |
3224 | if (*in_relro | |
3225 | && (!(*p)->is_section() | |
3226 | || !(*p)->output_section()->is_relro())) | |
3227 | { | |
3228 | uint64_t page_align = parameters->target().common_pagesize(); | |
3229 | if (page_align > align) | |
3230 | align = page_align; | |
3231 | *in_relro = false; | |
3232 | } | |
3233 | ||
96a2b4e4 | 3234 | off = align_address(off, align); |
3802b2dd ILT |
3235 | (*p)->set_address_and_file_offset(addr + (off - startoff), off); |
3236 | } | |
a445fddf ILT |
3237 | else |
3238 | { | |
3239 | // The script may have inserted a skip forward, but it | |
3240 | // better not have moved backward. | |
3802b2dd ILT |
3241 | gold_assert((*p)->address() >= addr + (off - startoff)); |
3242 | off += (*p)->address() - (addr + (off - startoff)); | |
a445fddf ILT |
3243 | (*p)->set_file_offset(off); |
3244 | (*p)->finalize_data_size(); | |
3245 | } | |
ead1e424 | 3246 | |
96a2b4e4 ILT |
3247 | // We want to ignore the size of a SHF_TLS or SHT_NOBITS |
3248 | // section. Such a section does not affect the size of a | |
3249 | // PT_LOAD segment. | |
3250 | if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS) | |
ead1e424 ILT |
3251 | || !(*p)->is_section_type(elfcpp::SHT_NOBITS)) |
3252 | off += (*p)->data_size(); | |
75f65a3e | 3253 | |
ead1e424 ILT |
3254 | if ((*p)->is_section()) |
3255 | { | |
3256 | (*p)->set_out_shndx(*pshndx); | |
3257 | ++*pshndx; | |
3258 | } | |
75f65a3e ILT |
3259 | } |
3260 | ||
3261 | *poff = off; | |
ead1e424 | 3262 | return addr + (off - startoff); |
75f65a3e ILT |
3263 | } |
3264 | ||
3265 | // For a non-PT_LOAD segment, set the offset from the sections, if | |
3266 | // any. | |
3267 | ||
3268 | void | |
3269 | Output_segment::set_offset() | |
3270 | { | |
a3ad94ed | 3271 | gold_assert(this->type_ != elfcpp::PT_LOAD); |
75f65a3e | 3272 | |
a445fddf ILT |
3273 | gold_assert(!this->are_addresses_set_); |
3274 | ||
75f65a3e ILT |
3275 | if (this->output_data_.empty() && this->output_bss_.empty()) |
3276 | { | |
3277 | this->vaddr_ = 0; | |
3278 | this->paddr_ = 0; | |
a445fddf | 3279 | this->are_addresses_set_ = true; |
75f65a3e | 3280 | this->memsz_ = 0; |
a445fddf | 3281 | this->min_p_align_ = 0; |
75f65a3e ILT |
3282 | this->offset_ = 0; |
3283 | this->filesz_ = 0; | |
3284 | return; | |
3285 | } | |
3286 | ||
3287 | const Output_data* first; | |
3288 | if (this->output_data_.empty()) | |
3289 | first = this->output_bss_.front(); | |
3290 | else | |
3291 | first = this->output_data_.front(); | |
3292 | this->vaddr_ = first->address(); | |
a445fddf ILT |
3293 | this->paddr_ = (first->has_load_address() |
3294 | ? first->load_address() | |
3295 | : this->vaddr_); | |
3296 | this->are_addresses_set_ = true; | |
75f65a3e ILT |
3297 | this->offset_ = first->offset(); |
3298 | ||
3299 | if (this->output_data_.empty()) | |
3300 | this->filesz_ = 0; | |
3301 | else | |
3302 | { | |
3303 | const Output_data* last_data = this->output_data_.back(); | |
3304 | this->filesz_ = (last_data->address() | |
3305 | + last_data->data_size() | |
3306 | - this->vaddr_); | |
3307 | } | |
3308 | ||
3309 | const Output_data* last; | |
3310 | if (this->output_bss_.empty()) | |
3311 | last = this->output_data_.back(); | |
3312 | else | |
3313 | last = this->output_bss_.back(); | |
3314 | this->memsz_ = (last->address() | |
3315 | + last->data_size() | |
3316 | - this->vaddr_); | |
96a2b4e4 ILT |
3317 | |
3318 | // If this is a TLS segment, align the memory size. The code in | |
3319 | // set_section_list ensures that the section after the TLS segment | |
3320 | // is aligned to give us room. | |
3321 | if (this->type_ == elfcpp::PT_TLS) | |
3322 | { | |
3323 | uint64_t segment_align = this->maximum_alignment(); | |
3324 | gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align)); | |
3325 | this->memsz_ = align_address(this->memsz_, segment_align); | |
3326 | } | |
9f1d377b ILT |
3327 | |
3328 | // If this is a RELRO segment, align the memory size. The code in | |
3329 | // set_section_list ensures that the section after the RELRO segment | |
3330 | // is aligned to give us room. | |
3331 | if (this->type_ == elfcpp::PT_GNU_RELRO) | |
3332 | { | |
3333 | uint64_t page_align = parameters->target().common_pagesize(); | |
3334 | gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align)); | |
3335 | this->memsz_ = align_address(this->memsz_, page_align); | |
3336 | } | |
75f65a3e ILT |
3337 | } |
3338 | ||
7bf1f802 ILT |
3339 | // Set the TLS offsets of the sections in the PT_TLS segment. |
3340 | ||
3341 | void | |
3342 | Output_segment::set_tls_offsets() | |
3343 | { | |
3344 | gold_assert(this->type_ == elfcpp::PT_TLS); | |
3345 | ||
3346 | for (Output_data_list::iterator p = this->output_data_.begin(); | |
3347 | p != this->output_data_.end(); | |
3348 | ++p) | |
3349 | (*p)->set_tls_offset(this->vaddr_); | |
3350 | ||
3351 | for (Output_data_list::iterator p = this->output_bss_.begin(); | |
3352 | p != this->output_bss_.end(); | |
3353 | ++p) | |
3354 | (*p)->set_tls_offset(this->vaddr_); | |
3355 | } | |
3356 | ||
a445fddf ILT |
3357 | // Return the address of the first section. |
3358 | ||
3359 | uint64_t | |
3360 | Output_segment::first_section_load_address() const | |
3361 | { | |
3362 | for (Output_data_list::const_iterator p = this->output_data_.begin(); | |
3363 | p != this->output_data_.end(); | |
3364 | ++p) | |
3365 | if ((*p)->is_section()) | |
3366 | return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address(); | |
3367 | ||
3368 | for (Output_data_list::const_iterator p = this->output_bss_.begin(); | |
3369 | p != this->output_bss_.end(); | |
3370 | ++p) | |
3371 | if ((*p)->is_section()) | |
3372 | return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address(); | |
3373 | ||
3374 | gold_unreachable(); | |
3375 | } | |
3376 | ||
75f65a3e ILT |
3377 | // Return the number of Output_sections in an Output_segment. |
3378 | ||
3379 | unsigned int | |
3380 | Output_segment::output_section_count() const | |
3381 | { | |
3382 | return (this->output_section_count_list(&this->output_data_) | |
3383 | + this->output_section_count_list(&this->output_bss_)); | |
3384 | } | |
3385 | ||
3386 | // Return the number of Output_sections in an Output_data_list. | |
3387 | ||
3388 | unsigned int | |
3389 | Output_segment::output_section_count_list(const Output_data_list* pdl) const | |
3390 | { | |
3391 | unsigned int count = 0; | |
3392 | for (Output_data_list::const_iterator p = pdl->begin(); | |
3393 | p != pdl->end(); | |
3394 | ++p) | |
3395 | { | |
3396 | if ((*p)->is_section()) | |
3397 | ++count; | |
3398 | } | |
3399 | return count; | |
a2fb1b05 ILT |
3400 | } |
3401 | ||
1c4f3631 ILT |
3402 | // Return the section attached to the list segment with the lowest |
3403 | // load address. This is used when handling a PHDRS clause in a | |
3404 | // linker script. | |
3405 | ||
3406 | Output_section* | |
3407 | Output_segment::section_with_lowest_load_address() const | |
3408 | { | |
3409 | Output_section* found = NULL; | |
3410 | uint64_t found_lma = 0; | |
3411 | this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma); | |
3412 | ||
3413 | Output_section* found_data = found; | |
3414 | this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma); | |
3415 | if (found != found_data && found_data != NULL) | |
3416 | { | |
3417 | gold_error(_("nobits section %s may not precede progbits section %s " | |
3418 | "in same segment"), | |
3419 | found->name(), found_data->name()); | |
3420 | return NULL; | |
3421 | } | |
3422 | ||
3423 | return found; | |
3424 | } | |
3425 | ||
3426 | // Look through a list for a section with a lower load address. | |
3427 | ||
3428 | void | |
3429 | Output_segment::lowest_load_address_in_list(const Output_data_list* pdl, | |
3430 | Output_section** found, | |
3431 | uint64_t* found_lma) const | |
3432 | { | |
3433 | for (Output_data_list::const_iterator p = pdl->begin(); | |
3434 | p != pdl->end(); | |
3435 | ++p) | |
3436 | { | |
3437 | if (!(*p)->is_section()) | |
3438 | continue; | |
3439 | Output_section* os = static_cast<Output_section*>(*p); | |
3440 | uint64_t lma = (os->has_load_address() | |
3441 | ? os->load_address() | |
3442 | : os->address()); | |
3443 | if (*found == NULL || lma < *found_lma) | |
3444 | { | |
3445 | *found = os; | |
3446 | *found_lma = lma; | |
3447 | } | |
3448 | } | |
3449 | } | |
3450 | ||
61ba1cf9 ILT |
3451 | // Write the segment data into *OPHDR. |
3452 | ||
3453 | template<int size, bool big_endian> | |
3454 | void | |
ead1e424 | 3455 | Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr) |
61ba1cf9 ILT |
3456 | { |
3457 | ophdr->put_p_type(this->type_); | |
3458 | ophdr->put_p_offset(this->offset_); | |
3459 | ophdr->put_p_vaddr(this->vaddr_); | |
3460 | ophdr->put_p_paddr(this->paddr_); | |
3461 | ophdr->put_p_filesz(this->filesz_); | |
3462 | ophdr->put_p_memsz(this->memsz_); | |
3463 | ophdr->put_p_flags(this->flags_); | |
a445fddf | 3464 | ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment())); |
61ba1cf9 ILT |
3465 | } |
3466 | ||
3467 | // Write the section headers into V. | |
3468 | ||
3469 | template<int size, bool big_endian> | |
3470 | unsigned char* | |
16649710 ILT |
3471 | Output_segment::write_section_headers(const Layout* layout, |
3472 | const Stringpool* secnamepool, | |
ead1e424 | 3473 | unsigned char* v, |
7d1a9ebb | 3474 | unsigned int *pshndx) const |
5482377d | 3475 | { |
ead1e424 ILT |
3476 | // Every section that is attached to a segment must be attached to a |
3477 | // PT_LOAD segment, so we only write out section headers for PT_LOAD | |
3478 | // segments. | |
3479 | if (this->type_ != elfcpp::PT_LOAD) | |
3480 | return v; | |
3481 | ||
7d1a9ebb ILT |
3482 | v = this->write_section_headers_list<size, big_endian>(layout, secnamepool, |
3483 | &this->output_data_, | |
3484 | v, pshndx); | |
3485 | v = this->write_section_headers_list<size, big_endian>(layout, secnamepool, | |
3486 | &this->output_bss_, | |
3487 | v, pshndx); | |
61ba1cf9 ILT |
3488 | return v; |
3489 | } | |
3490 | ||
3491 | template<int size, bool big_endian> | |
3492 | unsigned char* | |
16649710 ILT |
3493 | Output_segment::write_section_headers_list(const Layout* layout, |
3494 | const Stringpool* secnamepool, | |
61ba1cf9 | 3495 | const Output_data_list* pdl, |
ead1e424 | 3496 | unsigned char* v, |
7d1a9ebb | 3497 | unsigned int* pshndx) const |
61ba1cf9 ILT |
3498 | { |
3499 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
3500 | for (Output_data_list::const_iterator p = pdl->begin(); | |
3501 | p != pdl->end(); | |
3502 | ++p) | |
3503 | { | |
3504 | if ((*p)->is_section()) | |
3505 | { | |
5482377d | 3506 | const Output_section* ps = static_cast<const Output_section*>(*p); |
a3ad94ed | 3507 | gold_assert(*pshndx == ps->out_shndx()); |
61ba1cf9 | 3508 | elfcpp::Shdr_write<size, big_endian> oshdr(v); |
16649710 | 3509 | ps->write_header(layout, secnamepool, &oshdr); |
61ba1cf9 | 3510 | v += shdr_size; |
ead1e424 | 3511 | ++*pshndx; |
61ba1cf9 ILT |
3512 | } |
3513 | } | |
3514 | return v; | |
3515 | } | |
3516 | ||
7d9e3d98 ILT |
3517 | // Print the output sections to the map file. |
3518 | ||
3519 | void | |
3520 | Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const | |
3521 | { | |
3522 | if (this->type() != elfcpp::PT_LOAD) | |
3523 | return; | |
3524 | this->print_section_list_to_mapfile(mapfile, &this->output_data_); | |
3525 | this->print_section_list_to_mapfile(mapfile, &this->output_bss_); | |
3526 | } | |
3527 | ||
3528 | // Print an output section list to the map file. | |
3529 | ||
3530 | void | |
3531 | Output_segment::print_section_list_to_mapfile(Mapfile* mapfile, | |
3532 | const Output_data_list* pdl) const | |
3533 | { | |
3534 | for (Output_data_list::const_iterator p = pdl->begin(); | |
3535 | p != pdl->end(); | |
3536 | ++p) | |
3537 | (*p)->print_to_mapfile(mapfile); | |
3538 | } | |
3539 | ||
a2fb1b05 ILT |
3540 | // Output_file methods. |
3541 | ||
14144f39 ILT |
3542 | Output_file::Output_file(const char* name) |
3543 | : name_(name), | |
61ba1cf9 ILT |
3544 | o_(-1), |
3545 | file_size_(0), | |
c420411f | 3546 | base_(NULL), |
516cb3d0 ILT |
3547 | map_is_anonymous_(false), |
3548 | is_temporary_(false) | |
61ba1cf9 ILT |
3549 | { |
3550 | } | |
3551 | ||
404c2abb ILT |
3552 | // Try to open an existing file. Returns false if the file doesn't |
3553 | // exist, has a size of 0 or can't be mmapped. | |
3554 | ||
3555 | bool | |
3556 | Output_file::open_for_modification() | |
3557 | { | |
3558 | // The name "-" means "stdout". | |
3559 | if (strcmp(this->name_, "-") == 0) | |
3560 | return false; | |
3561 | ||
3562 | // Don't bother opening files with a size of zero. | |
3563 | struct stat s; | |
3564 | if (::stat(this->name_, &s) != 0 || s.st_size == 0) | |
3565 | return false; | |
3566 | ||
3567 | int o = open_descriptor(-1, this->name_, O_RDWR, 0); | |
3568 | if (o < 0) | |
3569 | gold_fatal(_("%s: open: %s"), this->name_, strerror(errno)); | |
3570 | this->o_ = o; | |
3571 | this->file_size_ = s.st_size; | |
3572 | ||
3573 | // If the file can't be mmapped, copying the content to an anonymous | |
3574 | // map will probably negate the performance benefits of incremental | |
3575 | // linking. This could be helped by using views and loading only | |
3576 | // the necessary parts, but this is not supported as of now. | |
3577 | if (!this->map_no_anonymous()) | |
3578 | { | |
3579 | release_descriptor(o, true); | |
3580 | this->o_ = -1; | |
3581 | this->file_size_ = 0; | |
3582 | return false; | |
3583 | } | |
3584 | ||
3585 | return true; | |
3586 | } | |
3587 | ||
61ba1cf9 ILT |
3588 | // Open the output file. |
3589 | ||
a2fb1b05 | 3590 | void |
61ba1cf9 | 3591 | Output_file::open(off_t file_size) |
a2fb1b05 | 3592 | { |
61ba1cf9 ILT |
3593 | this->file_size_ = file_size; |
3594 | ||
4e9d8586 ILT |
3595 | // Unlink the file first; otherwise the open() may fail if the file |
3596 | // is busy (e.g. it's an executable that's currently being executed). | |
3597 | // | |
3598 | // However, the linker may be part of a system where a zero-length | |
3599 | // file is created for it to write to, with tight permissions (gcc | |
3600 | // 2.95 did something like this). Unlinking the file would work | |
3601 | // around those permission controls, so we only unlink if the file | |
3602 | // has a non-zero size. We also unlink only regular files to avoid | |
3603 | // trouble with directories/etc. | |
3604 | // | |
3605 | // If we fail, continue; this command is merely a best-effort attempt | |
3606 | // to improve the odds for open(). | |
3607 | ||
42a1b686 | 3608 | // We let the name "-" mean "stdout" |
516cb3d0 | 3609 | if (!this->is_temporary_) |
42a1b686 | 3610 | { |
516cb3d0 ILT |
3611 | if (strcmp(this->name_, "-") == 0) |
3612 | this->o_ = STDOUT_FILENO; | |
3613 | else | |
3614 | { | |
3615 | struct stat s; | |
6a89f575 CC |
3616 | if (::stat(this->name_, &s) == 0 |
3617 | && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode))) | |
3618 | { | |
3619 | if (s.st_size != 0) | |
3620 | ::unlink(this->name_); | |
3621 | else if (!parameters->options().relocatable()) | |
3622 | { | |
3623 | // If we don't unlink the existing file, add execute | |
3624 | // permission where read permissions already exist | |
3625 | // and where the umask permits. | |
3626 | int mask = ::umask(0); | |
3627 | ::umask(mask); | |
3628 | s.st_mode |= (s.st_mode & 0444) >> 2; | |
3629 | ::chmod(this->name_, s.st_mode & ~mask); | |
3630 | } | |
3631 | } | |
516cb3d0 | 3632 | |
8851ecca | 3633 | int mode = parameters->options().relocatable() ? 0666 : 0777; |
2a00e4fb ILT |
3634 | int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC, |
3635 | mode); | |
516cb3d0 ILT |
3636 | if (o < 0) |
3637 | gold_fatal(_("%s: open: %s"), this->name_, strerror(errno)); | |
3638 | this->o_ = o; | |
3639 | } | |
42a1b686 | 3640 | } |
61ba1cf9 | 3641 | |
27bc2bce ILT |
3642 | this->map(); |
3643 | } | |
3644 | ||
3645 | // Resize the output file. | |
3646 | ||
3647 | void | |
3648 | Output_file::resize(off_t file_size) | |
3649 | { | |
c420411f ILT |
3650 | // If the mmap is mapping an anonymous memory buffer, this is easy: |
3651 | // just mremap to the new size. If it's mapping to a file, we want | |
3652 | // to unmap to flush to the file, then remap after growing the file. | |
3653 | if (this->map_is_anonymous_) | |
3654 | { | |
3655 | void* base = ::mremap(this->base_, this->file_size_, file_size, | |
3656 | MREMAP_MAYMOVE); | |
3657 | if (base == MAP_FAILED) | |
3658 | gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno)); | |
3659 | this->base_ = static_cast<unsigned char*>(base); | |
3660 | this->file_size_ = file_size; | |
3661 | } | |
3662 | else | |
3663 | { | |
3664 | this->unmap(); | |
3665 | this->file_size_ = file_size; | |
fdcac5af ILT |
3666 | if (!this->map_no_anonymous()) |
3667 | gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno)); | |
c420411f | 3668 | } |
27bc2bce ILT |
3669 | } |
3670 | ||
404c2abb ILT |
3671 | // Map an anonymous block of memory which will later be written to the |
3672 | // file. Return whether the map succeeded. | |
26736d8e | 3673 | |
404c2abb | 3674 | bool |
26736d8e ILT |
3675 | Output_file::map_anonymous() |
3676 | { | |
404c2abb ILT |
3677 | void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE, |
3678 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
3679 | if (base != MAP_FAILED) | |
3680 | { | |
3681 | this->map_is_anonymous_ = true; | |
3682 | this->base_ = static_cast<unsigned char*>(base); | |
3683 | return true; | |
3684 | } | |
3685 | return false; | |
26736d8e ILT |
3686 | } |
3687 | ||
404c2abb | 3688 | // Map the file into memory. Return whether the mapping succeeded. |
27bc2bce | 3689 | |
404c2abb ILT |
3690 | bool |
3691 | Output_file::map_no_anonymous() | |
27bc2bce | 3692 | { |
c420411f | 3693 | const int o = this->o_; |
61ba1cf9 | 3694 | |
c420411f ILT |
3695 | // If the output file is not a regular file, don't try to mmap it; |
3696 | // instead, we'll mmap a block of memory (an anonymous buffer), and | |
3697 | // then later write the buffer to the file. | |
3698 | void* base; | |
3699 | struct stat statbuf; | |
42a1b686 ILT |
3700 | if (o == STDOUT_FILENO || o == STDERR_FILENO |
3701 | || ::fstat(o, &statbuf) != 0 | |
516cb3d0 ILT |
3702 | || !S_ISREG(statbuf.st_mode) |
3703 | || this->is_temporary_) | |
404c2abb ILT |
3704 | return false; |
3705 | ||
3706 | // Ensure that we have disk space available for the file. If we | |
3707 | // don't do this, it is possible that we will call munmap, close, | |
3708 | // and exit with dirty buffers still in the cache with no assigned | |
3709 | // disk blocks. If the disk is out of space at that point, the | |
3710 | // output file will wind up incomplete, but we will have already | |
3711 | // exited. The alternative to fallocate would be to use fdatasync, | |
3712 | // but that would be a more significant performance hit. | |
3713 | if (::posix_fallocate(o, 0, this->file_size_) < 0) | |
3714 | gold_fatal(_("%s: %s"), this->name_, strerror(errno)); | |
3715 | ||
3716 | // Map the file into memory. | |
3717 | base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE, | |
3718 | MAP_SHARED, o, 0); | |
3719 | ||
3720 | // The mmap call might fail because of file system issues: the file | |
3721 | // system might not support mmap at all, or it might not support | |
3722 | // mmap with PROT_WRITE. | |
61ba1cf9 | 3723 | if (base == MAP_FAILED) |
404c2abb ILT |
3724 | return false; |
3725 | ||
3726 | this->map_is_anonymous_ = false; | |
61ba1cf9 | 3727 | this->base_ = static_cast<unsigned char*>(base); |
404c2abb ILT |
3728 | return true; |
3729 | } | |
3730 | ||
3731 | // Map the file into memory. | |
3732 | ||
3733 | void | |
3734 | Output_file::map() | |
3735 | { | |
3736 | if (this->map_no_anonymous()) | |
3737 | return; | |
3738 | ||
3739 | // The mmap call might fail because of file system issues: the file | |
3740 | // system might not support mmap at all, or it might not support | |
3741 | // mmap with PROT_WRITE. I'm not sure which errno values we will | |
3742 | // see in all cases, so if the mmap fails for any reason and we | |
3743 | // don't care about file contents, try for an anonymous map. | |
3744 | if (this->map_anonymous()) | |
3745 | return; | |
3746 | ||
3747 | gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"), | |
3748 | this->name_, static_cast<unsigned long>(this->file_size_), | |
3749 | strerror(errno)); | |
61ba1cf9 ILT |
3750 | } |
3751 | ||
c420411f | 3752 | // Unmap the file from memory. |
61ba1cf9 ILT |
3753 | |
3754 | void | |
c420411f | 3755 | Output_file::unmap() |
61ba1cf9 ILT |
3756 | { |
3757 | if (::munmap(this->base_, this->file_size_) < 0) | |
a0c4fb0a | 3758 | gold_error(_("%s: munmap: %s"), this->name_, strerror(errno)); |
61ba1cf9 | 3759 | this->base_ = NULL; |
c420411f ILT |
3760 | } |
3761 | ||
3762 | // Close the output file. | |
3763 | ||
3764 | void | |
3765 | Output_file::close() | |
3766 | { | |
3767 | // If the map isn't file-backed, we need to write it now. | |
516cb3d0 | 3768 | if (this->map_is_anonymous_ && !this->is_temporary_) |
c420411f ILT |
3769 | { |
3770 | size_t bytes_to_write = this->file_size_; | |
6d1e3092 | 3771 | size_t offset = 0; |
c420411f ILT |
3772 | while (bytes_to_write > 0) |
3773 | { | |
6d1e3092 CD |
3774 | ssize_t bytes_written = ::write(this->o_, this->base_ + offset, |
3775 | bytes_to_write); | |
c420411f ILT |
3776 | if (bytes_written == 0) |
3777 | gold_error(_("%s: write: unexpected 0 return-value"), this->name_); | |
3778 | else if (bytes_written < 0) | |
3779 | gold_error(_("%s: write: %s"), this->name_, strerror(errno)); | |
3780 | else | |
6d1e3092 CD |
3781 | { |
3782 | bytes_to_write -= bytes_written; | |
3783 | offset += bytes_written; | |
3784 | } | |
c420411f ILT |
3785 | } |
3786 | } | |
3787 | this->unmap(); | |
61ba1cf9 | 3788 | |
42a1b686 | 3789 | // We don't close stdout or stderr |
516cb3d0 ILT |
3790 | if (this->o_ != STDOUT_FILENO |
3791 | && this->o_ != STDERR_FILENO | |
3792 | && !this->is_temporary_) | |
42a1b686 ILT |
3793 | if (::close(this->o_) < 0) |
3794 | gold_error(_("%s: close: %s"), this->name_, strerror(errno)); | |
61ba1cf9 | 3795 | this->o_ = -1; |
a2fb1b05 ILT |
3796 | } |
3797 | ||
3798 | // Instantiate the templates we need. We could use the configure | |
3799 | // script to restrict this to only the ones for implemented targets. | |
3800 | ||
193a53d9 | 3801 | #ifdef HAVE_TARGET_32_LITTLE |
a2fb1b05 ILT |
3802 | template |
3803 | off_t | |
3804 | Output_section::add_input_section<32, false>( | |
730cdc88 | 3805 | Sized_relobj<32, false>* object, |
ead1e424 | 3806 | unsigned int shndx, |
a2fb1b05 | 3807 | const char* secname, |
730cdc88 | 3808 | const elfcpp::Shdr<32, false>& shdr, |
a445fddf ILT |
3809 | unsigned int reloc_shndx, |
3810 | bool have_sections_script); | |
193a53d9 | 3811 | #endif |
a2fb1b05 | 3812 | |
193a53d9 | 3813 | #ifdef HAVE_TARGET_32_BIG |
a2fb1b05 ILT |
3814 | template |
3815 | off_t | |
3816 | Output_section::add_input_section<32, true>( | |
730cdc88 | 3817 | Sized_relobj<32, true>* object, |
ead1e424 | 3818 | unsigned int shndx, |
a2fb1b05 | 3819 | const char* secname, |
730cdc88 | 3820 | const elfcpp::Shdr<32, true>& shdr, |
a445fddf ILT |
3821 | unsigned int reloc_shndx, |
3822 | bool have_sections_script); | |
193a53d9 | 3823 | #endif |
a2fb1b05 | 3824 | |
193a53d9 | 3825 | #ifdef HAVE_TARGET_64_LITTLE |
a2fb1b05 ILT |
3826 | template |
3827 | off_t | |
3828 | Output_section::add_input_section<64, false>( | |
730cdc88 | 3829 | Sized_relobj<64, false>* object, |
ead1e424 | 3830 | unsigned int shndx, |
a2fb1b05 | 3831 | const char* secname, |
730cdc88 | 3832 | const elfcpp::Shdr<64, false>& shdr, |
a445fddf ILT |
3833 | unsigned int reloc_shndx, |
3834 | bool have_sections_script); | |
193a53d9 | 3835 | #endif |
a2fb1b05 | 3836 | |
193a53d9 | 3837 | #ifdef HAVE_TARGET_64_BIG |
a2fb1b05 ILT |
3838 | template |
3839 | off_t | |
3840 | Output_section::add_input_section<64, true>( | |
730cdc88 | 3841 | Sized_relobj<64, true>* object, |
ead1e424 | 3842 | unsigned int shndx, |
a2fb1b05 | 3843 | const char* secname, |
730cdc88 | 3844 | const elfcpp::Shdr<64, true>& shdr, |
a445fddf ILT |
3845 | unsigned int reloc_shndx, |
3846 | bool have_sections_script); | |
193a53d9 | 3847 | #endif |
a2fb1b05 | 3848 | |
bbbfea06 CC |
3849 | #ifdef HAVE_TARGET_32_LITTLE |
3850 | template | |
3851 | class Output_reloc<elfcpp::SHT_REL, false, 32, false>; | |
3852 | #endif | |
3853 | ||
3854 | #ifdef HAVE_TARGET_32_BIG | |
3855 | template | |
3856 | class Output_reloc<elfcpp::SHT_REL, false, 32, true>; | |
3857 | #endif | |
3858 | ||
3859 | #ifdef HAVE_TARGET_64_LITTLE | |
3860 | template | |
3861 | class Output_reloc<elfcpp::SHT_REL, false, 64, false>; | |
3862 | #endif | |
3863 | ||
3864 | #ifdef HAVE_TARGET_64_BIG | |
3865 | template | |
3866 | class Output_reloc<elfcpp::SHT_REL, false, 64, true>; | |
3867 | #endif | |
3868 | ||
3869 | #ifdef HAVE_TARGET_32_LITTLE | |
3870 | template | |
3871 | class Output_reloc<elfcpp::SHT_REL, true, 32, false>; | |
3872 | #endif | |
3873 | ||
3874 | #ifdef HAVE_TARGET_32_BIG | |
3875 | template | |
3876 | class Output_reloc<elfcpp::SHT_REL, true, 32, true>; | |
3877 | #endif | |
3878 | ||
3879 | #ifdef HAVE_TARGET_64_LITTLE | |
3880 | template | |
3881 | class Output_reloc<elfcpp::SHT_REL, true, 64, false>; | |
3882 | #endif | |
3883 | ||
3884 | #ifdef HAVE_TARGET_64_BIG | |
3885 | template | |
3886 | class Output_reloc<elfcpp::SHT_REL, true, 64, true>; | |
3887 | #endif | |
3888 | ||
3889 | #ifdef HAVE_TARGET_32_LITTLE | |
3890 | template | |
3891 | class Output_reloc<elfcpp::SHT_RELA, false, 32, false>; | |
3892 | #endif | |
3893 | ||
3894 | #ifdef HAVE_TARGET_32_BIG | |
3895 | template | |
3896 | class Output_reloc<elfcpp::SHT_RELA, false, 32, true>; | |
3897 | #endif | |
3898 | ||
3899 | #ifdef HAVE_TARGET_64_LITTLE | |
3900 | template | |
3901 | class Output_reloc<elfcpp::SHT_RELA, false, 64, false>; | |
3902 | #endif | |
3903 | ||
3904 | #ifdef HAVE_TARGET_64_BIG | |
3905 | template | |
3906 | class Output_reloc<elfcpp::SHT_RELA, false, 64, true>; | |
3907 | #endif | |
3908 | ||
3909 | #ifdef HAVE_TARGET_32_LITTLE | |
3910 | template | |
3911 | class Output_reloc<elfcpp::SHT_RELA, true, 32, false>; | |
3912 | #endif | |
3913 | ||
3914 | #ifdef HAVE_TARGET_32_BIG | |
3915 | template | |
3916 | class Output_reloc<elfcpp::SHT_RELA, true, 32, true>; | |
3917 | #endif | |
3918 | ||
3919 | #ifdef HAVE_TARGET_64_LITTLE | |
3920 | template | |
3921 | class Output_reloc<elfcpp::SHT_RELA, true, 64, false>; | |
3922 | #endif | |
3923 | ||
3924 | #ifdef HAVE_TARGET_64_BIG | |
3925 | template | |
3926 | class Output_reloc<elfcpp::SHT_RELA, true, 64, true>; | |
3927 | #endif | |
3928 | ||
193a53d9 | 3929 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
3930 | template |
3931 | class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>; | |
193a53d9 | 3932 | #endif |
c06b7b0b | 3933 | |
193a53d9 | 3934 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
3935 | template |
3936 | class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>; | |
193a53d9 | 3937 | #endif |
c06b7b0b | 3938 | |
193a53d9 | 3939 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
3940 | template |
3941 | class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>; | |
193a53d9 | 3942 | #endif |
c06b7b0b | 3943 | |
193a53d9 | 3944 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
3945 | template |
3946 | class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>; | |
193a53d9 | 3947 | #endif |
c06b7b0b | 3948 | |
193a53d9 | 3949 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
3950 | template |
3951 | class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>; | |
193a53d9 | 3952 | #endif |
c06b7b0b | 3953 | |
193a53d9 | 3954 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
3955 | template |
3956 | class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>; | |
193a53d9 | 3957 | #endif |
c06b7b0b | 3958 | |
193a53d9 | 3959 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
3960 | template |
3961 | class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>; | |
193a53d9 | 3962 | #endif |
c06b7b0b | 3963 | |
193a53d9 | 3964 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
3965 | template |
3966 | class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>; | |
193a53d9 | 3967 | #endif |
c06b7b0b | 3968 | |
193a53d9 | 3969 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
3970 | template |
3971 | class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>; | |
193a53d9 | 3972 | #endif |
c06b7b0b | 3973 | |
193a53d9 | 3974 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
3975 | template |
3976 | class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>; | |
193a53d9 | 3977 | #endif |
c06b7b0b | 3978 | |
193a53d9 | 3979 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
3980 | template |
3981 | class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>; | |
193a53d9 | 3982 | #endif |
c06b7b0b | 3983 | |
193a53d9 | 3984 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
3985 | template |
3986 | class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>; | |
193a53d9 | 3987 | #endif |
c06b7b0b | 3988 | |
193a53d9 | 3989 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
3990 | template |
3991 | class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>; | |
193a53d9 | 3992 | #endif |
c06b7b0b | 3993 | |
193a53d9 | 3994 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
3995 | template |
3996 | class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>; | |
193a53d9 | 3997 | #endif |
c06b7b0b | 3998 | |
193a53d9 | 3999 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
4000 | template |
4001 | class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>; | |
193a53d9 | 4002 | #endif |
c06b7b0b | 4003 | |
193a53d9 | 4004 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
4005 | template |
4006 | class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>; | |
193a53d9 | 4007 | #endif |
c06b7b0b | 4008 | |
6a74a719 ILT |
4009 | #ifdef HAVE_TARGET_32_LITTLE |
4010 | template | |
4011 | class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>; | |
4012 | #endif | |
4013 | ||
4014 | #ifdef HAVE_TARGET_32_BIG | |
4015 | template | |
4016 | class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>; | |
4017 | #endif | |
4018 | ||
4019 | #ifdef HAVE_TARGET_64_LITTLE | |
4020 | template | |
4021 | class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>; | |
4022 | #endif | |
4023 | ||
4024 | #ifdef HAVE_TARGET_64_BIG | |
4025 | template | |
4026 | class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>; | |
4027 | #endif | |
4028 | ||
4029 | #ifdef HAVE_TARGET_32_LITTLE | |
4030 | template | |
4031 | class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>; | |
4032 | #endif | |
4033 | ||
4034 | #ifdef HAVE_TARGET_32_BIG | |
4035 | template | |
4036 | class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>; | |
4037 | #endif | |
4038 | ||
4039 | #ifdef HAVE_TARGET_64_LITTLE | |
4040 | template | |
4041 | class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>; | |
4042 | #endif | |
4043 | ||
4044 | #ifdef HAVE_TARGET_64_BIG | |
4045 | template | |
4046 | class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>; | |
4047 | #endif | |
4048 | ||
4049 | #ifdef HAVE_TARGET_32_LITTLE | |
4050 | template | |
4051 | class Output_data_group<32, false>; | |
4052 | #endif | |
4053 | ||
4054 | #ifdef HAVE_TARGET_32_BIG | |
4055 | template | |
4056 | class Output_data_group<32, true>; | |
4057 | #endif | |
4058 | ||
4059 | #ifdef HAVE_TARGET_64_LITTLE | |
4060 | template | |
4061 | class Output_data_group<64, false>; | |
4062 | #endif | |
4063 | ||
4064 | #ifdef HAVE_TARGET_64_BIG | |
4065 | template | |
4066 | class Output_data_group<64, true>; | |
4067 | #endif | |
4068 | ||
193a53d9 | 4069 | #ifdef HAVE_TARGET_32_LITTLE |
ead1e424 | 4070 | template |
dbe717ef | 4071 | class Output_data_got<32, false>; |
193a53d9 | 4072 | #endif |
ead1e424 | 4073 | |
193a53d9 | 4074 | #ifdef HAVE_TARGET_32_BIG |
ead1e424 | 4075 | template |
dbe717ef | 4076 | class Output_data_got<32, true>; |
193a53d9 | 4077 | #endif |
ead1e424 | 4078 | |
193a53d9 | 4079 | #ifdef HAVE_TARGET_64_LITTLE |
ead1e424 | 4080 | template |
dbe717ef | 4081 | class Output_data_got<64, false>; |
193a53d9 | 4082 | #endif |
ead1e424 | 4083 | |
193a53d9 | 4084 | #ifdef HAVE_TARGET_64_BIG |
ead1e424 | 4085 | template |
dbe717ef | 4086 | class Output_data_got<64, true>; |
193a53d9 | 4087 | #endif |
ead1e424 | 4088 | |
a2fb1b05 | 4089 | } // End namespace gold. |