]>
Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // output.cc -- manage the output file for gold |
2 | ||
88597d34 | 3 | // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <[email protected]>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
a2fb1b05 ILT |
23 | #include "gold.h" |
24 | ||
25 | #include <cstdlib> | |
04bf7072 | 26 | #include <cstring> |
61ba1cf9 ILT |
27 | #include <cerrno> |
28 | #include <fcntl.h> | |
29 | #include <unistd.h> | |
4e9d8586 | 30 | #include <sys/stat.h> |
75f65a3e | 31 | #include <algorithm> |
88597d34 ILT |
32 | |
33 | #ifdef HAVE_SYS_MMAN_H | |
34 | #include <sys/mman.h> | |
35 | #endif | |
36 | ||
6a89f575 | 37 | #include "libiberty.h" |
a2fb1b05 | 38 | |
8ea8cd50 | 39 | #include "dwarf.h" |
7e1edb90 | 40 | #include "parameters.h" |
a2fb1b05 | 41 | #include "object.h" |
ead1e424 ILT |
42 | #include "symtab.h" |
43 | #include "reloc.h" | |
b8e6aad9 | 44 | #include "merge.h" |
2a00e4fb | 45 | #include "descriptors.h" |
5393d741 | 46 | #include "layout.h" |
a2fb1b05 ILT |
47 | #include "output.h" |
48 | ||
88597d34 ILT |
49 | // For systems without mmap support. |
50 | #ifndef HAVE_MMAP | |
51 | # define mmap gold_mmap | |
52 | # define munmap gold_munmap | |
53 | # define mremap gold_mremap | |
54 | # ifndef MAP_FAILED | |
55 | # define MAP_FAILED (reinterpret_cast<void*>(-1)) | |
56 | # endif | |
57 | # ifndef PROT_READ | |
58 | # define PROT_READ 0 | |
59 | # endif | |
60 | # ifndef PROT_WRITE | |
61 | # define PROT_WRITE 0 | |
62 | # endif | |
63 | # ifndef MAP_PRIVATE | |
64 | # define MAP_PRIVATE 0 | |
65 | # endif | |
66 | # ifndef MAP_ANONYMOUS | |
67 | # define MAP_ANONYMOUS 0 | |
68 | # endif | |
69 | # ifndef MAP_SHARED | |
70 | # define MAP_SHARED 0 | |
71 | # endif | |
72 | ||
73 | # ifndef ENOSYS | |
74 | # define ENOSYS EINVAL | |
75 | # endif | |
76 | ||
77 | static void * | |
78 | gold_mmap(void *, size_t, int, int, int, off_t) | |
79 | { | |
80 | errno = ENOSYS; | |
81 | return MAP_FAILED; | |
82 | } | |
83 | ||
84 | static int | |
85 | gold_munmap(void *, size_t) | |
86 | { | |
87 | errno = ENOSYS; | |
88 | return -1; | |
89 | } | |
90 | ||
91 | static void * | |
92 | gold_mremap(void *, size_t, size_t, int) | |
93 | { | |
94 | errno = ENOSYS; | |
95 | return MAP_FAILED; | |
96 | } | |
97 | ||
98 | #endif | |
99 | ||
100 | #if defined(HAVE_MMAP) && !defined(HAVE_MREMAP) | |
101 | # define mremap gold_mremap | |
102 | extern "C" void *gold_mremap(void *, size_t, size_t, int); | |
103 | #endif | |
104 | ||
c420411f ILT |
105 | // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS |
106 | #ifndef MAP_ANONYMOUS | |
107 | # define MAP_ANONYMOUS MAP_ANON | |
108 | #endif | |
109 | ||
88597d34 ILT |
110 | #ifndef MREMAP_MAYMOVE |
111 | # define MREMAP_MAYMOVE 1 | |
112 | #endif | |
113 | ||
9201d894 ILT |
114 | #ifndef HAVE_POSIX_FALLOCATE |
115 | // A dummy, non general, version of posix_fallocate. Here we just set | |
116 | // the file size and hope that there is enough disk space. FIXME: We | |
117 | // could allocate disk space by walking block by block and writing a | |
118 | // zero byte into each block. | |
119 | static int | |
120 | posix_fallocate(int o, off_t offset, off_t len) | |
121 | { | |
bab9090f CC |
122 | if (ftruncate(o, offset + len) < 0) |
123 | return errno; | |
124 | return 0; | |
9201d894 ILT |
125 | } |
126 | #endif // !defined(HAVE_POSIX_FALLOCATE) | |
127 | ||
d0a9ace3 ILT |
128 | // Mingw does not have S_ISLNK. |
129 | #ifndef S_ISLNK | |
130 | # define S_ISLNK(mode) 0 | |
131 | #endif | |
132 | ||
a2fb1b05 ILT |
133 | namespace gold |
134 | { | |
135 | ||
a3ad94ed ILT |
136 | // Output_data variables. |
137 | ||
27bc2bce | 138 | bool Output_data::allocated_sizes_are_fixed; |
a3ad94ed | 139 | |
a2fb1b05 ILT |
140 | // Output_data methods. |
141 | ||
142 | Output_data::~Output_data() | |
143 | { | |
144 | } | |
145 | ||
730cdc88 ILT |
146 | // Return the default alignment for the target size. |
147 | ||
148 | uint64_t | |
149 | Output_data::default_alignment() | |
150 | { | |
8851ecca ILT |
151 | return Output_data::default_alignment_for_size( |
152 | parameters->target().get_size()); | |
730cdc88 ILT |
153 | } |
154 | ||
75f65a3e ILT |
155 | // Return the default alignment for a size--32 or 64. |
156 | ||
157 | uint64_t | |
730cdc88 | 158 | Output_data::default_alignment_for_size(int size) |
75f65a3e ILT |
159 | { |
160 | if (size == 32) | |
161 | return 4; | |
162 | else if (size == 64) | |
163 | return 8; | |
164 | else | |
a3ad94ed | 165 | gold_unreachable(); |
75f65a3e ILT |
166 | } |
167 | ||
75f65a3e ILT |
168 | // Output_section_header methods. This currently assumes that the |
169 | // segment and section lists are complete at construction time. | |
170 | ||
171 | Output_section_headers::Output_section_headers( | |
16649710 ILT |
172 | const Layout* layout, |
173 | const Layout::Segment_list* segment_list, | |
6a74a719 | 174 | const Layout::Section_list* section_list, |
16649710 | 175 | const Layout::Section_list* unattached_section_list, |
d491d34e ILT |
176 | const Stringpool* secnamepool, |
177 | const Output_section* shstrtab_section) | |
9025d29d | 178 | : layout_(layout), |
75f65a3e | 179 | segment_list_(segment_list), |
6a74a719 | 180 | section_list_(section_list), |
a3ad94ed | 181 | unattached_section_list_(unattached_section_list), |
d491d34e ILT |
182 | secnamepool_(secnamepool), |
183 | shstrtab_section_(shstrtab_section) | |
20e6d0d6 DK |
184 | { |
185 | } | |
186 | ||
187 | // Compute the current data size. | |
188 | ||
189 | off_t | |
190 | Output_section_headers::do_size() const | |
75f65a3e | 191 | { |
61ba1cf9 ILT |
192 | // Count all the sections. Start with 1 for the null section. |
193 | off_t count = 1; | |
8851ecca | 194 | if (!parameters->options().relocatable()) |
6a74a719 | 195 | { |
20e6d0d6 DK |
196 | for (Layout::Segment_list::const_iterator p = |
197 | this->segment_list_->begin(); | |
198 | p != this->segment_list_->end(); | |
6a74a719 ILT |
199 | ++p) |
200 | if ((*p)->type() == elfcpp::PT_LOAD) | |
201 | count += (*p)->output_section_count(); | |
202 | } | |
203 | else | |
204 | { | |
20e6d0d6 DK |
205 | for (Layout::Section_list::const_iterator p = |
206 | this->section_list_->begin(); | |
207 | p != this->section_list_->end(); | |
6a74a719 ILT |
208 | ++p) |
209 | if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0) | |
210 | ++count; | |
211 | } | |
20e6d0d6 | 212 | count += this->unattached_section_list_->size(); |
75f65a3e | 213 | |
8851ecca | 214 | const int size = parameters->target().get_size(); |
75f65a3e ILT |
215 | int shdr_size; |
216 | if (size == 32) | |
217 | shdr_size = elfcpp::Elf_sizes<32>::shdr_size; | |
218 | else if (size == 64) | |
219 | shdr_size = elfcpp::Elf_sizes<64>::shdr_size; | |
220 | else | |
a3ad94ed | 221 | gold_unreachable(); |
75f65a3e | 222 | |
20e6d0d6 | 223 | return count * shdr_size; |
75f65a3e ILT |
224 | } |
225 | ||
61ba1cf9 ILT |
226 | // Write out the section headers. |
227 | ||
75f65a3e | 228 | void |
61ba1cf9 | 229 | Output_section_headers::do_write(Output_file* of) |
a2fb1b05 | 230 | { |
8851ecca | 231 | switch (parameters->size_and_endianness()) |
61ba1cf9 | 232 | { |
9025d29d | 233 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
234 | case Parameters::TARGET_32_LITTLE: |
235 | this->do_sized_write<32, false>(of); | |
236 | break; | |
9025d29d | 237 | #endif |
8851ecca ILT |
238 | #ifdef HAVE_TARGET_32_BIG |
239 | case Parameters::TARGET_32_BIG: | |
240 | this->do_sized_write<32, true>(of); | |
241 | break; | |
9025d29d | 242 | #endif |
9025d29d | 243 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
244 | case Parameters::TARGET_64_LITTLE: |
245 | this->do_sized_write<64, false>(of); | |
246 | break; | |
9025d29d | 247 | #endif |
8851ecca ILT |
248 | #ifdef HAVE_TARGET_64_BIG |
249 | case Parameters::TARGET_64_BIG: | |
250 | this->do_sized_write<64, true>(of); | |
251 | break; | |
252 | #endif | |
253 | default: | |
254 | gold_unreachable(); | |
61ba1cf9 | 255 | } |
61ba1cf9 ILT |
256 | } |
257 | ||
258 | template<int size, bool big_endian> | |
259 | void | |
260 | Output_section_headers::do_sized_write(Output_file* of) | |
261 | { | |
262 | off_t all_shdrs_size = this->data_size(); | |
263 | unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size); | |
264 | ||
265 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
266 | unsigned char* v = view; | |
267 | ||
268 | { | |
269 | typename elfcpp::Shdr_write<size, big_endian> oshdr(v); | |
270 | oshdr.put_sh_name(0); | |
271 | oshdr.put_sh_type(elfcpp::SHT_NULL); | |
272 | oshdr.put_sh_flags(0); | |
273 | oshdr.put_sh_addr(0); | |
274 | oshdr.put_sh_offset(0); | |
d491d34e ILT |
275 | |
276 | size_t section_count = (this->data_size() | |
277 | / elfcpp::Elf_sizes<size>::shdr_size); | |
278 | if (section_count < elfcpp::SHN_LORESERVE) | |
279 | oshdr.put_sh_size(0); | |
280 | else | |
281 | oshdr.put_sh_size(section_count); | |
282 | ||
283 | unsigned int shstrndx = this->shstrtab_section_->out_shndx(); | |
284 | if (shstrndx < elfcpp::SHN_LORESERVE) | |
285 | oshdr.put_sh_link(0); | |
286 | else | |
287 | oshdr.put_sh_link(shstrndx); | |
288 | ||
5696ab0b ILT |
289 | size_t segment_count = this->segment_list_->size(); |
290 | oshdr.put_sh_info(segment_count >= elfcpp::PN_XNUM ? segment_count : 0); | |
291 | ||
61ba1cf9 ILT |
292 | oshdr.put_sh_addralign(0); |
293 | oshdr.put_sh_entsize(0); | |
294 | } | |
295 | ||
296 | v += shdr_size; | |
297 | ||
6a74a719 | 298 | unsigned int shndx = 1; |
8851ecca | 299 | if (!parameters->options().relocatable()) |
6a74a719 ILT |
300 | { |
301 | for (Layout::Segment_list::const_iterator p = | |
302 | this->segment_list_->begin(); | |
303 | p != this->segment_list_->end(); | |
304 | ++p) | |
305 | v = (*p)->write_section_headers<size, big_endian>(this->layout_, | |
306 | this->secnamepool_, | |
307 | v, | |
308 | &shndx); | |
309 | } | |
310 | else | |
311 | { | |
312 | for (Layout::Section_list::const_iterator p = | |
313 | this->section_list_->begin(); | |
314 | p != this->section_list_->end(); | |
315 | ++p) | |
316 | { | |
317 | // We do unallocated sections below, except that group | |
318 | // sections have to come first. | |
319 | if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0 | |
320 | && (*p)->type() != elfcpp::SHT_GROUP) | |
321 | continue; | |
322 | gold_assert(shndx == (*p)->out_shndx()); | |
323 | elfcpp::Shdr_write<size, big_endian> oshdr(v); | |
324 | (*p)->write_header(this->layout_, this->secnamepool_, &oshdr); | |
325 | v += shdr_size; | |
326 | ++shndx; | |
327 | } | |
328 | } | |
329 | ||
a3ad94ed | 330 | for (Layout::Section_list::const_iterator p = |
16649710 ILT |
331 | this->unattached_section_list_->begin(); |
332 | p != this->unattached_section_list_->end(); | |
61ba1cf9 ILT |
333 | ++p) |
334 | { | |
6a74a719 ILT |
335 | // For a relocatable link, we did unallocated group sections |
336 | // above, since they have to come first. | |
337 | if ((*p)->type() == elfcpp::SHT_GROUP | |
8851ecca | 338 | && parameters->options().relocatable()) |
6a74a719 | 339 | continue; |
a3ad94ed | 340 | gold_assert(shndx == (*p)->out_shndx()); |
61ba1cf9 | 341 | elfcpp::Shdr_write<size, big_endian> oshdr(v); |
16649710 | 342 | (*p)->write_header(this->layout_, this->secnamepool_, &oshdr); |
61ba1cf9 | 343 | v += shdr_size; |
ead1e424 | 344 | ++shndx; |
61ba1cf9 ILT |
345 | } |
346 | ||
347 | of->write_output_view(this->offset(), all_shdrs_size, view); | |
a2fb1b05 ILT |
348 | } |
349 | ||
54dc6425 ILT |
350 | // Output_segment_header methods. |
351 | ||
61ba1cf9 | 352 | Output_segment_headers::Output_segment_headers( |
61ba1cf9 | 353 | const Layout::Segment_list& segment_list) |
9025d29d | 354 | : segment_list_(segment_list) |
61ba1cf9 | 355 | { |
cdc29364 | 356 | this->set_current_data_size_for_child(this->do_size()); |
61ba1cf9 ILT |
357 | } |
358 | ||
54dc6425 | 359 | void |
61ba1cf9 | 360 | Output_segment_headers::do_write(Output_file* of) |
75f65a3e | 361 | { |
8851ecca | 362 | switch (parameters->size_and_endianness()) |
61ba1cf9 | 363 | { |
9025d29d | 364 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
365 | case Parameters::TARGET_32_LITTLE: |
366 | this->do_sized_write<32, false>(of); | |
367 | break; | |
9025d29d | 368 | #endif |
8851ecca ILT |
369 | #ifdef HAVE_TARGET_32_BIG |
370 | case Parameters::TARGET_32_BIG: | |
371 | this->do_sized_write<32, true>(of); | |
372 | break; | |
9025d29d | 373 | #endif |
9025d29d | 374 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
375 | case Parameters::TARGET_64_LITTLE: |
376 | this->do_sized_write<64, false>(of); | |
377 | break; | |
9025d29d | 378 | #endif |
8851ecca ILT |
379 | #ifdef HAVE_TARGET_64_BIG |
380 | case Parameters::TARGET_64_BIG: | |
381 | this->do_sized_write<64, true>(of); | |
382 | break; | |
383 | #endif | |
384 | default: | |
385 | gold_unreachable(); | |
61ba1cf9 | 386 | } |
61ba1cf9 ILT |
387 | } |
388 | ||
389 | template<int size, bool big_endian> | |
390 | void | |
391 | Output_segment_headers::do_sized_write(Output_file* of) | |
392 | { | |
393 | const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size; | |
394 | off_t all_phdrs_size = this->segment_list_.size() * phdr_size; | |
a445fddf | 395 | gold_assert(all_phdrs_size == this->data_size()); |
61ba1cf9 ILT |
396 | unsigned char* view = of->get_output_view(this->offset(), |
397 | all_phdrs_size); | |
398 | unsigned char* v = view; | |
399 | for (Layout::Segment_list::const_iterator p = this->segment_list_.begin(); | |
400 | p != this->segment_list_.end(); | |
401 | ++p) | |
402 | { | |
403 | elfcpp::Phdr_write<size, big_endian> ophdr(v); | |
404 | (*p)->write_header(&ophdr); | |
405 | v += phdr_size; | |
406 | } | |
407 | ||
a445fddf ILT |
408 | gold_assert(v - view == all_phdrs_size); |
409 | ||
61ba1cf9 | 410 | of->write_output_view(this->offset(), all_phdrs_size, view); |
75f65a3e ILT |
411 | } |
412 | ||
20e6d0d6 DK |
413 | off_t |
414 | Output_segment_headers::do_size() const | |
415 | { | |
416 | const int size = parameters->target().get_size(); | |
417 | int phdr_size; | |
418 | if (size == 32) | |
419 | phdr_size = elfcpp::Elf_sizes<32>::phdr_size; | |
420 | else if (size == 64) | |
421 | phdr_size = elfcpp::Elf_sizes<64>::phdr_size; | |
422 | else | |
423 | gold_unreachable(); | |
424 | ||
425 | return this->segment_list_.size() * phdr_size; | |
426 | } | |
427 | ||
75f65a3e ILT |
428 | // Output_file_header methods. |
429 | ||
9025d29d | 430 | Output_file_header::Output_file_header(const Target* target, |
75f65a3e | 431 | const Symbol_table* symtab, |
a10ae760 | 432 | const Output_segment_headers* osh) |
9025d29d | 433 | : target_(target), |
75f65a3e | 434 | symtab_(symtab), |
61ba1cf9 | 435 | segment_header_(osh), |
75f65a3e | 436 | section_header_(NULL), |
a10ae760 | 437 | shstrtab_(NULL) |
75f65a3e | 438 | { |
20e6d0d6 | 439 | this->set_data_size(this->do_size()); |
75f65a3e ILT |
440 | } |
441 | ||
442 | // Set the section table information for a file header. | |
443 | ||
444 | void | |
445 | Output_file_header::set_section_info(const Output_section_headers* shdrs, | |
446 | const Output_section* shstrtab) | |
447 | { | |
448 | this->section_header_ = shdrs; | |
449 | this->shstrtab_ = shstrtab; | |
450 | } | |
451 | ||
452 | // Write out the file header. | |
453 | ||
454 | void | |
61ba1cf9 | 455 | Output_file_header::do_write(Output_file* of) |
54dc6425 | 456 | { |
27bc2bce ILT |
457 | gold_assert(this->offset() == 0); |
458 | ||
8851ecca | 459 | switch (parameters->size_and_endianness()) |
61ba1cf9 | 460 | { |
9025d29d | 461 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
462 | case Parameters::TARGET_32_LITTLE: |
463 | this->do_sized_write<32, false>(of); | |
464 | break; | |
9025d29d | 465 | #endif |
8851ecca ILT |
466 | #ifdef HAVE_TARGET_32_BIG |
467 | case Parameters::TARGET_32_BIG: | |
468 | this->do_sized_write<32, true>(of); | |
469 | break; | |
9025d29d | 470 | #endif |
9025d29d | 471 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
472 | case Parameters::TARGET_64_LITTLE: |
473 | this->do_sized_write<64, false>(of); | |
474 | break; | |
9025d29d | 475 | #endif |
8851ecca ILT |
476 | #ifdef HAVE_TARGET_64_BIG |
477 | case Parameters::TARGET_64_BIG: | |
478 | this->do_sized_write<64, true>(of); | |
479 | break; | |
480 | #endif | |
481 | default: | |
482 | gold_unreachable(); | |
61ba1cf9 | 483 | } |
61ba1cf9 ILT |
484 | } |
485 | ||
cc643b88 | 486 | // Write out the file header with appropriate size and endianness. |
61ba1cf9 ILT |
487 | |
488 | template<int size, bool big_endian> | |
489 | void | |
490 | Output_file_header::do_sized_write(Output_file* of) | |
491 | { | |
a3ad94ed | 492 | gold_assert(this->offset() == 0); |
61ba1cf9 ILT |
493 | |
494 | int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; | |
495 | unsigned char* view = of->get_output_view(0, ehdr_size); | |
496 | elfcpp::Ehdr_write<size, big_endian> oehdr(view); | |
497 | ||
498 | unsigned char e_ident[elfcpp::EI_NIDENT]; | |
499 | memset(e_ident, 0, elfcpp::EI_NIDENT); | |
500 | e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0; | |
501 | e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1; | |
502 | e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2; | |
503 | e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3; | |
504 | if (size == 32) | |
505 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32; | |
506 | else if (size == 64) | |
507 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64; | |
508 | else | |
a3ad94ed | 509 | gold_unreachable(); |
61ba1cf9 ILT |
510 | e_ident[elfcpp::EI_DATA] = (big_endian |
511 | ? elfcpp::ELFDATA2MSB | |
512 | : elfcpp::ELFDATA2LSB); | |
513 | e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT; | |
61ba1cf9 ILT |
514 | oehdr.put_e_ident(e_ident); |
515 | ||
516 | elfcpp::ET e_type; | |
8851ecca | 517 | if (parameters->options().relocatable()) |
61ba1cf9 | 518 | e_type = elfcpp::ET_REL; |
374ad285 | 519 | else if (parameters->options().output_is_position_independent()) |
436ca963 | 520 | e_type = elfcpp::ET_DYN; |
61ba1cf9 ILT |
521 | else |
522 | e_type = elfcpp::ET_EXEC; | |
523 | oehdr.put_e_type(e_type); | |
524 | ||
525 | oehdr.put_e_machine(this->target_->machine_code()); | |
526 | oehdr.put_e_version(elfcpp::EV_CURRENT); | |
527 | ||
d391083d | 528 | oehdr.put_e_entry(this->entry<size>()); |
61ba1cf9 | 529 | |
6a74a719 ILT |
530 | if (this->segment_header_ == NULL) |
531 | oehdr.put_e_phoff(0); | |
532 | else | |
533 | oehdr.put_e_phoff(this->segment_header_->offset()); | |
534 | ||
61ba1cf9 | 535 | oehdr.put_e_shoff(this->section_header_->offset()); |
d5b40221 | 536 | oehdr.put_e_flags(this->target_->processor_specific_flags()); |
61ba1cf9 | 537 | oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size); |
6a74a719 ILT |
538 | |
539 | if (this->segment_header_ == NULL) | |
540 | { | |
541 | oehdr.put_e_phentsize(0); | |
542 | oehdr.put_e_phnum(0); | |
543 | } | |
544 | else | |
545 | { | |
546 | oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size); | |
5696ab0b ILT |
547 | size_t phnum = (this->segment_header_->data_size() |
548 | / elfcpp::Elf_sizes<size>::phdr_size); | |
549 | if (phnum > elfcpp::PN_XNUM) | |
550 | phnum = elfcpp::PN_XNUM; | |
551 | oehdr.put_e_phnum(phnum); | |
6a74a719 ILT |
552 | } |
553 | ||
61ba1cf9 | 554 | oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size); |
d491d34e ILT |
555 | size_t section_count = (this->section_header_->data_size() |
556 | / elfcpp::Elf_sizes<size>::shdr_size); | |
557 | ||
558 | if (section_count < elfcpp::SHN_LORESERVE) | |
559 | oehdr.put_e_shnum(this->section_header_->data_size() | |
560 | / elfcpp::Elf_sizes<size>::shdr_size); | |
561 | else | |
562 | oehdr.put_e_shnum(0); | |
563 | ||
564 | unsigned int shstrndx = this->shstrtab_->out_shndx(); | |
565 | if (shstrndx < elfcpp::SHN_LORESERVE) | |
566 | oehdr.put_e_shstrndx(this->shstrtab_->out_shndx()); | |
567 | else | |
568 | oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX); | |
61ba1cf9 | 569 | |
36959681 ILT |
570 | // Let the target adjust the ELF header, e.g., to set EI_OSABI in |
571 | // the e_ident field. | |
572 | parameters->target().adjust_elf_header(view, ehdr_size); | |
573 | ||
61ba1cf9 | 574 | of->write_output_view(0, ehdr_size, view); |
54dc6425 ILT |
575 | } |
576 | ||
a10ae760 | 577 | // Return the value to use for the entry address. |
d391083d ILT |
578 | |
579 | template<int size> | |
580 | typename elfcpp::Elf_types<size>::Elf_Addr | |
581 | Output_file_header::entry() | |
582 | { | |
a10ae760 | 583 | const bool should_issue_warning = (parameters->options().entry() != NULL |
8851ecca ILT |
584 | && !parameters->options().relocatable() |
585 | && !parameters->options().shared()); | |
a10ae760 | 586 | const char* entry = parameters->entry(); |
2ea97941 | 587 | Symbol* sym = this->symtab_->lookup(entry); |
d391083d ILT |
588 | |
589 | typename Sized_symbol<size>::Value_type v; | |
590 | if (sym != NULL) | |
591 | { | |
592 | Sized_symbol<size>* ssym; | |
593 | ssym = this->symtab_->get_sized_symbol<size>(sym); | |
594 | if (!ssym->is_defined() && should_issue_warning) | |
2ea97941 | 595 | gold_warning("entry symbol '%s' exists but is not defined", entry); |
d391083d ILT |
596 | v = ssym->value(); |
597 | } | |
598 | else | |
599 | { | |
600 | // We couldn't find the entry symbol. See if we can parse it as | |
601 | // a number. This supports, e.g., -e 0x1000. | |
602 | char* endptr; | |
2ea97941 | 603 | v = strtoull(entry, &endptr, 0); |
d391083d ILT |
604 | if (*endptr != '\0') |
605 | { | |
606 | if (should_issue_warning) | |
2ea97941 | 607 | gold_warning("cannot find entry symbol '%s'", entry); |
d391083d ILT |
608 | v = 0; |
609 | } | |
610 | } | |
611 | ||
612 | return v; | |
613 | } | |
614 | ||
20e6d0d6 DK |
615 | // Compute the current data size. |
616 | ||
617 | off_t | |
618 | Output_file_header::do_size() const | |
619 | { | |
620 | const int size = parameters->target().get_size(); | |
621 | if (size == 32) | |
622 | return elfcpp::Elf_sizes<32>::ehdr_size; | |
623 | else if (size == 64) | |
624 | return elfcpp::Elf_sizes<64>::ehdr_size; | |
625 | else | |
626 | gold_unreachable(); | |
627 | } | |
628 | ||
dbe717ef ILT |
629 | // Output_data_const methods. |
630 | ||
631 | void | |
a3ad94ed | 632 | Output_data_const::do_write(Output_file* of) |
dbe717ef | 633 | { |
a3ad94ed ILT |
634 | of->write(this->offset(), this->data_.data(), this->data_.size()); |
635 | } | |
636 | ||
637 | // Output_data_const_buffer methods. | |
638 | ||
639 | void | |
640 | Output_data_const_buffer::do_write(Output_file* of) | |
641 | { | |
642 | of->write(this->offset(), this->p_, this->data_size()); | |
dbe717ef ILT |
643 | } |
644 | ||
645 | // Output_section_data methods. | |
646 | ||
16649710 ILT |
647 | // Record the output section, and set the entry size and such. |
648 | ||
649 | void | |
650 | Output_section_data::set_output_section(Output_section* os) | |
651 | { | |
652 | gold_assert(this->output_section_ == NULL); | |
653 | this->output_section_ = os; | |
654 | this->do_adjust_output_section(os); | |
655 | } | |
656 | ||
657 | // Return the section index of the output section. | |
658 | ||
dbe717ef ILT |
659 | unsigned int |
660 | Output_section_data::do_out_shndx() const | |
661 | { | |
a3ad94ed | 662 | gold_assert(this->output_section_ != NULL); |
dbe717ef ILT |
663 | return this->output_section_->out_shndx(); |
664 | } | |
665 | ||
759b1a24 ILT |
666 | // Set the alignment, which means we may need to update the alignment |
667 | // of the output section. | |
668 | ||
669 | void | |
2ea97941 | 670 | Output_section_data::set_addralign(uint64_t addralign) |
759b1a24 | 671 | { |
2ea97941 | 672 | this->addralign_ = addralign; |
759b1a24 | 673 | if (this->output_section_ != NULL |
2ea97941 ILT |
674 | && this->output_section_->addralign() < addralign) |
675 | this->output_section_->set_addralign(addralign); | |
759b1a24 ILT |
676 | } |
677 | ||
a3ad94ed ILT |
678 | // Output_data_strtab methods. |
679 | ||
27bc2bce | 680 | // Set the final data size. |
a3ad94ed ILT |
681 | |
682 | void | |
27bc2bce | 683 | Output_data_strtab::set_final_data_size() |
a3ad94ed ILT |
684 | { |
685 | this->strtab_->set_string_offsets(); | |
686 | this->set_data_size(this->strtab_->get_strtab_size()); | |
687 | } | |
688 | ||
689 | // Write out a string table. | |
690 | ||
691 | void | |
692 | Output_data_strtab::do_write(Output_file* of) | |
693 | { | |
694 | this->strtab_->write(of, this->offset()); | |
695 | } | |
696 | ||
c06b7b0b ILT |
697 | // Output_reloc methods. |
698 | ||
7bf1f802 ILT |
699 | // A reloc against a global symbol. |
700 | ||
701 | template<bool dynamic, int size, bool big_endian> | |
702 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
703 | Symbol* gsym, | |
704 | unsigned int type, | |
705 | Output_data* od, | |
e8c846c3 | 706 | Address address, |
0da6fa6c DM |
707 | bool is_relative, |
708 | bool is_symbolless) | |
7bf1f802 | 709 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), |
0da6fa6c | 710 | is_relative_(is_relative), is_symbolless_(is_symbolless), |
397b129b | 711 | is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE) |
7bf1f802 | 712 | { |
dceae3c1 ILT |
713 | // this->type_ is a bitfield; make sure TYPE fits. |
714 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
715 | this->u1_.gsym = gsym; |
716 | this->u2_.od = od; | |
dceae3c1 ILT |
717 | if (dynamic) |
718 | this->set_needs_dynsym_index(); | |
7bf1f802 ILT |
719 | } |
720 | ||
721 | template<bool dynamic, int size, bool big_endian> | |
722 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
723 | Symbol* gsym, | |
724 | unsigned int type, | |
ef9beddf | 725 | Sized_relobj<size, big_endian>* relobj, |
7bf1f802 | 726 | unsigned int shndx, |
e8c846c3 | 727 | Address address, |
0da6fa6c DM |
728 | bool is_relative, |
729 | bool is_symbolless) | |
7bf1f802 | 730 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), |
0da6fa6c | 731 | is_relative_(is_relative), is_symbolless_(is_symbolless), |
397b129b | 732 | is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx) |
7bf1f802 ILT |
733 | { |
734 | gold_assert(shndx != INVALID_CODE); | |
dceae3c1 ILT |
735 | // this->type_ is a bitfield; make sure TYPE fits. |
736 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
737 | this->u1_.gsym = gsym; |
738 | this->u2_.relobj = relobj; | |
dceae3c1 ILT |
739 | if (dynamic) |
740 | this->set_needs_dynsym_index(); | |
7bf1f802 ILT |
741 | } |
742 | ||
743 | // A reloc against a local symbol. | |
744 | ||
745 | template<bool dynamic, int size, bool big_endian> | |
746 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
747 | Sized_relobj<size, big_endian>* relobj, | |
748 | unsigned int local_sym_index, | |
749 | unsigned int type, | |
750 | Output_data* od, | |
e8c846c3 | 751 | Address address, |
2ea97941 | 752 | bool is_relative, |
0da6fa6c | 753 | bool is_symbolless, |
397b129b CC |
754 | bool is_section_symbol, |
755 | bool use_plt_offset) | |
7bf1f802 | 756 | : address_(address), local_sym_index_(local_sym_index), type_(type), |
0da6fa6c | 757 | is_relative_(is_relative), is_symbolless_(is_symbolless), |
397b129b CC |
758 | is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset), |
759 | shndx_(INVALID_CODE) | |
7bf1f802 ILT |
760 | { |
761 | gold_assert(local_sym_index != GSYM_CODE | |
762 | && local_sym_index != INVALID_CODE); | |
dceae3c1 ILT |
763 | // this->type_ is a bitfield; make sure TYPE fits. |
764 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
765 | this->u1_.relobj = relobj; |
766 | this->u2_.od = od; | |
dceae3c1 ILT |
767 | if (dynamic) |
768 | this->set_needs_dynsym_index(); | |
7bf1f802 ILT |
769 | } |
770 | ||
771 | template<bool dynamic, int size, bool big_endian> | |
772 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
773 | Sized_relobj<size, big_endian>* relobj, | |
774 | unsigned int local_sym_index, | |
775 | unsigned int type, | |
776 | unsigned int shndx, | |
e8c846c3 | 777 | Address address, |
2ea97941 | 778 | bool is_relative, |
0da6fa6c | 779 | bool is_symbolless, |
397b129b CC |
780 | bool is_section_symbol, |
781 | bool use_plt_offset) | |
7bf1f802 | 782 | : address_(address), local_sym_index_(local_sym_index), type_(type), |
0da6fa6c | 783 | is_relative_(is_relative), is_symbolless_(is_symbolless), |
397b129b CC |
784 | is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset), |
785 | shndx_(shndx) | |
7bf1f802 ILT |
786 | { |
787 | gold_assert(local_sym_index != GSYM_CODE | |
788 | && local_sym_index != INVALID_CODE); | |
789 | gold_assert(shndx != INVALID_CODE); | |
dceae3c1 ILT |
790 | // this->type_ is a bitfield; make sure TYPE fits. |
791 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
792 | this->u1_.relobj = relobj; |
793 | this->u2_.relobj = relobj; | |
dceae3c1 ILT |
794 | if (dynamic) |
795 | this->set_needs_dynsym_index(); | |
7bf1f802 ILT |
796 | } |
797 | ||
798 | // A reloc against the STT_SECTION symbol of an output section. | |
799 | ||
800 | template<bool dynamic, int size, bool big_endian> | |
801 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
802 | Output_section* os, | |
803 | unsigned int type, | |
804 | Output_data* od, | |
805 | Address address) | |
806 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), | |
0da6fa6c | 807 | is_relative_(false), is_symbolless_(false), |
397b129b | 808 | is_section_symbol_(true), use_plt_offset_(false), shndx_(INVALID_CODE) |
7bf1f802 | 809 | { |
dceae3c1 ILT |
810 | // this->type_ is a bitfield; make sure TYPE fits. |
811 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
812 | this->u1_.os = os; |
813 | this->u2_.od = od; | |
814 | if (dynamic) | |
dceae3c1 ILT |
815 | this->set_needs_dynsym_index(); |
816 | else | |
817 | os->set_needs_symtab_index(); | |
7bf1f802 ILT |
818 | } |
819 | ||
820 | template<bool dynamic, int size, bool big_endian> | |
821 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
822 | Output_section* os, | |
823 | unsigned int type, | |
ef9beddf | 824 | Sized_relobj<size, big_endian>* relobj, |
7bf1f802 ILT |
825 | unsigned int shndx, |
826 | Address address) | |
827 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), | |
0da6fa6c | 828 | is_relative_(false), is_symbolless_(false), |
397b129b | 829 | is_section_symbol_(true), use_plt_offset_(false), shndx_(shndx) |
7bf1f802 ILT |
830 | { |
831 | gold_assert(shndx != INVALID_CODE); | |
dceae3c1 ILT |
832 | // this->type_ is a bitfield; make sure TYPE fits. |
833 | gold_assert(this->type_ == type); | |
7bf1f802 ILT |
834 | this->u1_.os = os; |
835 | this->u2_.relobj = relobj; | |
836 | if (dynamic) | |
dceae3c1 ILT |
837 | this->set_needs_dynsym_index(); |
838 | else | |
839 | os->set_needs_symtab_index(); | |
840 | } | |
841 | ||
e291e7b9 ILT |
842 | // An absolute relocation. |
843 | ||
844 | template<bool dynamic, int size, bool big_endian> | |
845 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
846 | unsigned int type, | |
847 | Output_data* od, | |
848 | Address address) | |
849 | : address_(address), local_sym_index_(0), type_(type), | |
0da6fa6c | 850 | is_relative_(false), is_symbolless_(false), |
397b129b | 851 | is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE) |
e291e7b9 ILT |
852 | { |
853 | // this->type_ is a bitfield; make sure TYPE fits. | |
854 | gold_assert(this->type_ == type); | |
855 | this->u1_.relobj = NULL; | |
856 | this->u2_.od = od; | |
857 | } | |
858 | ||
859 | template<bool dynamic, int size, bool big_endian> | |
860 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
861 | unsigned int type, | |
862 | Sized_relobj<size, big_endian>* relobj, | |
863 | unsigned int shndx, | |
864 | Address address) | |
865 | : address_(address), local_sym_index_(0), type_(type), | |
0da6fa6c | 866 | is_relative_(false), is_symbolless_(false), |
397b129b | 867 | is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx) |
e291e7b9 ILT |
868 | { |
869 | gold_assert(shndx != INVALID_CODE); | |
870 | // this->type_ is a bitfield; make sure TYPE fits. | |
871 | gold_assert(this->type_ == type); | |
872 | this->u1_.relobj = NULL; | |
873 | this->u2_.relobj = relobj; | |
874 | } | |
875 | ||
876 | // A target specific relocation. | |
877 | ||
878 | template<bool dynamic, int size, bool big_endian> | |
879 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
880 | unsigned int type, | |
881 | void* arg, | |
882 | Output_data* od, | |
883 | Address address) | |
884 | : address_(address), local_sym_index_(TARGET_CODE), type_(type), | |
0da6fa6c | 885 | is_relative_(false), is_symbolless_(false), |
397b129b | 886 | is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE) |
e291e7b9 ILT |
887 | { |
888 | // this->type_ is a bitfield; make sure TYPE fits. | |
889 | gold_assert(this->type_ == type); | |
890 | this->u1_.arg = arg; | |
891 | this->u2_.od = od; | |
892 | } | |
893 | ||
894 | template<bool dynamic, int size, bool big_endian> | |
895 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc( | |
896 | unsigned int type, | |
897 | void* arg, | |
898 | Sized_relobj<size, big_endian>* relobj, | |
899 | unsigned int shndx, | |
900 | Address address) | |
901 | : address_(address), local_sym_index_(TARGET_CODE), type_(type), | |
0da6fa6c | 902 | is_relative_(false), is_symbolless_(false), |
397b129b | 903 | is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx) |
e291e7b9 ILT |
904 | { |
905 | gold_assert(shndx != INVALID_CODE); | |
906 | // this->type_ is a bitfield; make sure TYPE fits. | |
907 | gold_assert(this->type_ == type); | |
908 | this->u1_.arg = arg; | |
909 | this->u2_.relobj = relobj; | |
910 | } | |
911 | ||
dceae3c1 ILT |
912 | // Record that we need a dynamic symbol index for this relocation. |
913 | ||
914 | template<bool dynamic, int size, bool big_endian> | |
915 | void | |
916 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>:: | |
917 | set_needs_dynsym_index() | |
918 | { | |
0da6fa6c | 919 | if (this->is_symbolless_) |
dceae3c1 ILT |
920 | return; |
921 | switch (this->local_sym_index_) | |
922 | { | |
923 | case INVALID_CODE: | |
924 | gold_unreachable(); | |
925 | ||
926 | case GSYM_CODE: | |
927 | this->u1_.gsym->set_needs_dynsym_entry(); | |
928 | break; | |
929 | ||
930 | case SECTION_CODE: | |
931 | this->u1_.os->set_needs_dynsym_index(); | |
932 | break; | |
933 | ||
e291e7b9 ILT |
934 | case TARGET_CODE: |
935 | // The target must take care of this if necessary. | |
936 | break; | |
937 | ||
dceae3c1 ILT |
938 | case 0: |
939 | break; | |
940 | ||
941 | default: | |
942 | { | |
943 | const unsigned int lsi = this->local_sym_index_; | |
6fa2a40b CC |
944 | Sized_relobj_file<size, big_endian>* relobj = |
945 | this->u1_.relobj->sized_relobj(); | |
946 | gold_assert(relobj != NULL); | |
dceae3c1 | 947 | if (!this->is_section_symbol_) |
6fa2a40b | 948 | relobj->set_needs_output_dynsym_entry(lsi); |
dceae3c1 | 949 | else |
6fa2a40b | 950 | relobj->output_section(lsi)->set_needs_dynsym_index(); |
dceae3c1 ILT |
951 | } |
952 | break; | |
953 | } | |
7bf1f802 ILT |
954 | } |
955 | ||
c06b7b0b ILT |
956 | // Get the symbol index of a relocation. |
957 | ||
958 | template<bool dynamic, int size, bool big_endian> | |
959 | unsigned int | |
960 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index() | |
961 | const | |
962 | { | |
963 | unsigned int index; | |
0da6fa6c DM |
964 | if (this->is_symbolless_) |
965 | return 0; | |
c06b7b0b ILT |
966 | switch (this->local_sym_index_) |
967 | { | |
968 | case INVALID_CODE: | |
a3ad94ed | 969 | gold_unreachable(); |
c06b7b0b ILT |
970 | |
971 | case GSYM_CODE: | |
5a6f7e2d | 972 | if (this->u1_.gsym == NULL) |
c06b7b0b ILT |
973 | index = 0; |
974 | else if (dynamic) | |
5a6f7e2d | 975 | index = this->u1_.gsym->dynsym_index(); |
c06b7b0b | 976 | else |
5a6f7e2d | 977 | index = this->u1_.gsym->symtab_index(); |
c06b7b0b ILT |
978 | break; |
979 | ||
980 | case SECTION_CODE: | |
981 | if (dynamic) | |
5a6f7e2d | 982 | index = this->u1_.os->dynsym_index(); |
c06b7b0b | 983 | else |
5a6f7e2d | 984 | index = this->u1_.os->symtab_index(); |
c06b7b0b ILT |
985 | break; |
986 | ||
e291e7b9 ILT |
987 | case TARGET_CODE: |
988 | index = parameters->target().reloc_symbol_index(this->u1_.arg, | |
989 | this->type_); | |
990 | break; | |
991 | ||
436ca963 ILT |
992 | case 0: |
993 | // Relocations without symbols use a symbol index of 0. | |
994 | index = 0; | |
995 | break; | |
996 | ||
c06b7b0b | 997 | default: |
dceae3c1 ILT |
998 | { |
999 | const unsigned int lsi = this->local_sym_index_; | |
6fa2a40b CC |
1000 | Sized_relobj_file<size, big_endian>* relobj = |
1001 | this->u1_.relobj->sized_relobj(); | |
1002 | gold_assert(relobj != NULL); | |
dceae3c1 ILT |
1003 | if (!this->is_section_symbol_) |
1004 | { | |
1005 | if (dynamic) | |
6fa2a40b | 1006 | index = relobj->dynsym_index(lsi); |
dceae3c1 | 1007 | else |
6fa2a40b | 1008 | index = relobj->symtab_index(lsi); |
dceae3c1 ILT |
1009 | } |
1010 | else | |
1011 | { | |
6fa2a40b | 1012 | Output_section* os = relobj->output_section(lsi); |
dceae3c1 ILT |
1013 | gold_assert(os != NULL); |
1014 | if (dynamic) | |
1015 | index = os->dynsym_index(); | |
1016 | else | |
1017 | index = os->symtab_index(); | |
1018 | } | |
1019 | } | |
c06b7b0b ILT |
1020 | break; |
1021 | } | |
a3ad94ed | 1022 | gold_assert(index != -1U); |
c06b7b0b ILT |
1023 | return index; |
1024 | } | |
1025 | ||
624f8810 ILT |
1026 | // For a local section symbol, get the address of the offset ADDEND |
1027 | // within the input section. | |
dceae3c1 ILT |
1028 | |
1029 | template<bool dynamic, int size, bool big_endian> | |
ef9beddf | 1030 | typename elfcpp::Elf_types<size>::Elf_Addr |
dceae3c1 | 1031 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>:: |
624f8810 | 1032 | local_section_offset(Addend addend) const |
dceae3c1 | 1033 | { |
624f8810 ILT |
1034 | gold_assert(this->local_sym_index_ != GSYM_CODE |
1035 | && this->local_sym_index_ != SECTION_CODE | |
e291e7b9 | 1036 | && this->local_sym_index_ != TARGET_CODE |
624f8810 | 1037 | && this->local_sym_index_ != INVALID_CODE |
e291e7b9 | 1038 | && this->local_sym_index_ != 0 |
624f8810 | 1039 | && this->is_section_symbol_); |
dceae3c1 | 1040 | const unsigned int lsi = this->local_sym_index_; |
ef9beddf | 1041 | Output_section* os = this->u1_.relobj->output_section(lsi); |
624f8810 | 1042 | gold_assert(os != NULL); |
ef9beddf | 1043 | Address offset = this->u1_.relobj->get_output_section_offset(lsi); |
eff45813 | 1044 | if (offset != invalid_address) |
624f8810 ILT |
1045 | return offset + addend; |
1046 | // This is a merge section. | |
6fa2a40b CC |
1047 | Sized_relobj_file<size, big_endian>* relobj = |
1048 | this->u1_.relobj->sized_relobj(); | |
1049 | gold_assert(relobj != NULL); | |
1050 | offset = os->output_address(relobj, lsi, addend); | |
eff45813 | 1051 | gold_assert(offset != invalid_address); |
dceae3c1 ILT |
1052 | return offset; |
1053 | } | |
1054 | ||
d98bc257 | 1055 | // Get the output address of a relocation. |
c06b7b0b ILT |
1056 | |
1057 | template<bool dynamic, int size, bool big_endian> | |
a984ee1d | 1058 | typename elfcpp::Elf_types<size>::Elf_Addr |
d98bc257 | 1059 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const |
c06b7b0b | 1060 | { |
a3ad94ed | 1061 | Address address = this->address_; |
5a6f7e2d ILT |
1062 | if (this->shndx_ != INVALID_CODE) |
1063 | { | |
ef9beddf | 1064 | Output_section* os = this->u2_.relobj->output_section(this->shndx_); |
5a6f7e2d | 1065 | gold_assert(os != NULL); |
ef9beddf | 1066 | Address off = this->u2_.relobj->get_output_section_offset(this->shndx_); |
eff45813 | 1067 | if (off != invalid_address) |
730cdc88 ILT |
1068 | address += os->address() + off; |
1069 | else | |
1070 | { | |
6fa2a40b CC |
1071 | Sized_relobj_file<size, big_endian>* relobj = |
1072 | this->u2_.relobj->sized_relobj(); | |
1073 | gold_assert(relobj != NULL); | |
1074 | address = os->output_address(relobj, this->shndx_, address); | |
eff45813 | 1075 | gold_assert(address != invalid_address); |
730cdc88 | 1076 | } |
5a6f7e2d ILT |
1077 | } |
1078 | else if (this->u2_.od != NULL) | |
1079 | address += this->u2_.od->address(); | |
d98bc257 ILT |
1080 | return address; |
1081 | } | |
1082 | ||
1083 | // Write out the offset and info fields of a Rel or Rela relocation | |
1084 | // entry. | |
1085 | ||
1086 | template<bool dynamic, int size, bool big_endian> | |
1087 | template<typename Write_rel> | |
1088 | void | |
1089 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel( | |
1090 | Write_rel* wr) const | |
1091 | { | |
1092 | wr->put_r_offset(this->get_address()); | |
0da6fa6c | 1093 | unsigned int sym_index = this->get_symbol_index(); |
e8c846c3 | 1094 | wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_)); |
c06b7b0b ILT |
1095 | } |
1096 | ||
1097 | // Write out a Rel relocation. | |
1098 | ||
1099 | template<bool dynamic, int size, bool big_endian> | |
1100 | void | |
1101 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write( | |
1102 | unsigned char* pov) const | |
1103 | { | |
1104 | elfcpp::Rel_write<size, big_endian> orel(pov); | |
1105 | this->write_rel(&orel); | |
1106 | } | |
1107 | ||
e8c846c3 ILT |
1108 | // Get the value of the symbol referred to by a Rel relocation. |
1109 | ||
1110 | template<bool dynamic, int size, bool big_endian> | |
1111 | typename elfcpp::Elf_types<size>::Elf_Addr | |
d1f003c6 | 1112 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value( |
624f8810 | 1113 | Addend addend) const |
e8c846c3 ILT |
1114 | { |
1115 | if (this->local_sym_index_ == GSYM_CODE) | |
1116 | { | |
1117 | const Sized_symbol<size>* sym; | |
1118 | sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym); | |
d1f003c6 | 1119 | return sym->value() + addend; |
e8c846c3 ILT |
1120 | } |
1121 | gold_assert(this->local_sym_index_ != SECTION_CODE | |
e291e7b9 | 1122 | && this->local_sym_index_ != TARGET_CODE |
d1f003c6 | 1123 | && this->local_sym_index_ != INVALID_CODE |
e291e7b9 | 1124 | && this->local_sym_index_ != 0 |
d1f003c6 ILT |
1125 | && !this->is_section_symbol_); |
1126 | const unsigned int lsi = this->local_sym_index_; | |
6fa2a40b CC |
1127 | Sized_relobj_file<size, big_endian>* relobj = |
1128 | this->u1_.relobj->sized_relobj(); | |
1129 | gold_assert(relobj != NULL); | |
397b129b CC |
1130 | if (this->use_plt_offset_) |
1131 | { | |
1132 | uint64_t plt_address = | |
1133 | parameters->target().plt_address_for_local(relobj, lsi); | |
1134 | return plt_address + relobj->local_plt_offset(lsi); | |
1135 | } | |
6fa2a40b CC |
1136 | const Symbol_value<size>* symval = relobj->local_symbol(lsi); |
1137 | return symval->value(relobj, addend); | |
e8c846c3 ILT |
1138 | } |
1139 | ||
d98bc257 ILT |
1140 | // Reloc comparison. This function sorts the dynamic relocs for the |
1141 | // benefit of the dynamic linker. First we sort all relative relocs | |
1142 | // to the front. Among relative relocs, we sort by output address. | |
1143 | // Among non-relative relocs, we sort by symbol index, then by output | |
1144 | // address. | |
1145 | ||
1146 | template<bool dynamic, int size, bool big_endian> | |
1147 | int | |
1148 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>:: | |
1149 | compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2) | |
1150 | const | |
1151 | { | |
1152 | if (this->is_relative_) | |
1153 | { | |
1154 | if (!r2.is_relative_) | |
1155 | return -1; | |
1156 | // Otherwise sort by reloc address below. | |
1157 | } | |
1158 | else if (r2.is_relative_) | |
1159 | return 1; | |
1160 | else | |
1161 | { | |
1162 | unsigned int sym1 = this->get_symbol_index(); | |
1163 | unsigned int sym2 = r2.get_symbol_index(); | |
1164 | if (sym1 < sym2) | |
1165 | return -1; | |
1166 | else if (sym1 > sym2) | |
1167 | return 1; | |
1168 | // Otherwise sort by reloc address. | |
1169 | } | |
1170 | ||
1171 | section_offset_type addr1 = this->get_address(); | |
1172 | section_offset_type addr2 = r2.get_address(); | |
1173 | if (addr1 < addr2) | |
1174 | return -1; | |
1175 | else if (addr1 > addr2) | |
1176 | return 1; | |
1177 | ||
1178 | // Final tie breaker, in order to generate the same output on any | |
1179 | // host: reloc type. | |
1180 | unsigned int type1 = this->type_; | |
1181 | unsigned int type2 = r2.type_; | |
1182 | if (type1 < type2) | |
1183 | return -1; | |
1184 | else if (type1 > type2) | |
1185 | return 1; | |
1186 | ||
1187 | // These relocs appear to be exactly the same. | |
1188 | return 0; | |
1189 | } | |
1190 | ||
c06b7b0b ILT |
1191 | // Write out a Rela relocation. |
1192 | ||
1193 | template<bool dynamic, int size, bool big_endian> | |
1194 | void | |
1195 | Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write( | |
1196 | unsigned char* pov) const | |
1197 | { | |
1198 | elfcpp::Rela_write<size, big_endian> orel(pov); | |
1199 | this->rel_.write_rel(&orel); | |
e8c846c3 | 1200 | Addend addend = this->addend_; |
e291e7b9 ILT |
1201 | if (this->rel_.is_target_specific()) |
1202 | addend = parameters->target().reloc_addend(this->rel_.target_arg(), | |
1203 | this->rel_.type(), addend); | |
0da6fa6c | 1204 | else if (this->rel_.is_symbolless()) |
d1f003c6 ILT |
1205 | addend = this->rel_.symbol_value(addend); |
1206 | else if (this->rel_.is_local_section_symbol()) | |
624f8810 | 1207 | addend = this->rel_.local_section_offset(addend); |
e8c846c3 | 1208 | orel.put_r_addend(addend); |
c06b7b0b ILT |
1209 | } |
1210 | ||
1211 | // Output_data_reloc_base methods. | |
1212 | ||
16649710 ILT |
1213 | // Adjust the output section. |
1214 | ||
1215 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
1216 | void | |
1217 | Output_data_reloc_base<sh_type, dynamic, size, big_endian> | |
1218 | ::do_adjust_output_section(Output_section* os) | |
1219 | { | |
1220 | if (sh_type == elfcpp::SHT_REL) | |
1221 | os->set_entsize(elfcpp::Elf_sizes<size>::rel_size); | |
1222 | else if (sh_type == elfcpp::SHT_RELA) | |
1223 | os->set_entsize(elfcpp::Elf_sizes<size>::rela_size); | |
1224 | else | |
1225 | gold_unreachable(); | |
7223e9ca ILT |
1226 | |
1227 | // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a | |
1228 | // static link. The backends will generate a dynamic reloc section | |
1229 | // to hold this. In that case we don't want to link to the dynsym | |
1230 | // section, because there isn't one. | |
1231 | if (!dynamic) | |
16649710 | 1232 | os->set_should_link_to_symtab(); |
7223e9ca ILT |
1233 | else if (parameters->doing_static_link()) |
1234 | ; | |
1235 | else | |
1236 | os->set_should_link_to_dynsym(); | |
16649710 ILT |
1237 | } |
1238 | ||
c06b7b0b ILT |
1239 | // Write out relocation data. |
1240 | ||
1241 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
1242 | void | |
1243 | Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write( | |
1244 | Output_file* of) | |
1245 | { | |
1246 | const off_t off = this->offset(); | |
1247 | const off_t oview_size = this->data_size(); | |
1248 | unsigned char* const oview = of->get_output_view(off, oview_size); | |
1249 | ||
3a44184e | 1250 | if (this->sort_relocs()) |
d98bc257 ILT |
1251 | { |
1252 | gold_assert(dynamic); | |
1253 | std::sort(this->relocs_.begin(), this->relocs_.end(), | |
1254 | Sort_relocs_comparison()); | |
1255 | } | |
1256 | ||
c06b7b0b ILT |
1257 | unsigned char* pov = oview; |
1258 | for (typename Relocs::const_iterator p = this->relocs_.begin(); | |
1259 | p != this->relocs_.end(); | |
1260 | ++p) | |
1261 | { | |
1262 | p->write(pov); | |
1263 | pov += reloc_size; | |
1264 | } | |
1265 | ||
a3ad94ed | 1266 | gold_assert(pov - oview == oview_size); |
c06b7b0b ILT |
1267 | |
1268 | of->write_output_view(off, oview_size, oview); | |
1269 | ||
1270 | // We no longer need the relocation entries. | |
1271 | this->relocs_.clear(); | |
1272 | } | |
1273 | ||
6a74a719 ILT |
1274 | // Class Output_relocatable_relocs. |
1275 | ||
1276 | template<int sh_type, int size, bool big_endian> | |
1277 | void | |
1278 | Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size() | |
1279 | { | |
1280 | this->set_data_size(this->rr_->output_reloc_count() | |
1281 | * Reloc_types<sh_type, size, big_endian>::reloc_size); | |
1282 | } | |
1283 | ||
1284 | // class Output_data_group. | |
1285 | ||
1286 | template<int size, bool big_endian> | |
1287 | Output_data_group<size, big_endian>::Output_data_group( | |
6fa2a40b | 1288 | Sized_relobj_file<size, big_endian>* relobj, |
6a74a719 | 1289 | section_size_type entry_count, |
8825ac63 ILT |
1290 | elfcpp::Elf_Word flags, |
1291 | std::vector<unsigned int>* input_shndxes) | |
20e6d0d6 | 1292 | : Output_section_data(entry_count * 4, 4, false), |
8825ac63 ILT |
1293 | relobj_(relobj), |
1294 | flags_(flags) | |
6a74a719 | 1295 | { |
8825ac63 | 1296 | this->input_shndxes_.swap(*input_shndxes); |
6a74a719 ILT |
1297 | } |
1298 | ||
1299 | // Write out the section group, which means translating the section | |
1300 | // indexes to apply to the output file. | |
1301 | ||
1302 | template<int size, bool big_endian> | |
1303 | void | |
1304 | Output_data_group<size, big_endian>::do_write(Output_file* of) | |
1305 | { | |
1306 | const off_t off = this->offset(); | |
1307 | const section_size_type oview_size = | |
1308 | convert_to_section_size_type(this->data_size()); | |
1309 | unsigned char* const oview = of->get_output_view(off, oview_size); | |
1310 | ||
1311 | elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview); | |
1312 | elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_); | |
1313 | ++contents; | |
1314 | ||
1315 | for (std::vector<unsigned int>::const_iterator p = | |
8825ac63 ILT |
1316 | this->input_shndxes_.begin(); |
1317 | p != this->input_shndxes_.end(); | |
6a74a719 ILT |
1318 | ++p, ++contents) |
1319 | { | |
ef9beddf | 1320 | Output_section* os = this->relobj_->output_section(*p); |
6a74a719 ILT |
1321 | |
1322 | unsigned int output_shndx; | |
1323 | if (os != NULL) | |
1324 | output_shndx = os->out_shndx(); | |
1325 | else | |
1326 | { | |
1327 | this->relobj_->error(_("section group retained but " | |
1328 | "group element discarded")); | |
1329 | output_shndx = 0; | |
1330 | } | |
1331 | ||
1332 | elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx); | |
1333 | } | |
1334 | ||
1335 | size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview; | |
1336 | gold_assert(wrote == oview_size); | |
1337 | ||
1338 | of->write_output_view(off, oview_size, oview); | |
1339 | ||
1340 | // We no longer need this information. | |
8825ac63 | 1341 | this->input_shndxes_.clear(); |
6a74a719 ILT |
1342 | } |
1343 | ||
dbe717ef | 1344 | // Output_data_got::Got_entry methods. |
ead1e424 ILT |
1345 | |
1346 | // Write out the entry. | |
1347 | ||
1348 | template<int size, bool big_endian> | |
1349 | void | |
7e1edb90 | 1350 | Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const |
ead1e424 ILT |
1351 | { |
1352 | Valtype val = 0; | |
1353 | ||
1354 | switch (this->local_sym_index_) | |
1355 | { | |
1356 | case GSYM_CODE: | |
1357 | { | |
e8c846c3 ILT |
1358 | // If the symbol is resolved locally, we need to write out the |
1359 | // link-time value, which will be relocated dynamically by a | |
1360 | // RELATIVE relocation. | |
ead1e424 | 1361 | Symbol* gsym = this->u_.gsym; |
7223e9ca | 1362 | if (this->use_plt_offset_ && gsym->has_plt_offset()) |
67181c72 | 1363 | val = (parameters->target().plt_address_for_global(gsym) |
7223e9ca ILT |
1364 | + gsym->plt_offset()); |
1365 | else | |
1366 | { | |
1367 | Sized_symbol<size>* sgsym; | |
1368 | // This cast is a bit ugly. We don't want to put a | |
1369 | // virtual method in Symbol, because we want Symbol to be | |
1370 | // as small as possible. | |
1371 | sgsym = static_cast<Sized_symbol<size>*>(gsym); | |
1372 | val = sgsym->value(); | |
1373 | } | |
ead1e424 ILT |
1374 | } |
1375 | break; | |
1376 | ||
1377 | case CONSTANT_CODE: | |
1378 | val = this->u_.constant; | |
1379 | break; | |
1380 | ||
4829d394 CC |
1381 | case RESERVED_CODE: |
1382 | // If we're doing an incremental update, don't touch this GOT entry. | |
1383 | if (parameters->incremental_update()) | |
1384 | return; | |
1385 | val = this->u_.constant; | |
1386 | break; | |
1387 | ||
ead1e424 | 1388 | default: |
d1f003c6 | 1389 | { |
83896202 | 1390 | const Relobj* object = this->u_.object; |
d1f003c6 | 1391 | const unsigned int lsi = this->local_sym_index_; |
7223e9ca | 1392 | if (!this->use_plt_offset_) |
83896202 ILT |
1393 | { |
1394 | uint64_t lval = object->local_symbol_value(lsi, 0); | |
1395 | val = convert_types<Valtype, uint64_t>(lval); | |
1396 | } | |
7223e9ca ILT |
1397 | else |
1398 | { | |
67181c72 ILT |
1399 | uint64_t plt_address = |
1400 | parameters->target().plt_address_for_local(object, lsi); | |
1401 | val = plt_address + object->local_plt_offset(lsi); | |
7223e9ca | 1402 | } |
d1f003c6 | 1403 | } |
e727fa71 | 1404 | break; |
ead1e424 ILT |
1405 | } |
1406 | ||
a3ad94ed | 1407 | elfcpp::Swap<size, big_endian>::writeval(pov, val); |
ead1e424 ILT |
1408 | } |
1409 | ||
dbe717ef | 1410 | // Output_data_got methods. |
ead1e424 | 1411 | |
dbe717ef ILT |
1412 | // Add an entry for a global symbol to the GOT. This returns true if |
1413 | // this is a new GOT entry, false if the symbol already had a GOT | |
1414 | // entry. | |
1415 | ||
1416 | template<int size, bool big_endian> | |
1417 | bool | |
0a65a3a7 CC |
1418 | Output_data_got<size, big_endian>::add_global( |
1419 | Symbol* gsym, | |
1420 | unsigned int got_type) | |
ead1e424 | 1421 | { |
0a65a3a7 | 1422 | if (gsym->has_got_offset(got_type)) |
dbe717ef | 1423 | return false; |
ead1e424 | 1424 | |
4829d394 CC |
1425 | unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false)); |
1426 | gsym->set_got_offset(got_type, got_offset); | |
7223e9ca ILT |
1427 | return true; |
1428 | } | |
1429 | ||
1430 | // Like add_global, but use the PLT offset. | |
1431 | ||
1432 | template<int size, bool big_endian> | |
1433 | bool | |
1434 | Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym, | |
1435 | unsigned int got_type) | |
1436 | { | |
1437 | if (gsym->has_got_offset(got_type)) | |
1438 | return false; | |
1439 | ||
4829d394 CC |
1440 | unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true)); |
1441 | gsym->set_got_offset(got_type, got_offset); | |
dbe717ef ILT |
1442 | return true; |
1443 | } | |
ead1e424 | 1444 | |
7bf1f802 ILT |
1445 | // Add an entry for a global symbol to the GOT, and add a dynamic |
1446 | // relocation of type R_TYPE for the GOT entry. | |
7223e9ca | 1447 | |
7bf1f802 ILT |
1448 | template<int size, bool big_endian> |
1449 | void | |
1450 | Output_data_got<size, big_endian>::add_global_with_rel( | |
1451 | Symbol* gsym, | |
0a65a3a7 | 1452 | unsigned int got_type, |
83896202 | 1453 | Output_data_reloc_generic* rel_dyn, |
7bf1f802 ILT |
1454 | unsigned int r_type) |
1455 | { | |
0a65a3a7 | 1456 | if (gsym->has_got_offset(got_type)) |
7bf1f802 ILT |
1457 | return; |
1458 | ||
4829d394 | 1459 | unsigned int got_offset = this->add_got_entry(Got_entry()); |
2ea97941 | 1460 | gsym->set_got_offset(got_type, got_offset); |
83896202 | 1461 | rel_dyn->add_global_generic(gsym, r_type, this, got_offset, 0); |
7bf1f802 ILT |
1462 | } |
1463 | ||
0a65a3a7 CC |
1464 | // Add a pair of entries for a global symbol to the GOT, and add |
1465 | // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively. | |
1466 | // If R_TYPE_2 == 0, add the second entry with no relocation. | |
7bf1f802 ILT |
1467 | template<int size, bool big_endian> |
1468 | void | |
0a65a3a7 CC |
1469 | Output_data_got<size, big_endian>::add_global_pair_with_rel( |
1470 | Symbol* gsym, | |
1471 | unsigned int got_type, | |
83896202 | 1472 | Output_data_reloc_generic* rel_dyn, |
0a65a3a7 CC |
1473 | unsigned int r_type_1, |
1474 | unsigned int r_type_2) | |
7bf1f802 | 1475 | { |
0a65a3a7 | 1476 | if (gsym->has_got_offset(got_type)) |
7bf1f802 ILT |
1477 | return; |
1478 | ||
4829d394 | 1479 | unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry()); |
2ea97941 | 1480 | gsym->set_got_offset(got_type, got_offset); |
83896202 | 1481 | rel_dyn->add_global_generic(gsym, r_type_1, this, got_offset, 0); |
0a65a3a7 | 1482 | |
0a65a3a7 | 1483 | if (r_type_2 != 0) |
83896202 ILT |
1484 | rel_dyn->add_global_generic(gsym, r_type_2, this, |
1485 | got_offset + size / 8, 0); | |
7bf1f802 ILT |
1486 | } |
1487 | ||
0a65a3a7 CC |
1488 | // Add an entry for a local symbol to the GOT. This returns true if |
1489 | // this is a new GOT entry, false if the symbol already has a GOT | |
1490 | // entry. | |
07f397ab ILT |
1491 | |
1492 | template<int size, bool big_endian> | |
1493 | bool | |
0a65a3a7 | 1494 | Output_data_got<size, big_endian>::add_local( |
83896202 | 1495 | Relobj* object, |
0a65a3a7 CC |
1496 | unsigned int symndx, |
1497 | unsigned int got_type) | |
07f397ab | 1498 | { |
0a65a3a7 | 1499 | if (object->local_has_got_offset(symndx, got_type)) |
07f397ab ILT |
1500 | return false; |
1501 | ||
4829d394 CC |
1502 | unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx, |
1503 | false)); | |
1504 | object->set_local_got_offset(symndx, got_type, got_offset); | |
7223e9ca ILT |
1505 | return true; |
1506 | } | |
1507 | ||
1508 | // Like add_local, but use the PLT offset. | |
1509 | ||
1510 | template<int size, bool big_endian> | |
1511 | bool | |
1512 | Output_data_got<size, big_endian>::add_local_plt( | |
83896202 | 1513 | Relobj* object, |
7223e9ca ILT |
1514 | unsigned int symndx, |
1515 | unsigned int got_type) | |
1516 | { | |
1517 | if (object->local_has_got_offset(symndx, got_type)) | |
1518 | return false; | |
1519 | ||
4829d394 CC |
1520 | unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx, |
1521 | true)); | |
1522 | object->set_local_got_offset(symndx, got_type, got_offset); | |
07f397ab ILT |
1523 | return true; |
1524 | } | |
1525 | ||
0a65a3a7 CC |
1526 | // Add an entry for a local symbol to the GOT, and add a dynamic |
1527 | // relocation of type R_TYPE for the GOT entry. | |
7223e9ca | 1528 | |
7bf1f802 ILT |
1529 | template<int size, bool big_endian> |
1530 | void | |
0a65a3a7 | 1531 | Output_data_got<size, big_endian>::add_local_with_rel( |
83896202 | 1532 | Relobj* object, |
0a65a3a7 CC |
1533 | unsigned int symndx, |
1534 | unsigned int got_type, | |
83896202 | 1535 | Output_data_reloc_generic* rel_dyn, |
7bf1f802 ILT |
1536 | unsigned int r_type) |
1537 | { | |
0a65a3a7 | 1538 | if (object->local_has_got_offset(symndx, got_type)) |
7bf1f802 ILT |
1539 | return; |
1540 | ||
4829d394 | 1541 | unsigned int got_offset = this->add_got_entry(Got_entry()); |
2ea97941 | 1542 | object->set_local_got_offset(symndx, got_type, got_offset); |
83896202 | 1543 | rel_dyn->add_local_generic(object, symndx, r_type, this, got_offset, 0); |
07f397ab ILT |
1544 | } |
1545 | ||
0a65a3a7 CC |
1546 | // Add a pair of entries for a local symbol to the GOT, and add |
1547 | // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively. | |
1548 | // If R_TYPE_2 == 0, add the second entry with no relocation. | |
7bf1f802 ILT |
1549 | template<int size, bool big_endian> |
1550 | void | |
0a65a3a7 | 1551 | Output_data_got<size, big_endian>::add_local_pair_with_rel( |
83896202 | 1552 | Relobj* object, |
7bf1f802 ILT |
1553 | unsigned int symndx, |
1554 | unsigned int shndx, | |
0a65a3a7 | 1555 | unsigned int got_type, |
83896202 | 1556 | Output_data_reloc_generic* rel_dyn, |
0a65a3a7 CC |
1557 | unsigned int r_type_1, |
1558 | unsigned int r_type_2) | |
7bf1f802 | 1559 | { |
0a65a3a7 | 1560 | if (object->local_has_got_offset(symndx, got_type)) |
7bf1f802 ILT |
1561 | return; |
1562 | ||
4829d394 CC |
1563 | unsigned int got_offset = |
1564 | this->add_got_entry_pair(Got_entry(), | |
1565 | Got_entry(object, symndx, false)); | |
2ea97941 | 1566 | object->set_local_got_offset(symndx, got_type, got_offset); |
ef9beddf | 1567 | Output_section* os = object->output_section(shndx); |
83896202 | 1568 | rel_dyn->add_output_section_generic(os, r_type_1, this, got_offset, 0); |
7bf1f802 | 1569 | |
0a65a3a7 | 1570 | if (r_type_2 != 0) |
83896202 ILT |
1571 | rel_dyn->add_output_section_generic(os, r_type_2, this, |
1572 | got_offset + size / 8, 0); | |
4829d394 CC |
1573 | } |
1574 | ||
1575 | // Reserve a slot in the GOT for a local symbol or the second slot of a pair. | |
1576 | ||
1577 | template<int size, bool big_endian> | |
1578 | void | |
6fa2a40b CC |
1579 | Output_data_got<size, big_endian>::reserve_local( |
1580 | unsigned int i, | |
83896202 | 1581 | Relobj* object, |
6fa2a40b CC |
1582 | unsigned int sym_index, |
1583 | unsigned int got_type) | |
4829d394 | 1584 | { |
6fa2a40b CC |
1585 | this->reserve_slot(i); |
1586 | object->set_local_got_offset(sym_index, got_type, this->got_offset(i)); | |
4829d394 | 1587 | } |
7bf1f802 | 1588 | |
4829d394 CC |
1589 | // Reserve a slot in the GOT for a global symbol. |
1590 | ||
1591 | template<int size, bool big_endian> | |
1592 | void | |
6fa2a40b | 1593 | Output_data_got<size, big_endian>::reserve_global( |
4829d394 CC |
1594 | unsigned int i, |
1595 | Symbol* gsym, | |
1596 | unsigned int got_type) | |
1597 | { | |
6fa2a40b | 1598 | this->reserve_slot(i); |
4829d394 | 1599 | gsym->set_got_offset(got_type, this->got_offset(i)); |
7bf1f802 ILT |
1600 | } |
1601 | ||
ead1e424 ILT |
1602 | // Write out the GOT. |
1603 | ||
1604 | template<int size, bool big_endian> | |
1605 | void | |
dbe717ef | 1606 | Output_data_got<size, big_endian>::do_write(Output_file* of) |
ead1e424 ILT |
1607 | { |
1608 | const int add = size / 8; | |
1609 | ||
1610 | const off_t off = this->offset(); | |
c06b7b0b | 1611 | const off_t oview_size = this->data_size(); |
ead1e424 ILT |
1612 | unsigned char* const oview = of->get_output_view(off, oview_size); |
1613 | ||
1614 | unsigned char* pov = oview; | |
1615 | for (typename Got_entries::const_iterator p = this->entries_.begin(); | |
1616 | p != this->entries_.end(); | |
1617 | ++p) | |
1618 | { | |
7e1edb90 | 1619 | p->write(pov); |
ead1e424 ILT |
1620 | pov += add; |
1621 | } | |
1622 | ||
a3ad94ed | 1623 | gold_assert(pov - oview == oview_size); |
c06b7b0b | 1624 | |
ead1e424 ILT |
1625 | of->write_output_view(off, oview_size, oview); |
1626 | ||
1627 | // We no longer need the GOT entries. | |
1628 | this->entries_.clear(); | |
1629 | } | |
1630 | ||
4829d394 CC |
1631 | // Create a new GOT entry and return its offset. |
1632 | ||
1633 | template<int size, bool big_endian> | |
1634 | unsigned int | |
1635 | Output_data_got<size, big_endian>::add_got_entry(Got_entry got_entry) | |
1636 | { | |
1637 | if (!this->is_data_size_valid()) | |
1638 | { | |
1639 | this->entries_.push_back(got_entry); | |
1640 | this->set_got_size(); | |
1641 | return this->last_got_offset(); | |
1642 | } | |
1643 | else | |
1644 | { | |
1645 | // For an incremental update, find an available slot. | |
1646 | off_t got_offset = this->free_list_.allocate(size / 8, size / 8, 0); | |
1647 | if (got_offset == -1) | |
e6455dfb CC |
1648 | gold_fallback(_("out of patch space (GOT);" |
1649 | " relink with --incremental-full")); | |
4829d394 CC |
1650 | unsigned int got_index = got_offset / (size / 8); |
1651 | gold_assert(got_index < this->entries_.size()); | |
1652 | this->entries_[got_index] = got_entry; | |
1653 | return static_cast<unsigned int>(got_offset); | |
1654 | } | |
1655 | } | |
1656 | ||
1657 | // Create a pair of new GOT entries and return the offset of the first. | |
1658 | ||
1659 | template<int size, bool big_endian> | |
1660 | unsigned int | |
1661 | Output_data_got<size, big_endian>::add_got_entry_pair(Got_entry got_entry_1, | |
1662 | Got_entry got_entry_2) | |
1663 | { | |
1664 | if (!this->is_data_size_valid()) | |
1665 | { | |
1666 | unsigned int got_offset; | |
1667 | this->entries_.push_back(got_entry_1); | |
1668 | got_offset = this->last_got_offset(); | |
1669 | this->entries_.push_back(got_entry_2); | |
1670 | this->set_got_size(); | |
1671 | return got_offset; | |
1672 | } | |
1673 | else | |
1674 | { | |
1675 | // For an incremental update, find an available pair of slots. | |
1676 | off_t got_offset = this->free_list_.allocate(2 * size / 8, size / 8, 0); | |
1677 | if (got_offset == -1) | |
e6455dfb CC |
1678 | gold_fallback(_("out of patch space (GOT);" |
1679 | " relink with --incremental-full")); | |
4829d394 CC |
1680 | unsigned int got_index = got_offset / (size / 8); |
1681 | gold_assert(got_index < this->entries_.size()); | |
1682 | this->entries_[got_index] = got_entry_1; | |
1683 | this->entries_[got_index + 1] = got_entry_2; | |
1684 | return static_cast<unsigned int>(got_offset); | |
1685 | } | |
1686 | } | |
1687 | ||
a3ad94ed ILT |
1688 | // Output_data_dynamic::Dynamic_entry methods. |
1689 | ||
1690 | // Write out the entry. | |
1691 | ||
1692 | template<int size, bool big_endian> | |
1693 | void | |
1694 | Output_data_dynamic::Dynamic_entry::write( | |
1695 | unsigned char* pov, | |
7d1a9ebb | 1696 | const Stringpool* pool) const |
a3ad94ed ILT |
1697 | { |
1698 | typename elfcpp::Elf_types<size>::Elf_WXword val; | |
c2b45e22 | 1699 | switch (this->offset_) |
a3ad94ed ILT |
1700 | { |
1701 | case DYNAMIC_NUMBER: | |
1702 | val = this->u_.val; | |
1703 | break; | |
1704 | ||
a3ad94ed | 1705 | case DYNAMIC_SECTION_SIZE: |
16649710 | 1706 | val = this->u_.od->data_size(); |
612a8d3d DM |
1707 | if (this->od2 != NULL) |
1708 | val += this->od2->data_size(); | |
a3ad94ed ILT |
1709 | break; |
1710 | ||
1711 | case DYNAMIC_SYMBOL: | |
1712 | { | |
16649710 ILT |
1713 | const Sized_symbol<size>* s = |
1714 | static_cast<const Sized_symbol<size>*>(this->u_.sym); | |
a3ad94ed ILT |
1715 | val = s->value(); |
1716 | } | |
1717 | break; | |
1718 | ||
1719 | case DYNAMIC_STRING: | |
1720 | val = pool->get_offset(this->u_.str); | |
1721 | break; | |
1722 | ||
1723 | default: | |
c2b45e22 CC |
1724 | val = this->u_.od->address() + this->offset_; |
1725 | break; | |
a3ad94ed ILT |
1726 | } |
1727 | ||
1728 | elfcpp::Dyn_write<size, big_endian> dw(pov); | |
1729 | dw.put_d_tag(this->tag_); | |
1730 | dw.put_d_val(val); | |
1731 | } | |
1732 | ||
1733 | // Output_data_dynamic methods. | |
1734 | ||
16649710 ILT |
1735 | // Adjust the output section to set the entry size. |
1736 | ||
1737 | void | |
1738 | Output_data_dynamic::do_adjust_output_section(Output_section* os) | |
1739 | { | |
8851ecca | 1740 | if (parameters->target().get_size() == 32) |
16649710 | 1741 | os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size); |
8851ecca | 1742 | else if (parameters->target().get_size() == 64) |
16649710 ILT |
1743 | os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size); |
1744 | else | |
1745 | gold_unreachable(); | |
1746 | } | |
1747 | ||
a3ad94ed ILT |
1748 | // Set the final data size. |
1749 | ||
1750 | void | |
27bc2bce | 1751 | Output_data_dynamic::set_final_data_size() |
a3ad94ed | 1752 | { |
20e6d0d6 DK |
1753 | // Add the terminating entry if it hasn't been added. |
1754 | // Because of relaxation, we can run this multiple times. | |
9e9e071b ILT |
1755 | if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL) |
1756 | { | |
1757 | int extra = parameters->options().spare_dynamic_tags(); | |
1758 | for (int i = 0; i < extra; ++i) | |
1759 | this->add_constant(elfcpp::DT_NULL, 0); | |
1760 | this->add_constant(elfcpp::DT_NULL, 0); | |
1761 | } | |
a3ad94ed ILT |
1762 | |
1763 | int dyn_size; | |
8851ecca | 1764 | if (parameters->target().get_size() == 32) |
a3ad94ed | 1765 | dyn_size = elfcpp::Elf_sizes<32>::dyn_size; |
8851ecca | 1766 | else if (parameters->target().get_size() == 64) |
a3ad94ed ILT |
1767 | dyn_size = elfcpp::Elf_sizes<64>::dyn_size; |
1768 | else | |
1769 | gold_unreachable(); | |
1770 | this->set_data_size(this->entries_.size() * dyn_size); | |
1771 | } | |
1772 | ||
1773 | // Write out the dynamic entries. | |
1774 | ||
1775 | void | |
1776 | Output_data_dynamic::do_write(Output_file* of) | |
1777 | { | |
8851ecca | 1778 | switch (parameters->size_and_endianness()) |
a3ad94ed | 1779 | { |
9025d29d | 1780 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
1781 | case Parameters::TARGET_32_LITTLE: |
1782 | this->sized_write<32, false>(of); | |
1783 | break; | |
9025d29d | 1784 | #endif |
8851ecca ILT |
1785 | #ifdef HAVE_TARGET_32_BIG |
1786 | case Parameters::TARGET_32_BIG: | |
1787 | this->sized_write<32, true>(of); | |
1788 | break; | |
9025d29d | 1789 | #endif |
9025d29d | 1790 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
1791 | case Parameters::TARGET_64_LITTLE: |
1792 | this->sized_write<64, false>(of); | |
1793 | break; | |
9025d29d | 1794 | #endif |
8851ecca ILT |
1795 | #ifdef HAVE_TARGET_64_BIG |
1796 | case Parameters::TARGET_64_BIG: | |
1797 | this->sized_write<64, true>(of); | |
1798 | break; | |
1799 | #endif | |
1800 | default: | |
1801 | gold_unreachable(); | |
a3ad94ed | 1802 | } |
a3ad94ed ILT |
1803 | } |
1804 | ||
1805 | template<int size, bool big_endian> | |
1806 | void | |
1807 | Output_data_dynamic::sized_write(Output_file* of) | |
1808 | { | |
1809 | const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size; | |
1810 | ||
2ea97941 | 1811 | const off_t offset = this->offset(); |
a3ad94ed | 1812 | const off_t oview_size = this->data_size(); |
2ea97941 | 1813 | unsigned char* const oview = of->get_output_view(offset, oview_size); |
a3ad94ed ILT |
1814 | |
1815 | unsigned char* pov = oview; | |
1816 | for (typename Dynamic_entries::const_iterator p = this->entries_.begin(); | |
1817 | p != this->entries_.end(); | |
1818 | ++p) | |
1819 | { | |
7d1a9ebb | 1820 | p->write<size, big_endian>(pov, this->pool_); |
a3ad94ed ILT |
1821 | pov += dyn_size; |
1822 | } | |
1823 | ||
1824 | gold_assert(pov - oview == oview_size); | |
1825 | ||
2ea97941 | 1826 | of->write_output_view(offset, oview_size, oview); |
a3ad94ed ILT |
1827 | |
1828 | // We no longer need the dynamic entries. | |
1829 | this->entries_.clear(); | |
1830 | } | |
1831 | ||
d491d34e ILT |
1832 | // Class Output_symtab_xindex. |
1833 | ||
1834 | void | |
1835 | Output_symtab_xindex::do_write(Output_file* of) | |
1836 | { | |
2ea97941 | 1837 | const off_t offset = this->offset(); |
d491d34e | 1838 | const off_t oview_size = this->data_size(); |
2ea97941 | 1839 | unsigned char* const oview = of->get_output_view(offset, oview_size); |
d491d34e ILT |
1840 | |
1841 | memset(oview, 0, oview_size); | |
1842 | ||
1843 | if (parameters->target().is_big_endian()) | |
1844 | this->endian_do_write<true>(oview); | |
1845 | else | |
1846 | this->endian_do_write<false>(oview); | |
1847 | ||
2ea97941 | 1848 | of->write_output_view(offset, oview_size, oview); |
d491d34e ILT |
1849 | |
1850 | // We no longer need the data. | |
1851 | this->entries_.clear(); | |
1852 | } | |
1853 | ||
1854 | template<bool big_endian> | |
1855 | void | |
1856 | Output_symtab_xindex::endian_do_write(unsigned char* const oview) | |
1857 | { | |
1858 | for (Xindex_entries::const_iterator p = this->entries_.begin(); | |
1859 | p != this->entries_.end(); | |
1860 | ++p) | |
20e6d0d6 DK |
1861 | { |
1862 | unsigned int symndx = p->first; | |
1863 | gold_assert(symndx * 4 < this->data_size()); | |
1864 | elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second); | |
1865 | } | |
d491d34e ILT |
1866 | } |
1867 | ||
8ea8cd50 CC |
1868 | // Output_fill_debug_info methods. |
1869 | ||
1870 | // Return the minimum size needed for a dummy compilation unit header. | |
1871 | ||
1872 | size_t | |
1873 | Output_fill_debug_info::do_minimum_hole_size() const | |
1874 | { | |
1875 | // Compile unit header fields: unit_length, version, debug_abbrev_offset, | |
1876 | // address_size. | |
1877 | const size_t len = 4 + 2 + 4 + 1; | |
1878 | // For type units, add type_signature, type_offset. | |
1879 | if (this->is_debug_types_) | |
1880 | return len + 8 + 4; | |
1881 | return len; | |
1882 | } | |
1883 | ||
1884 | // Write a dummy compilation unit header to fill a hole in the | |
1885 | // .debug_info or .debug_types section. | |
1886 | ||
1887 | void | |
1888 | Output_fill_debug_info::do_write(Output_file* of, off_t off, size_t len) const | |
1889 | { | |
66570254 CC |
1890 | gold_debug(DEBUG_INCREMENTAL, "fill_debug_info(%08lx, %08lx)", |
1891 | static_cast<long>(off), static_cast<long>(len)); | |
8ea8cd50 CC |
1892 | |
1893 | gold_assert(len >= this->do_minimum_hole_size()); | |
1894 | ||
1895 | unsigned char* const oview = of->get_output_view(off, len); | |
1896 | unsigned char* pov = oview; | |
1897 | ||
1898 | // Write header fields: unit_length, version, debug_abbrev_offset, | |
1899 | // address_size. | |
1900 | if (this->is_big_endian()) | |
1901 | { | |
24c6c55a DM |
1902 | elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4); |
1903 | elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version); | |
1904 | elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, 0); | |
8ea8cd50 CC |
1905 | } |
1906 | else | |
1907 | { | |
24c6c55a DM |
1908 | elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4); |
1909 | elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version); | |
1910 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, 0); | |
8ea8cd50 CC |
1911 | } |
1912 | pov += 4 + 2 + 4; | |
1913 | *pov++ = 4; | |
1914 | ||
1915 | // For type units, the additional header fields -- type_signature, | |
1916 | // type_offset -- can be filled with zeroes. | |
1917 | ||
1918 | // Fill the remainder of the free space with zeroes. The first | |
1919 | // zero should tell the consumer there are no DIEs to read in this | |
1920 | // compilation unit. | |
1921 | if (pov < oview + len) | |
1922 | memset(pov, 0, oview + len - pov); | |
1923 | ||
1924 | of->write_output_view(off, len, oview); | |
1925 | } | |
1926 | ||
1927 | // Output_fill_debug_line methods. | |
1928 | ||
1929 | // Return the minimum size needed for a dummy line number program header. | |
1930 | ||
1931 | size_t | |
1932 | Output_fill_debug_line::do_minimum_hole_size() const | |
1933 | { | |
1934 | // Line number program header fields: unit_length, version, header_length, | |
1935 | // minimum_instruction_length, default_is_stmt, line_base, line_range, | |
1936 | // opcode_base, standard_opcode_lengths[], include_directories, filenames. | |
1937 | const size_t len = 4 + 2 + 4 + this->header_length; | |
1938 | return len; | |
1939 | } | |
1940 | ||
1941 | // Write a dummy line number program header to fill a hole in the | |
1942 | // .debug_line section. | |
1943 | ||
1944 | void | |
1945 | Output_fill_debug_line::do_write(Output_file* of, off_t off, size_t len) const | |
1946 | { | |
66570254 CC |
1947 | gold_debug(DEBUG_INCREMENTAL, "fill_debug_line(%08lx, %08lx)", |
1948 | static_cast<long>(off), static_cast<long>(len)); | |
8ea8cd50 CC |
1949 | |
1950 | gold_assert(len >= this->do_minimum_hole_size()); | |
1951 | ||
1952 | unsigned char* const oview = of->get_output_view(off, len); | |
1953 | unsigned char* pov = oview; | |
1954 | ||
1955 | // Write header fields: unit_length, version, header_length, | |
1956 | // minimum_instruction_length, default_is_stmt, line_base, line_range, | |
1957 | // opcode_base, standard_opcode_lengths[], include_directories, filenames. | |
1958 | // We set the header_length field to cover the entire hole, so the | |
1959 | // line number program is empty. | |
1960 | if (this->is_big_endian()) | |
1961 | { | |
24c6c55a DM |
1962 | elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4); |
1963 | elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version); | |
1964 | elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, len - (4 + 2 + 4)); | |
8ea8cd50 CC |
1965 | } |
1966 | else | |
1967 | { | |
24c6c55a DM |
1968 | elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4); |
1969 | elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version); | |
1970 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, len - (4 + 2 + 4)); | |
8ea8cd50 CC |
1971 | } |
1972 | pov += 4 + 2 + 4; | |
1973 | *pov++ = 1; // minimum_instruction_length | |
1974 | *pov++ = 0; // default_is_stmt | |
1975 | *pov++ = 0; // line_base | |
1976 | *pov++ = 5; // line_range | |
1977 | *pov++ = 13; // opcode_base | |
1978 | *pov++ = 0; // standard_opcode_lengths[1] | |
1979 | *pov++ = 1; // standard_opcode_lengths[2] | |
1980 | *pov++ = 1; // standard_opcode_lengths[3] | |
1981 | *pov++ = 1; // standard_opcode_lengths[4] | |
1982 | *pov++ = 1; // standard_opcode_lengths[5] | |
1983 | *pov++ = 0; // standard_opcode_lengths[6] | |
1984 | *pov++ = 0; // standard_opcode_lengths[7] | |
1985 | *pov++ = 0; // standard_opcode_lengths[8] | |
1986 | *pov++ = 1; // standard_opcode_lengths[9] | |
1987 | *pov++ = 0; // standard_opcode_lengths[10] | |
1988 | *pov++ = 0; // standard_opcode_lengths[11] | |
1989 | *pov++ = 1; // standard_opcode_lengths[12] | |
1990 | *pov++ = 0; // include_directories (empty) | |
1991 | *pov++ = 0; // filenames (empty) | |
1992 | ||
1993 | // Some consumers don't check the header_length field, and simply | |
1994 | // start reading the line number program immediately following the | |
1995 | // header. For those consumers, we fill the remainder of the free | |
1996 | // space with DW_LNS_set_basic_block opcodes. These are effectively | |
1997 | // no-ops: the resulting line table program will not create any rows. | |
1998 | if (pov < oview + len) | |
1999 | memset(pov, elfcpp::DW_LNS_set_basic_block, oview + len - pov); | |
2000 | ||
2001 | of->write_output_view(off, len, oview); | |
2002 | } | |
2003 | ||
ead1e424 ILT |
2004 | // Output_section::Input_section methods. |
2005 | ||
cdc29364 CC |
2006 | // Return the current data size. For an input section we store the size here. |
2007 | // For an Output_section_data, we have to ask it for the size. | |
2008 | ||
2009 | off_t | |
2010 | Output_section::Input_section::current_data_size() const | |
2011 | { | |
2012 | if (this->is_input_section()) | |
2013 | return this->u1_.data_size; | |
2014 | else | |
2015 | { | |
2016 | this->u2_.posd->pre_finalize_data_size(); | |
2017 | return this->u2_.posd->current_data_size(); | |
2018 | } | |
2019 | } | |
2020 | ||
ead1e424 ILT |
2021 | // Return the data size. For an input section we store the size here. |
2022 | // For an Output_section_data, we have to ask it for the size. | |
2023 | ||
2024 | off_t | |
2025 | Output_section::Input_section::data_size() const | |
2026 | { | |
2027 | if (this->is_input_section()) | |
b8e6aad9 | 2028 | return this->u1_.data_size; |
ead1e424 | 2029 | else |
b8e6aad9 | 2030 | return this->u2_.posd->data_size(); |
ead1e424 ILT |
2031 | } |
2032 | ||
0439c796 DK |
2033 | // Return the object for an input section. |
2034 | ||
2035 | Relobj* | |
2036 | Output_section::Input_section::relobj() const | |
2037 | { | |
2038 | if (this->is_input_section()) | |
2039 | return this->u2_.object; | |
2040 | else if (this->is_merge_section()) | |
2041 | { | |
2042 | gold_assert(this->u2_.pomb->first_relobj() != NULL); | |
2043 | return this->u2_.pomb->first_relobj(); | |
2044 | } | |
2045 | else if (this->is_relaxed_input_section()) | |
2046 | return this->u2_.poris->relobj(); | |
2047 | else | |
2048 | gold_unreachable(); | |
2049 | } | |
2050 | ||
2051 | // Return the input section index for an input section. | |
2052 | ||
2053 | unsigned int | |
2054 | Output_section::Input_section::shndx() const | |
2055 | { | |
2056 | if (this->is_input_section()) | |
2057 | return this->shndx_; | |
2058 | else if (this->is_merge_section()) | |
2059 | { | |
2060 | gold_assert(this->u2_.pomb->first_relobj() != NULL); | |
2061 | return this->u2_.pomb->first_shndx(); | |
2062 | } | |
2063 | else if (this->is_relaxed_input_section()) | |
2064 | return this->u2_.poris->shndx(); | |
2065 | else | |
2066 | gold_unreachable(); | |
2067 | } | |
2068 | ||
ead1e424 ILT |
2069 | // Set the address and file offset. |
2070 | ||
2071 | void | |
96803768 ILT |
2072 | Output_section::Input_section::set_address_and_file_offset( |
2073 | uint64_t address, | |
2074 | off_t file_offset, | |
2075 | off_t section_file_offset) | |
ead1e424 ILT |
2076 | { |
2077 | if (this->is_input_section()) | |
96803768 ILT |
2078 | this->u2_.object->set_section_offset(this->shndx_, |
2079 | file_offset - section_file_offset); | |
ead1e424 | 2080 | else |
96803768 ILT |
2081 | this->u2_.posd->set_address_and_file_offset(address, file_offset); |
2082 | } | |
2083 | ||
a445fddf ILT |
2084 | // Reset the address and file offset. |
2085 | ||
2086 | void | |
2087 | Output_section::Input_section::reset_address_and_file_offset() | |
2088 | { | |
2089 | if (!this->is_input_section()) | |
2090 | this->u2_.posd->reset_address_and_file_offset(); | |
2091 | } | |
2092 | ||
96803768 ILT |
2093 | // Finalize the data size. |
2094 | ||
2095 | void | |
2096 | Output_section::Input_section::finalize_data_size() | |
2097 | { | |
2098 | if (!this->is_input_section()) | |
2099 | this->u2_.posd->finalize_data_size(); | |
b8e6aad9 ILT |
2100 | } |
2101 | ||
1e983657 ILT |
2102 | // Try to turn an input offset into an output offset. We want to |
2103 | // return the output offset relative to the start of this | |
2104 | // Input_section in the output section. | |
b8e6aad9 | 2105 | |
8f00aeb8 | 2106 | inline bool |
8383303e ILT |
2107 | Output_section::Input_section::output_offset( |
2108 | const Relobj* object, | |
2ea97941 ILT |
2109 | unsigned int shndx, |
2110 | section_offset_type offset, | |
ca09d69a | 2111 | section_offset_type* poutput) const |
b8e6aad9 ILT |
2112 | { |
2113 | if (!this->is_input_section()) | |
2ea97941 | 2114 | return this->u2_.posd->output_offset(object, shndx, offset, poutput); |
b8e6aad9 ILT |
2115 | else |
2116 | { | |
2ea97941 | 2117 | if (this->shndx_ != shndx || this->u2_.object != object) |
b8e6aad9 | 2118 | return false; |
2ea97941 | 2119 | *poutput = offset; |
b8e6aad9 ILT |
2120 | return true; |
2121 | } | |
ead1e424 ILT |
2122 | } |
2123 | ||
a9a60db6 ILT |
2124 | // Return whether this is the merge section for the input section |
2125 | // SHNDX in OBJECT. | |
2126 | ||
2127 | inline bool | |
2128 | Output_section::Input_section::is_merge_section_for(const Relobj* object, | |
2ea97941 | 2129 | unsigned int shndx) const |
a9a60db6 ILT |
2130 | { |
2131 | if (this->is_input_section()) | |
2132 | return false; | |
2ea97941 | 2133 | return this->u2_.posd->is_merge_section_for(object, shndx); |
a9a60db6 ILT |
2134 | } |
2135 | ||
ead1e424 ILT |
2136 | // Write out the data. We don't have to do anything for an input |
2137 | // section--they are handled via Object::relocate--but this is where | |
2138 | // we write out the data for an Output_section_data. | |
2139 | ||
2140 | void | |
2141 | Output_section::Input_section::write(Output_file* of) | |
2142 | { | |
2143 | if (!this->is_input_section()) | |
b8e6aad9 | 2144 | this->u2_.posd->write(of); |
ead1e424 ILT |
2145 | } |
2146 | ||
96803768 ILT |
2147 | // Write the data to a buffer. As for write(), we don't have to do |
2148 | // anything for an input section. | |
2149 | ||
2150 | void | |
2151 | Output_section::Input_section::write_to_buffer(unsigned char* buffer) | |
2152 | { | |
2153 | if (!this->is_input_section()) | |
2154 | this->u2_.posd->write_to_buffer(buffer); | |
2155 | } | |
2156 | ||
7d9e3d98 ILT |
2157 | // Print to a map file. |
2158 | ||
2159 | void | |
2160 | Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const | |
2161 | { | |
2162 | switch (this->shndx_) | |
2163 | { | |
2164 | case OUTPUT_SECTION_CODE: | |
2165 | case MERGE_DATA_SECTION_CODE: | |
2166 | case MERGE_STRING_SECTION_CODE: | |
2167 | this->u2_.posd->print_to_mapfile(mapfile); | |
2168 | break; | |
2169 | ||
20e6d0d6 DK |
2170 | case RELAXED_INPUT_SECTION_CODE: |
2171 | { | |
2172 | Output_relaxed_input_section* relaxed_section = | |
2173 | this->relaxed_input_section(); | |
2174 | mapfile->print_input_section(relaxed_section->relobj(), | |
2175 | relaxed_section->shndx()); | |
2176 | } | |
2177 | break; | |
7d9e3d98 ILT |
2178 | default: |
2179 | mapfile->print_input_section(this->u2_.object, this->shndx_); | |
2180 | break; | |
2181 | } | |
2182 | } | |
2183 | ||
a2fb1b05 ILT |
2184 | // Output_section methods. |
2185 | ||
2186 | // Construct an Output_section. NAME will point into a Stringpool. | |
2187 | ||
2ea97941 ILT |
2188 | Output_section::Output_section(const char* name, elfcpp::Elf_Word type, |
2189 | elfcpp::Elf_Xword flags) | |
2190 | : name_(name), | |
a2fb1b05 ILT |
2191 | addralign_(0), |
2192 | entsize_(0), | |
a445fddf | 2193 | load_address_(0), |
16649710 | 2194 | link_section_(NULL), |
a2fb1b05 | 2195 | link_(0), |
16649710 | 2196 | info_section_(NULL), |
6a74a719 | 2197 | info_symndx_(NULL), |
a2fb1b05 | 2198 | info_(0), |
2ea97941 ILT |
2199 | type_(type), |
2200 | flags_(flags), | |
22f0da72 | 2201 | order_(ORDER_INVALID), |
91ea499d | 2202 | out_shndx_(-1U), |
c06b7b0b ILT |
2203 | symtab_index_(0), |
2204 | dynsym_index_(0), | |
ead1e424 ILT |
2205 | input_sections_(), |
2206 | first_input_offset_(0), | |
c51e6221 | 2207 | fills_(), |
96803768 | 2208 | postprocessing_buffer_(NULL), |
a3ad94ed | 2209 | needs_symtab_index_(false), |
16649710 ILT |
2210 | needs_dynsym_index_(false), |
2211 | should_link_to_symtab_(false), | |
730cdc88 | 2212 | should_link_to_dynsym_(false), |
27bc2bce | 2213 | after_input_sections_(false), |
7bf1f802 | 2214 | requires_postprocessing_(false), |
a445fddf ILT |
2215 | found_in_sections_clause_(false), |
2216 | has_load_address_(false), | |
755ab8af | 2217 | info_uses_section_index_(false), |
6e9ba2ca | 2218 | input_section_order_specified_(false), |
2fd32231 ILT |
2219 | may_sort_attached_input_sections_(false), |
2220 | must_sort_attached_input_sections_(false), | |
2221 | attached_input_sections_are_sorted_(false), | |
9f1d377b | 2222 | is_relro_(false), |
8a5e3e08 ILT |
2223 | is_small_section_(false), |
2224 | is_large_section_(false), | |
f5c870d2 | 2225 | generate_code_fills_at_write_(false), |
e8cd95c7 | 2226 | is_entsize_zero_(false), |
8923b24c | 2227 | section_offsets_need_adjustment_(false), |
1e5d2fb1 | 2228 | is_noload_(false), |
131687b4 | 2229 | always_keeps_input_sections_(false), |
cdc29364 | 2230 | has_fixed_layout_(false), |
9fbd3822 | 2231 | is_patch_space_allowed_(false), |
20e6d0d6 | 2232 | tls_offset_(0), |
c0a62865 | 2233 | checkpoint_(NULL), |
cdc29364 | 2234 | lookup_maps_(new Output_section_lookup_maps), |
9fbd3822 | 2235 | free_list_(), |
8ea8cd50 | 2236 | free_space_fill_(NULL), |
9fbd3822 | 2237 | patch_space_(0) |
a2fb1b05 | 2238 | { |
27bc2bce ILT |
2239 | // An unallocated section has no address. Forcing this means that |
2240 | // we don't need special treatment for symbols defined in debug | |
2241 | // sections. | |
2ea97941 | 2242 | if ((flags & elfcpp::SHF_ALLOC) == 0) |
27bc2bce | 2243 | this->set_address(0); |
a2fb1b05 ILT |
2244 | } |
2245 | ||
54dc6425 ILT |
2246 | Output_section::~Output_section() |
2247 | { | |
20e6d0d6 | 2248 | delete this->checkpoint_; |
54dc6425 ILT |
2249 | } |
2250 | ||
16649710 ILT |
2251 | // Set the entry size. |
2252 | ||
2253 | void | |
2254 | Output_section::set_entsize(uint64_t v) | |
2255 | { | |
e8cd95c7 ILT |
2256 | if (this->is_entsize_zero_) |
2257 | ; | |
2258 | else if (this->entsize_ == 0) | |
16649710 | 2259 | this->entsize_ = v; |
e8cd95c7 ILT |
2260 | else if (this->entsize_ != v) |
2261 | { | |
2262 | this->entsize_ = 0; | |
2263 | this->is_entsize_zero_ = 1; | |
2264 | } | |
16649710 ILT |
2265 | } |
2266 | ||
ead1e424 | 2267 | // Add the input section SHNDX, with header SHDR, named SECNAME, in |
730cdc88 ILT |
2268 | // OBJECT, to the Output_section. RELOC_SHNDX is the index of a |
2269 | // relocation section which applies to this section, or 0 if none, or | |
2270 | // -1U if more than one. Return the offset of the input section | |
2271 | // within the output section. Return -1 if the input section will | |
2272 | // receive special handling. In the normal case we don't always keep | |
2273 | // track of input sections for an Output_section. Instead, each | |
2274 | // Object keeps track of the Output_section for each of its input | |
a445fddf ILT |
2275 | // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep |
2276 | // track of input sections here; this is used when SECTIONS appears in | |
2277 | // a linker script. | |
a2fb1b05 ILT |
2278 | |
2279 | template<int size, bool big_endian> | |
2280 | off_t | |
6e9ba2ca | 2281 | Output_section::add_input_section(Layout* layout, |
6fa2a40b | 2282 | Sized_relobj_file<size, big_endian>* object, |
2ea97941 | 2283 | unsigned int shndx, |
ead1e424 | 2284 | const char* secname, |
730cdc88 | 2285 | const elfcpp::Shdr<size, big_endian>& shdr, |
a445fddf ILT |
2286 | unsigned int reloc_shndx, |
2287 | bool have_sections_script) | |
a2fb1b05 | 2288 | { |
2ea97941 ILT |
2289 | elfcpp::Elf_Xword addralign = shdr.get_sh_addralign(); |
2290 | if ((addralign & (addralign - 1)) != 0) | |
a2fb1b05 | 2291 | { |
75f2446e | 2292 | object->error(_("invalid alignment %lu for section \"%s\""), |
2ea97941 ILT |
2293 | static_cast<unsigned long>(addralign), secname); |
2294 | addralign = 1; | |
a2fb1b05 | 2295 | } |
a2fb1b05 | 2296 | |
2ea97941 ILT |
2297 | if (addralign > this->addralign_) |
2298 | this->addralign_ = addralign; | |
a2fb1b05 | 2299 | |
44a43cf9 | 2300 | typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags(); |
2ea97941 | 2301 | uint64_t entsize = shdr.get_sh_entsize(); |
44a43cf9 ILT |
2302 | |
2303 | // .debug_str is a mergeable string section, but is not always so | |
2304 | // marked by compilers. Mark manually here so we can optimize. | |
2305 | if (strcmp(secname, ".debug_str") == 0) | |
4f833eee ILT |
2306 | { |
2307 | sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS); | |
2ea97941 | 2308 | entsize = 1; |
4f833eee | 2309 | } |
44a43cf9 | 2310 | |
e8cd95c7 ILT |
2311 | this->update_flags_for_input_section(sh_flags); |
2312 | this->set_entsize(entsize); | |
2313 | ||
b8e6aad9 | 2314 | // If this is a SHF_MERGE section, we pass all the input sections to |
730cdc88 | 2315 | // a Output_data_merge. We don't try to handle relocations for such |
e0b64032 ILT |
2316 | // a section. We don't try to handle empty merge sections--they |
2317 | // mess up the mappings, and are useless anyhow. | |
cdc29364 | 2318 | // FIXME: Need to handle merge sections during incremental update. |
44a43cf9 | 2319 | if ((sh_flags & elfcpp::SHF_MERGE) != 0 |
e0b64032 | 2320 | && reloc_shndx == 0 |
cdc29364 CC |
2321 | && shdr.get_sh_size() > 0 |
2322 | && !parameters->incremental()) | |
b8e6aad9 | 2323 | { |
0439c796 DK |
2324 | // Keep information about merged input sections for rebuilding fast |
2325 | // lookup maps if we have sections-script or we do relaxation. | |
131687b4 DK |
2326 | bool keeps_input_sections = (this->always_keeps_input_sections_ |
2327 | || have_sections_script | |
2328 | || parameters->target().may_relax()); | |
2329 | ||
0439c796 DK |
2330 | if (this->add_merge_input_section(object, shndx, sh_flags, entsize, |
2331 | addralign, keeps_input_sections)) | |
b8e6aad9 ILT |
2332 | { |
2333 | // Tell the relocation routines that they need to call the | |
730cdc88 | 2334 | // output_offset method to determine the final address. |
b8e6aad9 ILT |
2335 | return -1; |
2336 | } | |
2337 | } | |
2338 | ||
cdc29364 CC |
2339 | section_size_type input_section_size = shdr.get_sh_size(); |
2340 | section_size_type uncompressed_size; | |
2341 | if (object->section_is_compressed(shndx, &uncompressed_size)) | |
2342 | input_section_size = uncompressed_size; | |
2343 | ||
2344 | off_t offset_in_section; | |
2345 | off_t aligned_offset_in_section; | |
2346 | if (this->has_fixed_layout()) | |
2347 | { | |
2348 | // For incremental updates, find a chunk of unused space in the section. | |
2349 | offset_in_section = this->free_list_.allocate(input_section_size, | |
2350 | addralign, 0); | |
2351 | if (offset_in_section == -1) | |
9fbd3822 CC |
2352 | gold_fallback(_("out of patch space in section %s; " |
2353 | "relink with --incremental-full"), | |
2354 | this->name()); | |
cdc29364 CC |
2355 | aligned_offset_in_section = offset_in_section; |
2356 | } | |
2357 | else | |
2358 | { | |
2359 | offset_in_section = this->current_data_size_for_child(); | |
2360 | aligned_offset_in_section = align_address(offset_in_section, | |
2361 | addralign); | |
2362 | this->set_current_data_size_for_child(aligned_offset_in_section | |
2363 | + input_section_size); | |
2364 | } | |
c51e6221 | 2365 | |
c0a62865 DK |
2366 | // Determine if we want to delay code-fill generation until the output |
2367 | // section is written. When the target is relaxing, we want to delay fill | |
4cf7a849 ST |
2368 | // generating to avoid adjusting them during relaxation. Also, if we are |
2369 | // sorting input sections we must delay fill generation. | |
c0a62865 DK |
2370 | if (!this->generate_code_fills_at_write_ |
2371 | && !have_sections_script | |
2372 | && (sh_flags & elfcpp::SHF_EXECINSTR) != 0 | |
2373 | && parameters->target().has_code_fill() | |
4cf7a849 | 2374 | && (parameters->target().may_relax() |
e9552f7e | 2375 | || layout->is_section_ordering_specified())) |
c0a62865 DK |
2376 | { |
2377 | gold_assert(this->fills_.empty()); | |
2378 | this->generate_code_fills_at_write_ = true; | |
2379 | } | |
2380 | ||
c51e6221 | 2381 | if (aligned_offset_in_section > offset_in_section |
c0a62865 | 2382 | && !this->generate_code_fills_at_write_ |
a445fddf | 2383 | && !have_sections_script |
44a43cf9 | 2384 | && (sh_flags & elfcpp::SHF_EXECINSTR) != 0 |
029ba973 | 2385 | && parameters->target().has_code_fill()) |
c51e6221 ILT |
2386 | { |
2387 | // We need to add some fill data. Using fill_list_ when | |
2388 | // possible is an optimization, since we will often have fill | |
2389 | // sections without input sections. | |
2390 | off_t fill_len = aligned_offset_in_section - offset_in_section; | |
2391 | if (this->input_sections_.empty()) | |
2392 | this->fills_.push_back(Fill(offset_in_section, fill_len)); | |
2393 | else | |
2394 | { | |
029ba973 | 2395 | std::string fill_data(parameters->target().code_fill(fill_len)); |
c51e6221 ILT |
2396 | Output_data_const* odc = new Output_data_const(fill_data, 1); |
2397 | this->input_sections_.push_back(Input_section(odc)); | |
2398 | } | |
2399 | } | |
2400 | ||
ead1e424 | 2401 | // We need to keep track of this section if we are already keeping |
2fd32231 ILT |
2402 | // track of sections, or if we are relaxing. Also, if this is a |
2403 | // section which requires sorting, or which may require sorting in | |
6e9ba2ca ST |
2404 | // the future, we keep track of the sections. If the |
2405 | // --section-ordering-file option is used to specify the order of | |
2406 | // sections, we need to keep track of sections. | |
131687b4 DK |
2407 | if (this->always_keeps_input_sections_ |
2408 | || have_sections_script | |
2fd32231 ILT |
2409 | || !this->input_sections_.empty() |
2410 | || this->may_sort_attached_input_sections() | |
7d9e3d98 | 2411 | || this->must_sort_attached_input_sections() |
20e6d0d6 | 2412 | || parameters->options().user_set_Map() |
6e9ba2ca | 2413 | || parameters->target().may_relax() |
e9552f7e | 2414 | || layout->is_section_ordering_specified()) |
6e9ba2ca | 2415 | { |
6fc6ea19 | 2416 | Input_section isecn(object, shndx, input_section_size, addralign); |
f0558624 ST |
2417 | /* If section ordering is requested by specifying a ordering file, |
2418 | using --section-ordering-file, match the section name with | |
2419 | a pattern. */ | |
2420 | if (parameters->options().section_ordering_file()) | |
6e9ba2ca ST |
2421 | { |
2422 | unsigned int section_order_index = | |
2423 | layout->find_section_order_index(std::string(secname)); | |
2424 | if (section_order_index != 0) | |
2425 | { | |
2426 | isecn.set_section_order_index(section_order_index); | |
2427 | this->set_input_section_order_specified(); | |
2428 | } | |
2429 | } | |
cdc29364 CC |
2430 | if (this->has_fixed_layout()) |
2431 | { | |
2432 | // For incremental updates, finalize the address and offset now. | |
2433 | uint64_t addr = this->address(); | |
2434 | isecn.set_address_and_file_offset(addr + aligned_offset_in_section, | |
2435 | aligned_offset_in_section, | |
2436 | this->offset()); | |
2437 | } | |
6e9ba2ca ST |
2438 | this->input_sections_.push_back(isecn); |
2439 | } | |
54dc6425 | 2440 | |
c51e6221 | 2441 | return aligned_offset_in_section; |
61ba1cf9 ILT |
2442 | } |
2443 | ||
ead1e424 ILT |
2444 | // Add arbitrary data to an output section. |
2445 | ||
2446 | void | |
2447 | Output_section::add_output_section_data(Output_section_data* posd) | |
2448 | { | |
b8e6aad9 ILT |
2449 | Input_section inp(posd); |
2450 | this->add_output_section_data(&inp); | |
a445fddf ILT |
2451 | |
2452 | if (posd->is_data_size_valid()) | |
2453 | { | |
cdc29364 CC |
2454 | off_t offset_in_section; |
2455 | if (this->has_fixed_layout()) | |
2456 | { | |
2457 | // For incremental updates, find a chunk of unused space. | |
2458 | offset_in_section = this->free_list_.allocate(posd->data_size(), | |
2459 | posd->addralign(), 0); | |
2460 | if (offset_in_section == -1) | |
9fbd3822 CC |
2461 | gold_fallback(_("out of patch space in section %s; " |
2462 | "relink with --incremental-full"), | |
2463 | this->name()); | |
cdc29364 CC |
2464 | // Finalize the address and offset now. |
2465 | uint64_t addr = this->address(); | |
2466 | off_t offset = this->offset(); | |
2467 | posd->set_address_and_file_offset(addr + offset_in_section, | |
2468 | offset + offset_in_section); | |
2469 | } | |
2470 | else | |
2471 | { | |
2472 | offset_in_section = this->current_data_size_for_child(); | |
2473 | off_t aligned_offset_in_section = align_address(offset_in_section, | |
2474 | posd->addralign()); | |
2475 | this->set_current_data_size_for_child(aligned_offset_in_section | |
2476 | + posd->data_size()); | |
2477 | } | |
2478 | } | |
2479 | else if (this->has_fixed_layout()) | |
2480 | { | |
2481 | // For incremental updates, arrange for the data to have a fixed layout. | |
2482 | // This will mean that additions to the data must be allocated from | |
2483 | // free space within the containing output section. | |
2484 | uint64_t addr = this->address(); | |
2485 | posd->set_address(addr); | |
2486 | posd->set_file_offset(0); | |
4829d394 CC |
2487 | // FIXME: This should eventually be unreachable. |
2488 | // gold_unreachable(); | |
a445fddf | 2489 | } |
b8e6aad9 ILT |
2490 | } |
2491 | ||
c0a62865 DK |
2492 | // Add a relaxed input section. |
2493 | ||
2494 | void | |
d06fb4d1 DK |
2495 | Output_section::add_relaxed_input_section(Layout* layout, |
2496 | Output_relaxed_input_section* poris, | |
2497 | const std::string& name) | |
c0a62865 DK |
2498 | { |
2499 | Input_section inp(poris); | |
d06fb4d1 DK |
2500 | |
2501 | // If the --section-ordering-file option is used to specify the order of | |
2502 | // sections, we need to keep track of sections. | |
e9552f7e | 2503 | if (layout->is_section_ordering_specified()) |
d06fb4d1 DK |
2504 | { |
2505 | unsigned int section_order_index = | |
2506 | layout->find_section_order_index(name); | |
2507 | if (section_order_index != 0) | |
2508 | { | |
2509 | inp.set_section_order_index(section_order_index); | |
2510 | this->set_input_section_order_specified(); | |
2511 | } | |
2512 | } | |
2513 | ||
c0a62865 | 2514 | this->add_output_section_data(&inp); |
0439c796 DK |
2515 | if (this->lookup_maps_->is_valid()) |
2516 | this->lookup_maps_->add_relaxed_input_section(poris->relobj(), | |
2517 | poris->shndx(), poris); | |
c0a62865 DK |
2518 | |
2519 | // For a relaxed section, we use the current data size. Linker scripts | |
2520 | // get all the input sections, including relaxed one from an output | |
2521 | // section and add them back to them same output section to compute the | |
2522 | // output section size. If we do not account for sizes of relaxed input | |
2523 | // sections, an output section would be incorrectly sized. | |
2524 | off_t offset_in_section = this->current_data_size_for_child(); | |
2525 | off_t aligned_offset_in_section = align_address(offset_in_section, | |
2526 | poris->addralign()); | |
2527 | this->set_current_data_size_for_child(aligned_offset_in_section | |
2528 | + poris->current_data_size()); | |
2529 | } | |
2530 | ||
b8e6aad9 | 2531 | // Add arbitrary data to an output section by Input_section. |
c06b7b0b | 2532 | |
b8e6aad9 ILT |
2533 | void |
2534 | Output_section::add_output_section_data(Input_section* inp) | |
2535 | { | |
ead1e424 | 2536 | if (this->input_sections_.empty()) |
27bc2bce | 2537 | this->first_input_offset_ = this->current_data_size_for_child(); |
c06b7b0b | 2538 | |
b8e6aad9 | 2539 | this->input_sections_.push_back(*inp); |
c06b7b0b | 2540 | |
2ea97941 ILT |
2541 | uint64_t addralign = inp->addralign(); |
2542 | if (addralign > this->addralign_) | |
2543 | this->addralign_ = addralign; | |
c06b7b0b | 2544 | |
b8e6aad9 ILT |
2545 | inp->set_output_section(this); |
2546 | } | |
2547 | ||
2548 | // Add a merge section to an output section. | |
2549 | ||
2550 | void | |
2551 | Output_section::add_output_merge_section(Output_section_data* posd, | |
2ea97941 | 2552 | bool is_string, uint64_t entsize) |
b8e6aad9 | 2553 | { |
2ea97941 | 2554 | Input_section inp(posd, is_string, entsize); |
b8e6aad9 ILT |
2555 | this->add_output_section_data(&inp); |
2556 | } | |
2557 | ||
2558 | // Add an input section to a SHF_MERGE section. | |
2559 | ||
2560 | bool | |
2ea97941 ILT |
2561 | Output_section::add_merge_input_section(Relobj* object, unsigned int shndx, |
2562 | uint64_t flags, uint64_t entsize, | |
0439c796 DK |
2563 | uint64_t addralign, |
2564 | bool keeps_input_sections) | |
b8e6aad9 | 2565 | { |
2ea97941 | 2566 | bool is_string = (flags & elfcpp::SHF_STRINGS) != 0; |
87f95776 ILT |
2567 | |
2568 | // We only merge strings if the alignment is not more than the | |
2569 | // character size. This could be handled, but it's unusual. | |
2ea97941 | 2570 | if (is_string && addralign > entsize) |
b8e6aad9 ILT |
2571 | return false; |
2572 | ||
20e6d0d6 DK |
2573 | // We cannot restore merged input section states. |
2574 | gold_assert(this->checkpoint_ == NULL); | |
2575 | ||
c0a62865 | 2576 | // Look up merge sections by required properties. |
0439c796 DK |
2577 | // Currently, we only invalidate the lookup maps in script processing |
2578 | // and relaxation. We should not have done either when we reach here. | |
2579 | // So we assume that the lookup maps are valid to simply code. | |
2580 | gold_assert(this->lookup_maps_->is_valid()); | |
2ea97941 | 2581 | Merge_section_properties msp(is_string, entsize, addralign); |
0439c796 DK |
2582 | Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp); |
2583 | bool is_new = false; | |
2584 | if (pomb != NULL) | |
c0a62865 | 2585 | { |
6bf924b0 DK |
2586 | gold_assert(pomb->is_string() == is_string |
2587 | && pomb->entsize() == entsize | |
2588 | && pomb->addralign() == addralign); | |
c0a62865 | 2589 | } |
b8e6aad9 ILT |
2590 | else |
2591 | { | |
6bf924b0 DK |
2592 | // Create a new Output_merge_data or Output_merge_string_data. |
2593 | if (!is_string) | |
2594 | pomb = new Output_merge_data(entsize, addralign); | |
2595 | else | |
9a0910c3 | 2596 | { |
6bf924b0 DK |
2597 | switch (entsize) |
2598 | { | |
2599 | case 1: | |
2600 | pomb = new Output_merge_string<char>(addralign); | |
2601 | break; | |
2602 | case 2: | |
2603 | pomb = new Output_merge_string<uint16_t>(addralign); | |
2604 | break; | |
2605 | case 4: | |
2606 | pomb = new Output_merge_string<uint32_t>(addralign); | |
2607 | break; | |
2608 | default: | |
2609 | return false; | |
2610 | } | |
9a0910c3 | 2611 | } |
0439c796 DK |
2612 | // If we need to do script processing or relaxation, we need to keep |
2613 | // the original input sections to rebuild the fast lookup maps. | |
2614 | if (keeps_input_sections) | |
2615 | pomb->set_keeps_input_sections(); | |
2616 | is_new = true; | |
b8e6aad9 ILT |
2617 | } |
2618 | ||
6bf924b0 DK |
2619 | if (pomb->add_input_section(object, shndx)) |
2620 | { | |
0439c796 DK |
2621 | // Add new merge section to this output section and link merge |
2622 | // section properties to new merge section in map. | |
2623 | if (is_new) | |
2624 | { | |
2625 | this->add_output_merge_section(pomb, is_string, entsize); | |
2626 | this->lookup_maps_->add_merge_section(msp, pomb); | |
2627 | } | |
2628 | ||
6bf924b0 DK |
2629 | // Add input section to new merge section and link input section to new |
2630 | // merge section in map. | |
0439c796 | 2631 | this->lookup_maps_->add_merge_input_section(object, shndx, pomb); |
6bf924b0 DK |
2632 | return true; |
2633 | } | |
2634 | else | |
0439c796 DK |
2635 | { |
2636 | // If add_input_section failed, delete new merge section to avoid | |
2637 | // exporting empty merge sections in Output_section::get_input_section. | |
2638 | if (is_new) | |
2639 | delete pomb; | |
2640 | return false; | |
2641 | } | |
b8e6aad9 ILT |
2642 | } |
2643 | ||
c0a62865 | 2644 | // Build a relaxation map to speed up relaxation of existing input sections. |
2ea97941 | 2645 | // Look up to the first LIMIT elements in INPUT_SECTIONS. |
c0a62865 | 2646 | |
20e6d0d6 | 2647 | void |
c0a62865 | 2648 | Output_section::build_relaxation_map( |
2ea97941 | 2649 | const Input_section_list& input_sections, |
c0a62865 DK |
2650 | size_t limit, |
2651 | Relaxation_map* relaxation_map) const | |
20e6d0d6 | 2652 | { |
c0a62865 DK |
2653 | for (size_t i = 0; i < limit; ++i) |
2654 | { | |
2ea97941 | 2655 | const Input_section& is(input_sections[i]); |
c0a62865 DK |
2656 | if (is.is_input_section() || is.is_relaxed_input_section()) |
2657 | { | |
5ac169d4 DK |
2658 | Section_id sid(is.relobj(), is.shndx()); |
2659 | (*relaxation_map)[sid] = i; | |
c0a62865 DK |
2660 | } |
2661 | } | |
2662 | } | |
2663 | ||
2664 | // Convert regular input sections in INPUT_SECTIONS into relaxed input | |
5ac169d4 DK |
2665 | // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id |
2666 | // indices of INPUT_SECTIONS. | |
20e6d0d6 | 2667 | |
c0a62865 DK |
2668 | void |
2669 | Output_section::convert_input_sections_in_list_to_relaxed_sections( | |
2670 | const std::vector<Output_relaxed_input_section*>& relaxed_sections, | |
2671 | const Relaxation_map& map, | |
2ea97941 | 2672 | Input_section_list* input_sections) |
c0a62865 DK |
2673 | { |
2674 | for (size_t i = 0; i < relaxed_sections.size(); ++i) | |
2675 | { | |
2676 | Output_relaxed_input_section* poris = relaxed_sections[i]; | |
5ac169d4 DK |
2677 | Section_id sid(poris->relobj(), poris->shndx()); |
2678 | Relaxation_map::const_iterator p = map.find(sid); | |
c0a62865 | 2679 | gold_assert(p != map.end()); |
2ea97941 | 2680 | gold_assert((*input_sections)[p->second].is_input_section()); |
d06fb4d1 DK |
2681 | |
2682 | // Remember section order index of original input section | |
2683 | // if it is set. Copy it to the relaxed input section. | |
2684 | unsigned int soi = | |
2685 | (*input_sections)[p->second].section_order_index(); | |
2ea97941 | 2686 | (*input_sections)[p->second] = Input_section(poris); |
d06fb4d1 | 2687 | (*input_sections)[p->second].set_section_order_index(soi); |
c0a62865 DK |
2688 | } |
2689 | } | |
2690 | ||
2691 | // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS | |
2692 | // is a vector of pointers to Output_relaxed_input_section or its derived | |
2693 | // classes. The relaxed sections must correspond to existing input sections. | |
2694 | ||
2695 | void | |
2696 | Output_section::convert_input_sections_to_relaxed_sections( | |
2697 | const std::vector<Output_relaxed_input_section*>& relaxed_sections) | |
2698 | { | |
029ba973 | 2699 | gold_assert(parameters->target().may_relax()); |
20e6d0d6 | 2700 | |
c0a62865 DK |
2701 | // We want to make sure that restore_states does not undo the effect of |
2702 | // this. If there is no checkpoint active, just search the current | |
2703 | // input section list and replace the sections there. If there is | |
2704 | // a checkpoint, also replace the sections there. | |
2705 | ||
2706 | // By default, we look at the whole list. | |
2707 | size_t limit = this->input_sections_.size(); | |
2708 | ||
2709 | if (this->checkpoint_ != NULL) | |
20e6d0d6 | 2710 | { |
c0a62865 DK |
2711 | // Replace input sections with relaxed input section in the saved |
2712 | // copy of the input section list. | |
2713 | if (this->checkpoint_->input_sections_saved()) | |
20e6d0d6 | 2714 | { |
c0a62865 DK |
2715 | Relaxation_map map; |
2716 | this->build_relaxation_map( | |
2717 | *(this->checkpoint_->input_sections()), | |
2718 | this->checkpoint_->input_sections()->size(), | |
2719 | &map); | |
2720 | this->convert_input_sections_in_list_to_relaxed_sections( | |
2721 | relaxed_sections, | |
2722 | map, | |
2723 | this->checkpoint_->input_sections()); | |
2724 | } | |
2725 | else | |
2726 | { | |
2727 | // We have not copied the input section list yet. Instead, just | |
2728 | // look at the portion that would be saved. | |
2729 | limit = this->checkpoint_->input_sections_size(); | |
20e6d0d6 | 2730 | } |
20e6d0d6 | 2731 | } |
c0a62865 DK |
2732 | |
2733 | // Convert input sections in input_section_list. | |
2734 | Relaxation_map map; | |
2735 | this->build_relaxation_map(this->input_sections_, limit, &map); | |
2736 | this->convert_input_sections_in_list_to_relaxed_sections( | |
2737 | relaxed_sections, | |
2738 | map, | |
2739 | &this->input_sections_); | |
41263c05 DK |
2740 | |
2741 | // Update fast look-up map. | |
0439c796 | 2742 | if (this->lookup_maps_->is_valid()) |
41263c05 DK |
2743 | for (size_t i = 0; i < relaxed_sections.size(); ++i) |
2744 | { | |
2745 | Output_relaxed_input_section* poris = relaxed_sections[i]; | |
0439c796 DK |
2746 | this->lookup_maps_->add_relaxed_input_section(poris->relobj(), |
2747 | poris->shndx(), poris); | |
41263c05 | 2748 | } |
20e6d0d6 DK |
2749 | } |
2750 | ||
9c547ec3 ILT |
2751 | // Update the output section flags based on input section flags. |
2752 | ||
2753 | void | |
2ea97941 | 2754 | Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags) |
9c547ec3 ILT |
2755 | { |
2756 | // If we created the section with SHF_ALLOC clear, we set the | |
2757 | // address. If we are now setting the SHF_ALLOC flag, we need to | |
2758 | // undo that. | |
2759 | if ((this->flags_ & elfcpp::SHF_ALLOC) == 0 | |
2ea97941 | 2760 | && (flags & elfcpp::SHF_ALLOC) != 0) |
9c547ec3 ILT |
2761 | this->mark_address_invalid(); |
2762 | ||
2ea97941 | 2763 | this->flags_ |= (flags |
9c547ec3 ILT |
2764 | & (elfcpp::SHF_WRITE |
2765 | | elfcpp::SHF_ALLOC | |
2766 | | elfcpp::SHF_EXECINSTR)); | |
e8cd95c7 ILT |
2767 | |
2768 | if ((flags & elfcpp::SHF_MERGE) == 0) | |
2769 | this->flags_ &=~ elfcpp::SHF_MERGE; | |
2770 | else | |
2771 | { | |
2772 | if (this->current_data_size_for_child() == 0) | |
2773 | this->flags_ |= elfcpp::SHF_MERGE; | |
2774 | } | |
2775 | ||
2776 | if ((flags & elfcpp::SHF_STRINGS) == 0) | |
2777 | this->flags_ &=~ elfcpp::SHF_STRINGS; | |
2778 | else | |
2779 | { | |
2780 | if (this->current_data_size_for_child() == 0) | |
2781 | this->flags_ |= elfcpp::SHF_STRINGS; | |
2782 | } | |
9c547ec3 ILT |
2783 | } |
2784 | ||
2ea97941 | 2785 | // Find the merge section into which an input section with index SHNDX in |
c0a62865 DK |
2786 | // OBJECT has been added. Return NULL if none found. |
2787 | ||
2788 | Output_section_data* | |
2789 | Output_section::find_merge_section(const Relobj* object, | |
2ea97941 | 2790 | unsigned int shndx) const |
c0a62865 | 2791 | { |
0439c796 DK |
2792 | if (!this->lookup_maps_->is_valid()) |
2793 | this->build_lookup_maps(); | |
2794 | return this->lookup_maps_->find_merge_section(object, shndx); | |
2795 | } | |
2796 | ||
2797 | // Build the lookup maps for merge and relaxed sections. This is needs | |
2798 | // to be declared as a const methods so that it is callable with a const | |
2799 | // Output_section pointer. The method only updates states of the maps. | |
2800 | ||
2801 | void | |
2802 | Output_section::build_lookup_maps() const | |
2803 | { | |
2804 | this->lookup_maps_->clear(); | |
2805 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); | |
2806 | p != this->input_sections_.end(); | |
2807 | ++p) | |
c0a62865 | 2808 | { |
0439c796 DK |
2809 | if (p->is_merge_section()) |
2810 | { | |
2811 | Output_merge_base* pomb = p->output_merge_base(); | |
2812 | Merge_section_properties msp(pomb->is_string(), pomb->entsize(), | |
2813 | pomb->addralign()); | |
2814 | this->lookup_maps_->add_merge_section(msp, pomb); | |
2815 | for (Output_merge_base::Input_sections::const_iterator is = | |
2816 | pomb->input_sections_begin(); | |
2817 | is != pomb->input_sections_end(); | |
2818 | ++is) | |
2819 | { | |
2820 | const Const_section_id& csid = *is; | |
2821 | this->lookup_maps_->add_merge_input_section(csid.first, | |
2822 | csid.second, pomb); | |
2823 | } | |
2824 | ||
2825 | } | |
2826 | else if (p->is_relaxed_input_section()) | |
2827 | { | |
2828 | Output_relaxed_input_section* poris = p->relaxed_input_section(); | |
2829 | this->lookup_maps_->add_relaxed_input_section(poris->relobj(), | |
2830 | poris->shndx(), poris); | |
2831 | } | |
c0a62865 | 2832 | } |
c0a62865 DK |
2833 | } |
2834 | ||
2835 | // Find an relaxed input section corresponding to an input section | |
2ea97941 | 2836 | // in OBJECT with index SHNDX. |
c0a62865 | 2837 | |
d6344fb5 | 2838 | const Output_relaxed_input_section* |
c0a62865 | 2839 | Output_section::find_relaxed_input_section(const Relobj* object, |
2ea97941 | 2840 | unsigned int shndx) const |
c0a62865 | 2841 | { |
0439c796 DK |
2842 | if (!this->lookup_maps_->is_valid()) |
2843 | this->build_lookup_maps(); | |
2844 | return this->lookup_maps_->find_relaxed_input_section(object, shndx); | |
c0a62865 DK |
2845 | } |
2846 | ||
2ea97941 ILT |
2847 | // Given an address OFFSET relative to the start of input section |
2848 | // SHNDX in OBJECT, return whether this address is being included in | |
2849 | // the final link. This should only be called if SHNDX in OBJECT has | |
730cdc88 ILT |
2850 | // a special mapping. |
2851 | ||
2852 | bool | |
2853 | Output_section::is_input_address_mapped(const Relobj* object, | |
2ea97941 ILT |
2854 | unsigned int shndx, |
2855 | off_t offset) const | |
730cdc88 | 2856 | { |
c0a62865 | 2857 | // Look at the Output_section_data_maps first. |
2ea97941 | 2858 | const Output_section_data* posd = this->find_merge_section(object, shndx); |
c0a62865 | 2859 | if (posd == NULL) |
2ea97941 | 2860 | posd = this->find_relaxed_input_section(object, shndx); |
c0a62865 DK |
2861 | |
2862 | if (posd != NULL) | |
2863 | { | |
2ea97941 ILT |
2864 | section_offset_type output_offset; |
2865 | bool found = posd->output_offset(object, shndx, offset, &output_offset); | |
c0a62865 | 2866 | gold_assert(found); |
2ea97941 | 2867 | return output_offset != -1; |
c0a62865 DK |
2868 | } |
2869 | ||
2870 | // Fall back to the slow look-up. | |
730cdc88 ILT |
2871 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); |
2872 | p != this->input_sections_.end(); | |
2873 | ++p) | |
2874 | { | |
2ea97941 ILT |
2875 | section_offset_type output_offset; |
2876 | if (p->output_offset(object, shndx, offset, &output_offset)) | |
2877 | return output_offset != -1; | |
730cdc88 ILT |
2878 | } |
2879 | ||
2880 | // By default we assume that the address is mapped. This should | |
2881 | // only be called after we have passed all sections to Layout. At | |
2882 | // that point we should know what we are discarding. | |
2883 | return true; | |
2884 | } | |
2885 | ||
2ea97941 ILT |
2886 | // Given an address OFFSET relative to the start of input section |
2887 | // SHNDX in object OBJECT, return the output offset relative to the | |
1e983657 | 2888 | // start of the input section in the output section. This should only |
2ea97941 | 2889 | // be called if SHNDX in OBJECT has a special mapping. |
730cdc88 | 2890 | |
8383303e | 2891 | section_offset_type |
2ea97941 ILT |
2892 | Output_section::output_offset(const Relobj* object, unsigned int shndx, |
2893 | section_offset_type offset) const | |
730cdc88 | 2894 | { |
c0a62865 DK |
2895 | // This can only be called meaningfully when we know the data size |
2896 | // of this. | |
2897 | gold_assert(this->is_data_size_valid()); | |
730cdc88 | 2898 | |
c0a62865 | 2899 | // Look at the Output_section_data_maps first. |
2ea97941 | 2900 | const Output_section_data* posd = this->find_merge_section(object, shndx); |
c0a62865 | 2901 | if (posd == NULL) |
2ea97941 | 2902 | posd = this->find_relaxed_input_section(object, shndx); |
c0a62865 DK |
2903 | if (posd != NULL) |
2904 | { | |
2ea97941 ILT |
2905 | section_offset_type output_offset; |
2906 | bool found = posd->output_offset(object, shndx, offset, &output_offset); | |
c0a62865 | 2907 | gold_assert(found); |
2ea97941 | 2908 | return output_offset; |
c0a62865 DK |
2909 | } |
2910 | ||
2911 | // Fall back to the slow look-up. | |
730cdc88 ILT |
2912 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); |
2913 | p != this->input_sections_.end(); | |
2914 | ++p) | |
2915 | { | |
2ea97941 ILT |
2916 | section_offset_type output_offset; |
2917 | if (p->output_offset(object, shndx, offset, &output_offset)) | |
2918 | return output_offset; | |
730cdc88 ILT |
2919 | } |
2920 | gold_unreachable(); | |
2921 | } | |
2922 | ||
2ea97941 ILT |
2923 | // Return the output virtual address of OFFSET relative to the start |
2924 | // of input section SHNDX in object OBJECT. | |
b8e6aad9 ILT |
2925 | |
2926 | uint64_t | |
2ea97941 ILT |
2927 | Output_section::output_address(const Relobj* object, unsigned int shndx, |
2928 | off_t offset) const | |
b8e6aad9 ILT |
2929 | { |
2930 | uint64_t addr = this->address() + this->first_input_offset_; | |
c0a62865 DK |
2931 | |
2932 | // Look at the Output_section_data_maps first. | |
2ea97941 | 2933 | const Output_section_data* posd = this->find_merge_section(object, shndx); |
c0a62865 | 2934 | if (posd == NULL) |
2ea97941 | 2935 | posd = this->find_relaxed_input_section(object, shndx); |
c0a62865 DK |
2936 | if (posd != NULL && posd->is_address_valid()) |
2937 | { | |
2ea97941 ILT |
2938 | section_offset_type output_offset; |
2939 | bool found = posd->output_offset(object, shndx, offset, &output_offset); | |
c0a62865 | 2940 | gold_assert(found); |
2ea97941 | 2941 | return posd->address() + output_offset; |
c0a62865 DK |
2942 | } |
2943 | ||
2944 | // Fall back to the slow look-up. | |
b8e6aad9 ILT |
2945 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); |
2946 | p != this->input_sections_.end(); | |
2947 | ++p) | |
2948 | { | |
2949 | addr = align_address(addr, p->addralign()); | |
2ea97941 ILT |
2950 | section_offset_type output_offset; |
2951 | if (p->output_offset(object, shndx, offset, &output_offset)) | |
730cdc88 | 2952 | { |
2ea97941 | 2953 | if (output_offset == -1) |
eff45813 | 2954 | return -1ULL; |
2ea97941 | 2955 | return addr + output_offset; |
730cdc88 | 2956 | } |
b8e6aad9 ILT |
2957 | addr += p->data_size(); |
2958 | } | |
2959 | ||
2960 | // If we get here, it means that we don't know the mapping for this | |
2961 | // input section. This might happen in principle if | |
2962 | // add_input_section were called before add_output_section_data. | |
2963 | // But it should never actually happen. | |
2964 | ||
2965 | gold_unreachable(); | |
ead1e424 ILT |
2966 | } |
2967 | ||
e29e076a | 2968 | // Find the output address of the start of the merged section for |
2ea97941 | 2969 | // input section SHNDX in object OBJECT. |
a9a60db6 | 2970 | |
e29e076a ILT |
2971 | bool |
2972 | Output_section::find_starting_output_address(const Relobj* object, | |
2ea97941 | 2973 | unsigned int shndx, |
e29e076a | 2974 | uint64_t* paddr) const |
a9a60db6 | 2975 | { |
c0a62865 DK |
2976 | // FIXME: This becomes a bottle-neck if we have many relaxed sections. |
2977 | // Looking up the merge section map does not always work as we sometimes | |
2978 | // find a merge section without its address set. | |
a9a60db6 ILT |
2979 | uint64_t addr = this->address() + this->first_input_offset_; |
2980 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); | |
2981 | p != this->input_sections_.end(); | |
2982 | ++p) | |
2983 | { | |
2984 | addr = align_address(addr, p->addralign()); | |
2985 | ||
2986 | // It would be nice if we could use the existing output_offset | |
2987 | // method to get the output offset of input offset 0. | |
2988 | // Unfortunately we don't know for sure that input offset 0 is | |
2989 | // mapped at all. | |
2ea97941 | 2990 | if (p->is_merge_section_for(object, shndx)) |
e29e076a ILT |
2991 | { |
2992 | *paddr = addr; | |
2993 | return true; | |
2994 | } | |
a9a60db6 ILT |
2995 | |
2996 | addr += p->data_size(); | |
2997 | } | |
e29e076a ILT |
2998 | |
2999 | // We couldn't find a merge output section for this input section. | |
3000 | return false; | |
a9a60db6 ILT |
3001 | } |
3002 | ||
cdc29364 CC |
3003 | // Update the data size of an Output_section. |
3004 | ||
3005 | void | |
3006 | Output_section::update_data_size() | |
3007 | { | |
3008 | if (this->input_sections_.empty()) | |
3009 | return; | |
3010 | ||
3011 | if (this->must_sort_attached_input_sections() | |
3012 | || this->input_section_order_specified()) | |
3013 | this->sort_attached_input_sections(); | |
3014 | ||
3015 | off_t off = this->first_input_offset_; | |
3016 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
3017 | p != this->input_sections_.end(); | |
3018 | ++p) | |
3019 | { | |
3020 | off = align_address(off, p->addralign()); | |
3021 | off += p->current_data_size(); | |
3022 | } | |
3023 | ||
3024 | this->set_current_data_size_for_child(off); | |
3025 | } | |
3026 | ||
27bc2bce | 3027 | // Set the data size of an Output_section. This is where we handle |
ead1e424 ILT |
3028 | // setting the addresses of any Output_section_data objects. |
3029 | ||
3030 | void | |
27bc2bce | 3031 | Output_section::set_final_data_size() |
ead1e424 | 3032 | { |
9fbd3822 CC |
3033 | off_t data_size; |
3034 | ||
ead1e424 | 3035 | if (this->input_sections_.empty()) |
9fbd3822 CC |
3036 | data_size = this->current_data_size_for_child(); |
3037 | else | |
27bc2bce | 3038 | { |
9fbd3822 CC |
3039 | if (this->must_sort_attached_input_sections() |
3040 | || this->input_section_order_specified()) | |
3041 | this->sort_attached_input_sections(); | |
ead1e424 | 3042 | |
9fbd3822 CC |
3043 | uint64_t address = this->address(); |
3044 | off_t startoff = this->offset(); | |
3045 | off_t off = startoff + this->first_input_offset_; | |
3046 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
3047 | p != this->input_sections_.end(); | |
3048 | ++p) | |
3049 | { | |
3050 | off = align_address(off, p->addralign()); | |
3051 | p->set_address_and_file_offset(address + (off - startoff), off, | |
3052 | startoff); | |
3053 | off += p->data_size(); | |
3054 | } | |
3055 | data_size = off - startoff; | |
3056 | } | |
2fd32231 | 3057 | |
9fbd3822 CC |
3058 | // For full incremental links, we want to allocate some patch space |
3059 | // in most sections for subsequent incremental updates. | |
3060 | if (this->is_patch_space_allowed_ && parameters->incremental_full()) | |
ead1e424 | 3061 | { |
9fbd3822 | 3062 | double pct = parameters->options().incremental_patch(); |
8ea8cd50 CC |
3063 | size_t extra = static_cast<size_t>(data_size * pct); |
3064 | if (this->free_space_fill_ != NULL | |
3065 | && this->free_space_fill_->minimum_hole_size() > extra) | |
3066 | extra = this->free_space_fill_->minimum_hole_size(); | |
9fbd3822 CC |
3067 | off_t new_size = align_address(data_size + extra, this->addralign()); |
3068 | this->patch_space_ = new_size - data_size; | |
3069 | gold_debug(DEBUG_INCREMENTAL, | |
3070 | "set_final_data_size: %08lx + %08lx: section %s", | |
3071 | static_cast<long>(data_size), | |
3072 | static_cast<long>(this->patch_space_), | |
3073 | this->name()); | |
3074 | data_size = new_size; | |
ead1e424 ILT |
3075 | } |
3076 | ||
9fbd3822 | 3077 | this->set_data_size(data_size); |
ead1e424 | 3078 | } |
9a0910c3 | 3079 | |
a445fddf ILT |
3080 | // Reset the address and file offset. |
3081 | ||
3082 | void | |
3083 | Output_section::do_reset_address_and_file_offset() | |
3084 | { | |
20e6d0d6 DK |
3085 | // An unallocated section has no address. Forcing this means that |
3086 | // we don't need special treatment for symbols defined in debug | |
1e5d2fb1 DK |
3087 | // sections. We do the same in the constructor. This does not |
3088 | // apply to NOLOAD sections though. | |
3089 | if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_) | |
20e6d0d6 DK |
3090 | this->set_address(0); |
3091 | ||
a445fddf ILT |
3092 | for (Input_section_list::iterator p = this->input_sections_.begin(); |
3093 | p != this->input_sections_.end(); | |
3094 | ++p) | |
3095 | p->reset_address_and_file_offset(); | |
9fbd3822 CC |
3096 | |
3097 | // Remove any patch space that was added in set_final_data_size. | |
3098 | if (this->patch_space_ > 0) | |
3099 | { | |
3100 | this->set_current_data_size_for_child(this->current_data_size_for_child() | |
3101 | - this->patch_space_); | |
3102 | this->patch_space_ = 0; | |
3103 | } | |
a445fddf | 3104 | } |
9fbd3822 | 3105 | |
20e6d0d6 DK |
3106 | // Return true if address and file offset have the values after reset. |
3107 | ||
3108 | bool | |
3109 | Output_section::do_address_and_file_offset_have_reset_values() const | |
3110 | { | |
3111 | if (this->is_offset_valid()) | |
3112 | return false; | |
3113 | ||
3114 | // An unallocated section has address 0 after its construction or a reset. | |
3115 | if ((this->flags_ & elfcpp::SHF_ALLOC) == 0) | |
3116 | return this->is_address_valid() && this->address() == 0; | |
3117 | else | |
3118 | return !this->is_address_valid(); | |
3119 | } | |
a445fddf | 3120 | |
7bf1f802 ILT |
3121 | // Set the TLS offset. Called only for SHT_TLS sections. |
3122 | ||
3123 | void | |
3124 | Output_section::do_set_tls_offset(uint64_t tls_base) | |
3125 | { | |
3126 | this->tls_offset_ = this->address() - tls_base; | |
3127 | } | |
3128 | ||
2fd32231 ILT |
3129 | // In a few cases we need to sort the input sections attached to an |
3130 | // output section. This is used to implement the type of constructor | |
3131 | // priority ordering implemented by the GNU linker, in which the | |
3132 | // priority becomes part of the section name and the sections are | |
3133 | // sorted by name. We only do this for an output section if we see an | |
5393d741 | 3134 | // attached input section matching ".ctors.*", ".dtors.*", |
2fd32231 ILT |
3135 | // ".init_array.*" or ".fini_array.*". |
3136 | ||
3137 | class Output_section::Input_section_sort_entry | |
3138 | { | |
3139 | public: | |
3140 | Input_section_sort_entry() | |
3141 | : input_section_(), index_(-1U), section_has_name_(false), | |
3142 | section_name_() | |
3143 | { } | |
3144 | ||
2ea97941 | 3145 | Input_section_sort_entry(const Input_section& input_section, |
6e9ba2ca ST |
3146 | unsigned int index, |
3147 | bool must_sort_attached_input_sections) | |
2ea97941 ILT |
3148 | : input_section_(input_section), index_(index), |
3149 | section_has_name_(input_section.is_input_section() | |
3150 | || input_section.is_relaxed_input_section()) | |
2fd32231 | 3151 | { |
6e9ba2ca ST |
3152 | if (this->section_has_name_ |
3153 | && must_sort_attached_input_sections) | |
2fd32231 ILT |
3154 | { |
3155 | // This is only called single-threaded from Layout::finalize, | |
3156 | // so it is OK to lock. Unfortunately we have no way to pass | |
3157 | // in a Task token. | |
3158 | const Task* dummy_task = reinterpret_cast<const Task*>(-1); | |
2ea97941 ILT |
3159 | Object* obj = (input_section.is_input_section() |
3160 | ? input_section.relobj() | |
3161 | : input_section.relaxed_input_section()->relobj()); | |
2fd32231 ILT |
3162 | Task_lock_obj<Object> tl(dummy_task, obj); |
3163 | ||
3164 | // This is a slow operation, which should be cached in | |
3165 | // Layout::layout if this becomes a speed problem. | |
2ea97941 | 3166 | this->section_name_ = obj->section_name(input_section.shndx()); |
2fd32231 ILT |
3167 | } |
3168 | } | |
3169 | ||
3170 | // Return the Input_section. | |
3171 | const Input_section& | |
3172 | input_section() const | |
3173 | { | |
3174 | gold_assert(this->index_ != -1U); | |
3175 | return this->input_section_; | |
3176 | } | |
3177 | ||
3178 | // The index of this entry in the original list. This is used to | |
3179 | // make the sort stable. | |
3180 | unsigned int | |
3181 | index() const | |
3182 | { | |
3183 | gold_assert(this->index_ != -1U); | |
3184 | return this->index_; | |
3185 | } | |
3186 | ||
3187 | // Whether there is a section name. | |
3188 | bool | |
3189 | section_has_name() const | |
3190 | { return this->section_has_name_; } | |
3191 | ||
3192 | // The section name. | |
3193 | const std::string& | |
3194 | section_name() const | |
3195 | { | |
3196 | gold_assert(this->section_has_name_); | |
3197 | return this->section_name_; | |
3198 | } | |
3199 | ||
ab794b6b ILT |
3200 | // Return true if the section name has a priority. This is assumed |
3201 | // to be true if it has a dot after the initial dot. | |
2fd32231 | 3202 | bool |
ab794b6b | 3203 | has_priority() const |
2fd32231 ILT |
3204 | { |
3205 | gold_assert(this->section_has_name_); | |
2a0ff005 | 3206 | return this->section_name_.find('.', 1) != std::string::npos; |
2fd32231 ILT |
3207 | } |
3208 | ||
5393d741 ILT |
3209 | // Return the priority. Believe it or not, gcc encodes the priority |
3210 | // differently for .ctors/.dtors and .init_array/.fini_array | |
3211 | // sections. | |
3212 | unsigned int | |
3213 | get_priority() const | |
3214 | { | |
3215 | gold_assert(this->section_has_name_); | |
3216 | bool is_ctors; | |
3217 | if (is_prefix_of(".ctors.", this->section_name_.c_str()) | |
3218 | || is_prefix_of(".dtors.", this->section_name_.c_str())) | |
3219 | is_ctors = true; | |
3220 | else if (is_prefix_of(".init_array.", this->section_name_.c_str()) | |
3221 | || is_prefix_of(".fini_array.", this->section_name_.c_str())) | |
3222 | is_ctors = false; | |
3223 | else | |
3224 | return 0; | |
3225 | char* end; | |
3226 | unsigned long prio = strtoul((this->section_name_.c_str() | |
3227 | + (is_ctors ? 7 : 12)), | |
3228 | &end, 10); | |
3229 | if (*end != '\0') | |
3230 | return 0; | |
3231 | else if (is_ctors) | |
3232 | return 65535 - prio; | |
3233 | else | |
3234 | return prio; | |
3235 | } | |
3236 | ||
ab794b6b ILT |
3237 | // Return true if this an input file whose base name matches |
3238 | // FILE_NAME. The base name must have an extension of ".o", and | |
3239 | // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o". | |
3240 | // This is to match crtbegin.o as well as crtbeginS.o without | |
3241 | // getting confused by other possibilities. Overall matching the | |
3242 | // file name this way is a dreadful hack, but the GNU linker does it | |
3243 | // in order to better support gcc, and we need to be compatible. | |
2fd32231 | 3244 | bool |
5393d741 ILT |
3245 | match_file_name(const char* file_name) const |
3246 | { return Layout::match_file_name(this->input_section_.relobj(), file_name); } | |
2fd32231 | 3247 | |
8fe2a369 ST |
3248 | // Returns 1 if THIS should appear before S in section order, -1 if S |
3249 | // appears before THIS and 0 if they are not comparable. | |
6e9ba2ca ST |
3250 | int |
3251 | compare_section_ordering(const Input_section_sort_entry& s) const | |
3252 | { | |
8fe2a369 ST |
3253 | unsigned int this_secn_index = this->input_section_.section_order_index(); |
3254 | unsigned int s_secn_index = s.input_section().section_order_index(); | |
3255 | if (this_secn_index > 0 && s_secn_index > 0) | |
3256 | { | |
3257 | if (this_secn_index < s_secn_index) | |
3258 | return 1; | |
3259 | else if (this_secn_index > s_secn_index) | |
3260 | return -1; | |
3261 | } | |
3262 | return 0; | |
6e9ba2ca ST |
3263 | } |
3264 | ||
2fd32231 ILT |
3265 | private: |
3266 | // The Input_section we are sorting. | |
3267 | Input_section input_section_; | |
3268 | // The index of this Input_section in the original list. | |
3269 | unsigned int index_; | |
3270 | // Whether this Input_section has a section name--it won't if this | |
3271 | // is some random Output_section_data. | |
3272 | bool section_has_name_; | |
3273 | // The section name if there is one. | |
3274 | std::string section_name_; | |
3275 | }; | |
3276 | ||
3277 | // Return true if S1 should come before S2 in the output section. | |
3278 | ||
3279 | bool | |
3280 | Output_section::Input_section_sort_compare::operator()( | |
3281 | const Output_section::Input_section_sort_entry& s1, | |
3282 | const Output_section::Input_section_sort_entry& s2) const | |
3283 | { | |
ab794b6b ILT |
3284 | // crtbegin.o must come first. |
3285 | bool s1_begin = s1.match_file_name("crtbegin"); | |
3286 | bool s2_begin = s2.match_file_name("crtbegin"); | |
2fd32231 ILT |
3287 | if (s1_begin || s2_begin) |
3288 | { | |
3289 | if (!s1_begin) | |
3290 | return false; | |
3291 | if (!s2_begin) | |
3292 | return true; | |
3293 | return s1.index() < s2.index(); | |
3294 | } | |
3295 | ||
ab794b6b ILT |
3296 | // crtend.o must come last. |
3297 | bool s1_end = s1.match_file_name("crtend"); | |
3298 | bool s2_end = s2.match_file_name("crtend"); | |
2fd32231 ILT |
3299 | if (s1_end || s2_end) |
3300 | { | |
3301 | if (!s1_end) | |
3302 | return true; | |
3303 | if (!s2_end) | |
3304 | return false; | |
3305 | return s1.index() < s2.index(); | |
3306 | } | |
3307 | ||
ab794b6b ILT |
3308 | // We sort all the sections with no names to the end. |
3309 | if (!s1.section_has_name() || !s2.section_has_name()) | |
3310 | { | |
3311 | if (s1.section_has_name()) | |
3312 | return true; | |
3313 | if (s2.section_has_name()) | |
3314 | return false; | |
3315 | return s1.index() < s2.index(); | |
3316 | } | |
2fd32231 | 3317 | |
ab794b6b | 3318 | // A section with a priority follows a section without a priority. |
ab794b6b ILT |
3319 | bool s1_has_priority = s1.has_priority(); |
3320 | bool s2_has_priority = s2.has_priority(); | |
3321 | if (s1_has_priority && !s2_has_priority) | |
2fd32231 | 3322 | return false; |
ab794b6b | 3323 | if (!s1_has_priority && s2_has_priority) |
2fd32231 ILT |
3324 | return true; |
3325 | ||
6e9ba2ca ST |
3326 | // Check if a section order exists for these sections through a section |
3327 | // ordering file. If sequence_num is 0, an order does not exist. | |
3328 | int sequence_num = s1.compare_section_ordering(s2); | |
3329 | if (sequence_num != 0) | |
3330 | return sequence_num == 1; | |
3331 | ||
2fd32231 ILT |
3332 | // Otherwise we sort by name. |
3333 | int compare = s1.section_name().compare(s2.section_name()); | |
3334 | if (compare != 0) | |
3335 | return compare < 0; | |
3336 | ||
3337 | // Otherwise we keep the input order. | |
3338 | return s1.index() < s2.index(); | |
3339 | } | |
3340 | ||
2a0ff005 DK |
3341 | // Return true if S1 should come before S2 in an .init_array or .fini_array |
3342 | // output section. | |
3343 | ||
3344 | bool | |
3345 | Output_section::Input_section_sort_init_fini_compare::operator()( | |
3346 | const Output_section::Input_section_sort_entry& s1, | |
3347 | const Output_section::Input_section_sort_entry& s2) const | |
3348 | { | |
3349 | // We sort all the sections with no names to the end. | |
3350 | if (!s1.section_has_name() || !s2.section_has_name()) | |
3351 | { | |
3352 | if (s1.section_has_name()) | |
3353 | return true; | |
3354 | if (s2.section_has_name()) | |
3355 | return false; | |
3356 | return s1.index() < s2.index(); | |
3357 | } | |
3358 | ||
3359 | // A section without a priority follows a section with a priority. | |
3360 | // This is the reverse of .ctors and .dtors sections. | |
3361 | bool s1_has_priority = s1.has_priority(); | |
3362 | bool s2_has_priority = s2.has_priority(); | |
3363 | if (s1_has_priority && !s2_has_priority) | |
3364 | return true; | |
3365 | if (!s1_has_priority && s2_has_priority) | |
3366 | return false; | |
3367 | ||
5393d741 ILT |
3368 | // .ctors and .dtors sections without priority come after |
3369 | // .init_array and .fini_array sections without priority. | |
3370 | if (!s1_has_priority | |
3371 | && (s1.section_name() == ".ctors" || s1.section_name() == ".dtors") | |
3372 | && s1.section_name() != s2.section_name()) | |
3373 | return false; | |
3374 | if (!s2_has_priority | |
3375 | && (s2.section_name() == ".ctors" || s2.section_name() == ".dtors") | |
3376 | && s2.section_name() != s1.section_name()) | |
3377 | return true; | |
3378 | ||
3379 | // Sort by priority if we can. | |
3380 | if (s1_has_priority) | |
3381 | { | |
3382 | unsigned int s1_prio = s1.get_priority(); | |
3383 | unsigned int s2_prio = s2.get_priority(); | |
3384 | if (s1_prio < s2_prio) | |
3385 | return true; | |
3386 | else if (s1_prio > s2_prio) | |
3387 | return false; | |
3388 | } | |
3389 | ||
6e9ba2ca ST |
3390 | // Check if a section order exists for these sections through a section |
3391 | // ordering file. If sequence_num is 0, an order does not exist. | |
3392 | int sequence_num = s1.compare_section_ordering(s2); | |
3393 | if (sequence_num != 0) | |
3394 | return sequence_num == 1; | |
3395 | ||
2a0ff005 DK |
3396 | // Otherwise we sort by name. |
3397 | int compare = s1.section_name().compare(s2.section_name()); | |
3398 | if (compare != 0) | |
3399 | return compare < 0; | |
3400 | ||
3401 | // Otherwise we keep the input order. | |
3402 | return s1.index() < s2.index(); | |
3403 | } | |
3404 | ||
8fe2a369 ST |
3405 | // Return true if S1 should come before S2. Sections that do not match |
3406 | // any pattern in the section ordering file are placed ahead of the sections | |
3407 | // that match some pattern. | |
3408 | ||
6e9ba2ca ST |
3409 | bool |
3410 | Output_section::Input_section_sort_section_order_index_compare::operator()( | |
3411 | const Output_section::Input_section_sort_entry& s1, | |
3412 | const Output_section::Input_section_sort_entry& s2) const | |
3413 | { | |
8fe2a369 ST |
3414 | unsigned int s1_secn_index = s1.input_section().section_order_index(); |
3415 | unsigned int s2_secn_index = s2.input_section().section_order_index(); | |
6e9ba2ca | 3416 | |
8fe2a369 ST |
3417 | // Keep input order if section ordering cannot determine order. |
3418 | if (s1_secn_index == s2_secn_index) | |
3419 | return s1.index() < s2.index(); | |
3420 | ||
3421 | return s1_secn_index < s2_secn_index; | |
6e9ba2ca ST |
3422 | } |
3423 | ||
e9552f7e ST |
3424 | // This updates the section order index of input sections according to the |
3425 | // the order specified in the mapping from Section id to order index. | |
3426 | ||
3427 | void | |
3428 | Output_section::update_section_layout( | |
f0558624 | 3429 | const Section_layout_order* order_map) |
e9552f7e ST |
3430 | { |
3431 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
3432 | p != this->input_sections_.end(); | |
3433 | ++p) | |
3434 | { | |
3435 | if (p->is_input_section() | |
3436 | || p->is_relaxed_input_section()) | |
3437 | { | |
3438 | Object* obj = (p->is_input_section() | |
3439 | ? p->relobj() | |
3440 | : p->relaxed_input_section()->relobj()); | |
3441 | unsigned int shndx = p->shndx(); | |
3442 | Section_layout_order::const_iterator it | |
f0558624 ST |
3443 | = order_map->find(Section_id(obj, shndx)); |
3444 | if (it == order_map->end()) | |
e9552f7e ST |
3445 | continue; |
3446 | unsigned int section_order_index = it->second; | |
3447 | if (section_order_index != 0) | |
3448 | { | |
3449 | p->set_section_order_index(section_order_index); | |
3450 | this->set_input_section_order_specified(); | |
3451 | } | |
3452 | } | |
3453 | } | |
3454 | } | |
3455 | ||
2fd32231 ILT |
3456 | // Sort the input sections attached to an output section. |
3457 | ||
3458 | void | |
3459 | Output_section::sort_attached_input_sections() | |
3460 | { | |
3461 | if (this->attached_input_sections_are_sorted_) | |
3462 | return; | |
3463 | ||
20e6d0d6 DK |
3464 | if (this->checkpoint_ != NULL |
3465 | && !this->checkpoint_->input_sections_saved()) | |
3466 | this->checkpoint_->save_input_sections(); | |
3467 | ||
2fd32231 ILT |
3468 | // The only thing we know about an input section is the object and |
3469 | // the section index. We need the section name. Recomputing this | |
3470 | // is slow but this is an unusual case. If this becomes a speed | |
3471 | // problem we can cache the names as required in Layout::layout. | |
3472 | ||
3473 | // We start by building a larger vector holding a copy of each | |
3474 | // Input_section, plus its current index in the list and its name. | |
3475 | std::vector<Input_section_sort_entry> sort_list; | |
3476 | ||
3477 | unsigned int i = 0; | |
3478 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
3479 | p != this->input_sections_.end(); | |
3480 | ++p, ++i) | |
6e9ba2ca ST |
3481 | sort_list.push_back(Input_section_sort_entry(*p, i, |
3482 | this->must_sort_attached_input_sections())); | |
2fd32231 ILT |
3483 | |
3484 | // Sort the input sections. | |
6e9ba2ca ST |
3485 | if (this->must_sort_attached_input_sections()) |
3486 | { | |
3487 | if (this->type() == elfcpp::SHT_PREINIT_ARRAY | |
3488 | || this->type() == elfcpp::SHT_INIT_ARRAY | |
3489 | || this->type() == elfcpp::SHT_FINI_ARRAY) | |
3490 | std::sort(sort_list.begin(), sort_list.end(), | |
3491 | Input_section_sort_init_fini_compare()); | |
3492 | else | |
3493 | std::sort(sort_list.begin(), sort_list.end(), | |
3494 | Input_section_sort_compare()); | |
3495 | } | |
2a0ff005 | 3496 | else |
6e9ba2ca | 3497 | { |
e9552f7e | 3498 | gold_assert(this->input_section_order_specified()); |
6e9ba2ca ST |
3499 | std::sort(sort_list.begin(), sort_list.end(), |
3500 | Input_section_sort_section_order_index_compare()); | |
3501 | } | |
2fd32231 ILT |
3502 | |
3503 | // Copy the sorted input sections back to our list. | |
3504 | this->input_sections_.clear(); | |
3505 | for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin(); | |
3506 | p != sort_list.end(); | |
3507 | ++p) | |
3508 | this->input_sections_.push_back(p->input_section()); | |
6e9ba2ca | 3509 | sort_list.clear(); |
2fd32231 ILT |
3510 | |
3511 | // Remember that we sorted the input sections, since we might get | |
3512 | // called again. | |
3513 | this->attached_input_sections_are_sorted_ = true; | |
3514 | } | |
3515 | ||
61ba1cf9 ILT |
3516 | // Write the section header to *OSHDR. |
3517 | ||
3518 | template<int size, bool big_endian> | |
3519 | void | |
16649710 ILT |
3520 | Output_section::write_header(const Layout* layout, |
3521 | const Stringpool* secnamepool, | |
61ba1cf9 ILT |
3522 | elfcpp::Shdr_write<size, big_endian>* oshdr) const |
3523 | { | |
3524 | oshdr->put_sh_name(secnamepool->get_offset(this->name_)); | |
3525 | oshdr->put_sh_type(this->type_); | |
6a74a719 | 3526 | |
2ea97941 | 3527 | elfcpp::Elf_Xword flags = this->flags_; |
755ab8af | 3528 | if (this->info_section_ != NULL && this->info_uses_section_index_) |
2ea97941 ILT |
3529 | flags |= elfcpp::SHF_INFO_LINK; |
3530 | oshdr->put_sh_flags(flags); | |
6a74a719 | 3531 | |
61ba1cf9 ILT |
3532 | oshdr->put_sh_addr(this->address()); |
3533 | oshdr->put_sh_offset(this->offset()); | |
3534 | oshdr->put_sh_size(this->data_size()); | |
16649710 ILT |
3535 | if (this->link_section_ != NULL) |
3536 | oshdr->put_sh_link(this->link_section_->out_shndx()); | |
3537 | else if (this->should_link_to_symtab_) | |
886f533a | 3538 | oshdr->put_sh_link(layout->symtab_section_shndx()); |
16649710 ILT |
3539 | else if (this->should_link_to_dynsym_) |
3540 | oshdr->put_sh_link(layout->dynsym_section()->out_shndx()); | |
3541 | else | |
3542 | oshdr->put_sh_link(this->link_); | |
755ab8af | 3543 | |
2ea97941 | 3544 | elfcpp::Elf_Word info; |
16649710 | 3545 | if (this->info_section_ != NULL) |
755ab8af ILT |
3546 | { |
3547 | if (this->info_uses_section_index_) | |
2ea97941 | 3548 | info = this->info_section_->out_shndx(); |
755ab8af | 3549 | else |
2ea97941 | 3550 | info = this->info_section_->symtab_index(); |
755ab8af | 3551 | } |
6a74a719 | 3552 | else if (this->info_symndx_ != NULL) |
2ea97941 | 3553 | info = this->info_symndx_->symtab_index(); |
16649710 | 3554 | else |
2ea97941 ILT |
3555 | info = this->info_; |
3556 | oshdr->put_sh_info(info); | |
755ab8af | 3557 | |
61ba1cf9 ILT |
3558 | oshdr->put_sh_addralign(this->addralign_); |
3559 | oshdr->put_sh_entsize(this->entsize_); | |
a2fb1b05 ILT |
3560 | } |
3561 | ||
ead1e424 ILT |
3562 | // Write out the data. For input sections the data is written out by |
3563 | // Object::relocate, but we have to handle Output_section_data objects | |
3564 | // here. | |
3565 | ||
3566 | void | |
3567 | Output_section::do_write(Output_file* of) | |
3568 | { | |
96803768 ILT |
3569 | gold_assert(!this->requires_postprocessing()); |
3570 | ||
c0a62865 DK |
3571 | // If the target performs relaxation, we delay filler generation until now. |
3572 | gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty()); | |
3573 | ||
c51e6221 ILT |
3574 | off_t output_section_file_offset = this->offset(); |
3575 | for (Fill_list::iterator p = this->fills_.begin(); | |
3576 | p != this->fills_.end(); | |
3577 | ++p) | |
3578 | { | |
8851ecca | 3579 | std::string fill_data(parameters->target().code_fill(p->length())); |
c51e6221 | 3580 | of->write(output_section_file_offset + p->section_offset(), |
a445fddf | 3581 | fill_data.data(), fill_data.size()); |
c51e6221 ILT |
3582 | } |
3583 | ||
c0a62865 | 3584 | off_t off = this->offset() + this->first_input_offset_; |
ead1e424 ILT |
3585 | for (Input_section_list::iterator p = this->input_sections_.begin(); |
3586 | p != this->input_sections_.end(); | |
3587 | ++p) | |
c0a62865 DK |
3588 | { |
3589 | off_t aligned_off = align_address(off, p->addralign()); | |
3590 | if (this->generate_code_fills_at_write_ && (off != aligned_off)) | |
3591 | { | |
3592 | size_t fill_len = aligned_off - off; | |
3593 | std::string fill_data(parameters->target().code_fill(fill_len)); | |
3594 | of->write(off, fill_data.data(), fill_data.size()); | |
3595 | } | |
3596 | ||
3597 | p->write(of); | |
3598 | off = aligned_off + p->data_size(); | |
3599 | } | |
8ea8cd50 CC |
3600 | |
3601 | // For incremental links, fill in unused chunks in debug sections | |
3602 | // with dummy compilation unit headers. | |
3603 | if (this->free_space_fill_ != NULL) | |
3604 | { | |
3605 | for (Free_list::Const_iterator p = this->free_list_.begin(); | |
3606 | p != this->free_list_.end(); | |
3607 | ++p) | |
3608 | { | |
3609 | off_t off = p->start_; | |
3610 | size_t len = p->end_ - off; | |
3611 | this->free_space_fill_->write(of, this->offset() + off, len); | |
3612 | } | |
3613 | if (this->patch_space_ > 0) | |
3614 | { | |
3615 | off_t off = this->current_data_size_for_child() - this->patch_space_; | |
3616 | this->free_space_fill_->write(of, this->offset() + off, | |
3617 | this->patch_space_); | |
3618 | } | |
3619 | } | |
ead1e424 ILT |
3620 | } |
3621 | ||
96803768 ILT |
3622 | // If a section requires postprocessing, create the buffer to use. |
3623 | ||
3624 | void | |
3625 | Output_section::create_postprocessing_buffer() | |
3626 | { | |
3627 | gold_assert(this->requires_postprocessing()); | |
1bedcac5 ILT |
3628 | |
3629 | if (this->postprocessing_buffer_ != NULL) | |
3630 | return; | |
96803768 ILT |
3631 | |
3632 | if (!this->input_sections_.empty()) | |
3633 | { | |
3634 | off_t off = this->first_input_offset_; | |
3635 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
3636 | p != this->input_sections_.end(); | |
3637 | ++p) | |
3638 | { | |
3639 | off = align_address(off, p->addralign()); | |
3640 | p->finalize_data_size(); | |
3641 | off += p->data_size(); | |
3642 | } | |
3643 | this->set_current_data_size_for_child(off); | |
3644 | } | |
3645 | ||
3646 | off_t buffer_size = this->current_data_size_for_child(); | |
3647 | this->postprocessing_buffer_ = new unsigned char[buffer_size]; | |
3648 | } | |
3649 | ||
3650 | // Write all the data of an Output_section into the postprocessing | |
3651 | // buffer. This is used for sections which require postprocessing, | |
3652 | // such as compression. Input sections are handled by | |
3653 | // Object::Relocate. | |
3654 | ||
3655 | void | |
3656 | Output_section::write_to_postprocessing_buffer() | |
3657 | { | |
3658 | gold_assert(this->requires_postprocessing()); | |
3659 | ||
c0a62865 DK |
3660 | // If the target performs relaxation, we delay filler generation until now. |
3661 | gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty()); | |
3662 | ||
96803768 ILT |
3663 | unsigned char* buffer = this->postprocessing_buffer(); |
3664 | for (Fill_list::iterator p = this->fills_.begin(); | |
3665 | p != this->fills_.end(); | |
3666 | ++p) | |
3667 | { | |
8851ecca | 3668 | std::string fill_data(parameters->target().code_fill(p->length())); |
a445fddf ILT |
3669 | memcpy(buffer + p->section_offset(), fill_data.data(), |
3670 | fill_data.size()); | |
96803768 ILT |
3671 | } |
3672 | ||
3673 | off_t off = this->first_input_offset_; | |
3674 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
3675 | p != this->input_sections_.end(); | |
3676 | ++p) | |
3677 | { | |
c0a62865 DK |
3678 | off_t aligned_off = align_address(off, p->addralign()); |
3679 | if (this->generate_code_fills_at_write_ && (off != aligned_off)) | |
3680 | { | |
3681 | size_t fill_len = aligned_off - off; | |
3682 | std::string fill_data(parameters->target().code_fill(fill_len)); | |
3683 | memcpy(buffer + off, fill_data.data(), fill_data.size()); | |
3684 | } | |
3685 | ||
3686 | p->write_to_buffer(buffer + aligned_off); | |
3687 | off = aligned_off + p->data_size(); | |
96803768 ILT |
3688 | } |
3689 | } | |
3690 | ||
a445fddf ILT |
3691 | // Get the input sections for linker script processing. We leave |
3692 | // behind the Output_section_data entries. Note that this may be | |
3693 | // slightly incorrect for merge sections. We will leave them behind, | |
3694 | // but it is possible that the script says that they should follow | |
3695 | // some other input sections, as in: | |
3696 | // .rodata { *(.rodata) *(.rodata.cst*) } | |
3697 | // For that matter, we don't handle this correctly: | |
3698 | // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) } | |
3699 | // With luck this will never matter. | |
3700 | ||
3701 | uint64_t | |
3702 | Output_section::get_input_sections( | |
2ea97941 | 3703 | uint64_t address, |
a445fddf | 3704 | const std::string& fill, |
6625d24e | 3705 | std::list<Input_section>* input_sections) |
a445fddf | 3706 | { |
20e6d0d6 DK |
3707 | if (this->checkpoint_ != NULL |
3708 | && !this->checkpoint_->input_sections_saved()) | |
3709 | this->checkpoint_->save_input_sections(); | |
3710 | ||
0439c796 DK |
3711 | // Invalidate fast look-up maps. |
3712 | this->lookup_maps_->invalidate(); | |
c0a62865 | 3713 | |
2ea97941 | 3714 | uint64_t orig_address = address; |
a445fddf | 3715 | |
2ea97941 | 3716 | address = align_address(address, this->addralign()); |
a445fddf ILT |
3717 | |
3718 | Input_section_list remaining; | |
3719 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
3720 | p != this->input_sections_.end(); | |
3721 | ++p) | |
3722 | { | |
0439c796 DK |
3723 | if (p->is_input_section() |
3724 | || p->is_relaxed_input_section() | |
3725 | || p->is_merge_section()) | |
6625d24e | 3726 | input_sections->push_back(*p); |
a445fddf ILT |
3727 | else |
3728 | { | |
2ea97941 ILT |
3729 | uint64_t aligned_address = align_address(address, p->addralign()); |
3730 | if (aligned_address != address && !fill.empty()) | |
a445fddf ILT |
3731 | { |
3732 | section_size_type length = | |
2ea97941 | 3733 | convert_to_section_size_type(aligned_address - address); |
a445fddf ILT |
3734 | std::string this_fill; |
3735 | this_fill.reserve(length); | |
3736 | while (this_fill.length() + fill.length() <= length) | |
3737 | this_fill += fill; | |
3738 | if (this_fill.length() < length) | |
3739 | this_fill.append(fill, 0, length - this_fill.length()); | |
3740 | ||
3741 | Output_section_data* posd = new Output_data_const(this_fill, 0); | |
3742 | remaining.push_back(Input_section(posd)); | |
3743 | } | |
2ea97941 | 3744 | address = aligned_address; |
a445fddf ILT |
3745 | |
3746 | remaining.push_back(*p); | |
3747 | ||
3748 | p->finalize_data_size(); | |
2ea97941 | 3749 | address += p->data_size(); |
a445fddf ILT |
3750 | } |
3751 | } | |
3752 | ||
3753 | this->input_sections_.swap(remaining); | |
3754 | this->first_input_offset_ = 0; | |
3755 | ||
2ea97941 ILT |
3756 | uint64_t data_size = address - orig_address; |
3757 | this->set_current_data_size_for_child(data_size); | |
3758 | return data_size; | |
a445fddf ILT |
3759 | } |
3760 | ||
6625d24e DK |
3761 | // Add a script input section. SIS is an Output_section::Input_section, |
3762 | // which can be either a plain input section or a special input section like | |
3763 | // a relaxed input section. For a special input section, its size must be | |
3764 | // finalized. | |
a445fddf ILT |
3765 | |
3766 | void | |
6625d24e | 3767 | Output_section::add_script_input_section(const Input_section& sis) |
a445fddf | 3768 | { |
6625d24e DK |
3769 | uint64_t data_size = sis.data_size(); |
3770 | uint64_t addralign = sis.addralign(); | |
2ea97941 ILT |
3771 | if (addralign > this->addralign_) |
3772 | this->addralign_ = addralign; | |
a445fddf ILT |
3773 | |
3774 | off_t offset_in_section = this->current_data_size_for_child(); | |
3775 | off_t aligned_offset_in_section = align_address(offset_in_section, | |
2ea97941 | 3776 | addralign); |
a445fddf ILT |
3777 | |
3778 | this->set_current_data_size_for_child(aligned_offset_in_section | |
2ea97941 | 3779 | + data_size); |
a445fddf | 3780 | |
6625d24e | 3781 | this->input_sections_.push_back(sis); |
0439c796 DK |
3782 | |
3783 | // Update fast lookup maps if necessary. | |
3784 | if (this->lookup_maps_->is_valid()) | |
3785 | { | |
3786 | if (sis.is_merge_section()) | |
3787 | { | |
3788 | Output_merge_base* pomb = sis.output_merge_base(); | |
3789 | Merge_section_properties msp(pomb->is_string(), pomb->entsize(), | |
3790 | pomb->addralign()); | |
3791 | this->lookup_maps_->add_merge_section(msp, pomb); | |
3792 | for (Output_merge_base::Input_sections::const_iterator p = | |
3793 | pomb->input_sections_begin(); | |
3794 | p != pomb->input_sections_end(); | |
3795 | ++p) | |
3796 | this->lookup_maps_->add_merge_input_section(p->first, p->second, | |
3797 | pomb); | |
3798 | } | |
3799 | else if (sis.is_relaxed_input_section()) | |
3800 | { | |
3801 | Output_relaxed_input_section* poris = sis.relaxed_input_section(); | |
3802 | this->lookup_maps_->add_relaxed_input_section(poris->relobj(), | |
3803 | poris->shndx(), poris); | |
3804 | } | |
3805 | } | |
20e6d0d6 DK |
3806 | } |
3807 | ||
8923b24c | 3808 | // Save states for relaxation. |
20e6d0d6 DK |
3809 | |
3810 | void | |
3811 | Output_section::save_states() | |
3812 | { | |
3813 | gold_assert(this->checkpoint_ == NULL); | |
3814 | Checkpoint_output_section* checkpoint = | |
3815 | new Checkpoint_output_section(this->addralign_, this->flags_, | |
3816 | this->input_sections_, | |
3817 | this->first_input_offset_, | |
3818 | this->attached_input_sections_are_sorted_); | |
3819 | this->checkpoint_ = checkpoint; | |
3820 | gold_assert(this->fills_.empty()); | |
3821 | } | |
3822 | ||
8923b24c DK |
3823 | void |
3824 | Output_section::discard_states() | |
3825 | { | |
3826 | gold_assert(this->checkpoint_ != NULL); | |
3827 | delete this->checkpoint_; | |
3828 | this->checkpoint_ = NULL; | |
3829 | gold_assert(this->fills_.empty()); | |
3830 | ||
0439c796 DK |
3831 | // Simply invalidate the fast lookup maps since we do not keep |
3832 | // track of them. | |
3833 | this->lookup_maps_->invalidate(); | |
8923b24c DK |
3834 | } |
3835 | ||
20e6d0d6 DK |
3836 | void |
3837 | Output_section::restore_states() | |
3838 | { | |
3839 | gold_assert(this->checkpoint_ != NULL); | |
3840 | Checkpoint_output_section* checkpoint = this->checkpoint_; | |
3841 | ||
3842 | this->addralign_ = checkpoint->addralign(); | |
3843 | this->flags_ = checkpoint->flags(); | |
3844 | this->first_input_offset_ = checkpoint->first_input_offset(); | |
3845 | ||
3846 | if (!checkpoint->input_sections_saved()) | |
3847 | { | |
3848 | // If we have not copied the input sections, just resize it. | |
3849 | size_t old_size = checkpoint->input_sections_size(); | |
3850 | gold_assert(this->input_sections_.size() >= old_size); | |
3851 | this->input_sections_.resize(old_size); | |
3852 | } | |
3853 | else | |
3854 | { | |
3855 | // We need to copy the whole list. This is not efficient for | |
3856 | // extremely large output with hundreads of thousands of input | |
3857 | // objects. We may need to re-think how we should pass sections | |
3858 | // to scripts. | |
c0a62865 | 3859 | this->input_sections_ = *checkpoint->input_sections(); |
20e6d0d6 DK |
3860 | } |
3861 | ||
3862 | this->attached_input_sections_are_sorted_ = | |
3863 | checkpoint->attached_input_sections_are_sorted(); | |
c0a62865 | 3864 | |
0439c796 DK |
3865 | // Simply invalidate the fast lookup maps since we do not keep |
3866 | // track of them. | |
3867 | this->lookup_maps_->invalidate(); | |
a445fddf ILT |
3868 | } |
3869 | ||
8923b24c DK |
3870 | // Update the section offsets of input sections in this. This is required if |
3871 | // relaxation causes some input sections to change sizes. | |
3872 | ||
3873 | void | |
3874 | Output_section::adjust_section_offsets() | |
3875 | { | |
3876 | if (!this->section_offsets_need_adjustment_) | |
3877 | return; | |
3878 | ||
3879 | off_t off = 0; | |
3880 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
3881 | p != this->input_sections_.end(); | |
3882 | ++p) | |
3883 | { | |
3884 | off = align_address(off, p->addralign()); | |
3885 | if (p->is_input_section()) | |
3886 | p->relobj()->set_section_offset(p->shndx(), off); | |
3887 | off += p->data_size(); | |
3888 | } | |
3889 | ||
3890 | this->section_offsets_need_adjustment_ = false; | |
3891 | } | |
3892 | ||
7d9e3d98 ILT |
3893 | // Print to the map file. |
3894 | ||
3895 | void | |
3896 | Output_section::do_print_to_mapfile(Mapfile* mapfile) const | |
3897 | { | |
3898 | mapfile->print_output_section(this); | |
3899 | ||
3900 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); | |
3901 | p != this->input_sections_.end(); | |
3902 | ++p) | |
3903 | p->print_to_mapfile(mapfile); | |
3904 | } | |
3905 | ||
38c5e8b4 ILT |
3906 | // Print stats for merge sections to stderr. |
3907 | ||
3908 | void | |
3909 | Output_section::print_merge_stats() | |
3910 | { | |
3911 | Input_section_list::iterator p; | |
3912 | for (p = this->input_sections_.begin(); | |
3913 | p != this->input_sections_.end(); | |
3914 | ++p) | |
3915 | p->print_merge_stats(this->name_); | |
3916 | } | |
3917 | ||
cdc29364 CC |
3918 | // Set a fixed layout for the section. Used for incremental update links. |
3919 | ||
3920 | void | |
3921 | Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset, | |
3922 | off_t sh_size, uint64_t sh_addralign) | |
3923 | { | |
3924 | this->addralign_ = sh_addralign; | |
3925 | this->set_current_data_size(sh_size); | |
3926 | if ((this->flags_ & elfcpp::SHF_ALLOC) != 0) | |
3927 | this->set_address(sh_addr); | |
3928 | this->set_file_offset(sh_offset); | |
3929 | this->finalize_data_size(); | |
3930 | this->free_list_.init(sh_size, false); | |
3931 | this->has_fixed_layout_ = true; | |
3932 | } | |
3933 | ||
3934 | // Reserve space within the fixed layout for the section. Used for | |
3935 | // incremental update links. | |
5146f448 | 3936 | |
cdc29364 CC |
3937 | void |
3938 | Output_section::reserve(uint64_t sh_offset, uint64_t sh_size) | |
3939 | { | |
3940 | this->free_list_.remove(sh_offset, sh_offset + sh_size); | |
3941 | } | |
3942 | ||
5146f448 CC |
3943 | // Allocate space from the free list for the section. Used for |
3944 | // incremental update links. | |
3945 | ||
3946 | off_t | |
3947 | Output_section::allocate(off_t len, uint64_t addralign) | |
3948 | { | |
3949 | return this->free_list_.allocate(len, addralign, 0); | |
3950 | } | |
3951 | ||
a2fb1b05 ILT |
3952 | // Output segment methods. |
3953 | ||
2ea97941 | 3954 | Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags) |
22f0da72 | 3955 | : vaddr_(0), |
a2fb1b05 ILT |
3956 | paddr_(0), |
3957 | memsz_(0), | |
a445fddf ILT |
3958 | max_align_(0), |
3959 | min_p_align_(0), | |
a2fb1b05 ILT |
3960 | offset_(0), |
3961 | filesz_(0), | |
2ea97941 ILT |
3962 | type_(type), |
3963 | flags_(flags), | |
a445fddf | 3964 | is_max_align_known_(false), |
8a5e3e08 ILT |
3965 | are_addresses_set_(false), |
3966 | is_large_data_segment_(false) | |
a2fb1b05 | 3967 | { |
bb321bb1 ILT |
3968 | // The ELF ABI specifies that a PT_TLS segment always has PF_R as |
3969 | // the flags. | |
3970 | if (type == elfcpp::PT_TLS) | |
3971 | this->flags_ = elfcpp::PF_R; | |
a2fb1b05 ILT |
3972 | } |
3973 | ||
22f0da72 | 3974 | // Add an Output_section to a PT_LOAD Output_segment. |
a2fb1b05 ILT |
3975 | |
3976 | void | |
22f0da72 ILT |
3977 | Output_segment::add_output_section_to_load(Layout* layout, |
3978 | Output_section* os, | |
3979 | elfcpp::Elf_Word seg_flags) | |
a2fb1b05 | 3980 | { |
22f0da72 | 3981 | gold_assert(this->type() == elfcpp::PT_LOAD); |
a3ad94ed | 3982 | gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0); |
a445fddf | 3983 | gold_assert(!this->is_max_align_known_); |
8a5e3e08 | 3984 | gold_assert(os->is_large_data_section() == this->is_large_data_segment()); |
75f65a3e | 3985 | |
a192ba05 | 3986 | this->update_flags_for_output_section(seg_flags); |
75f65a3e | 3987 | |
22f0da72 ILT |
3988 | // We don't want to change the ordering if we have a linker script |
3989 | // with a SECTIONS clause. | |
3990 | Output_section_order order = os->order(); | |
3991 | if (layout->script_options()->saw_sections_clause()) | |
3992 | order = static_cast<Output_section_order>(0); | |
75f65a3e | 3993 | else |
22f0da72 | 3994 | gold_assert(order != ORDER_INVALID); |
54dc6425 | 3995 | |
22f0da72 ILT |
3996 | this->output_lists_[order].push_back(os); |
3997 | } | |
9f1d377b | 3998 | |
22f0da72 | 3999 | // Add an Output_section to a non-PT_LOAD Output_segment. |
1a2dff53 | 4000 | |
22f0da72 ILT |
4001 | void |
4002 | Output_segment::add_output_section_to_nonload(Output_section* os, | |
4003 | elfcpp::Elf_Word seg_flags) | |
4004 | { | |
4005 | gold_assert(this->type() != elfcpp::PT_LOAD); | |
4006 | gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0); | |
4007 | gold_assert(!this->is_max_align_known_); | |
1a2dff53 | 4008 | |
22f0da72 | 4009 | this->update_flags_for_output_section(seg_flags); |
9f1d377b | 4010 | |
22f0da72 ILT |
4011 | this->output_lists_[0].push_back(os); |
4012 | } | |
8a5e3e08 | 4013 | |
22f0da72 ILT |
4014 | // Remove an Output_section from this segment. It is an error if it |
4015 | // is not present. | |
8a5e3e08 | 4016 | |
22f0da72 ILT |
4017 | void |
4018 | Output_segment::remove_output_section(Output_section* os) | |
4019 | { | |
4020 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) | |
8a5e3e08 | 4021 | { |
22f0da72 ILT |
4022 | Output_data_list* pdl = &this->output_lists_[i]; |
4023 | for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p) | |
8a5e3e08 | 4024 | { |
22f0da72 | 4025 | if (*p == os) |
8a5e3e08 | 4026 | { |
22f0da72 | 4027 | pdl->erase(p); |
8a5e3e08 ILT |
4028 | return; |
4029 | } | |
4030 | } | |
f5c870d2 | 4031 | } |
1650c4ff ILT |
4032 | gold_unreachable(); |
4033 | } | |
4034 | ||
a192ba05 ILT |
4035 | // Add an Output_data (which need not be an Output_section) to the |
4036 | // start of a segment. | |
75f65a3e ILT |
4037 | |
4038 | void | |
4039 | Output_segment::add_initial_output_data(Output_data* od) | |
4040 | { | |
a445fddf | 4041 | gold_assert(!this->is_max_align_known_); |
22f0da72 ILT |
4042 | Output_data_list::iterator p = this->output_lists_[0].begin(); |
4043 | this->output_lists_[0].insert(p, od); | |
4044 | } | |
4045 | ||
4046 | // Return true if this segment has any sections which hold actual | |
4047 | // data, rather than being a BSS section. | |
4048 | ||
4049 | bool | |
4050 | Output_segment::has_any_data_sections() const | |
4051 | { | |
4052 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) | |
4053 | { | |
4054 | const Output_data_list* pdl = &this->output_lists_[i]; | |
4055 | for (Output_data_list::const_iterator p = pdl->begin(); | |
4056 | p != pdl->end(); | |
4057 | ++p) | |
4058 | { | |
4059 | if (!(*p)->is_section()) | |
4060 | return true; | |
4061 | if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS) | |
4062 | return true; | |
4063 | } | |
4064 | } | |
4065 | return false; | |
75f65a3e ILT |
4066 | } |
4067 | ||
5bc2f5be CC |
4068 | // Return whether the first data section (not counting TLS sections) |
4069 | // is a relro section. | |
9f1d377b ILT |
4070 | |
4071 | bool | |
4072 | Output_segment::is_first_section_relro() const | |
4073 | { | |
22f0da72 ILT |
4074 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) |
4075 | { | |
5bc2f5be CC |
4076 | if (i == static_cast<int>(ORDER_TLS_DATA) |
4077 | || i == static_cast<int>(ORDER_TLS_BSS)) | |
4078 | continue; | |
22f0da72 ILT |
4079 | const Output_data_list* pdl = &this->output_lists_[i]; |
4080 | if (!pdl->empty()) | |
4081 | { | |
4082 | Output_data* p = pdl->front(); | |
4083 | return p->is_section() && p->output_section()->is_relro(); | |
4084 | } | |
4085 | } | |
4086 | return false; | |
9f1d377b ILT |
4087 | } |
4088 | ||
75f65a3e | 4089 | // Return the maximum alignment of the Output_data in Output_segment. |
75f65a3e ILT |
4090 | |
4091 | uint64_t | |
a445fddf | 4092 | Output_segment::maximum_alignment() |
75f65a3e | 4093 | { |
a445fddf | 4094 | if (!this->is_max_align_known_) |
ead1e424 | 4095 | { |
22f0da72 ILT |
4096 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) |
4097 | { | |
4098 | const Output_data_list* pdl = &this->output_lists_[i]; | |
4099 | uint64_t addralign = Output_segment::maximum_alignment_list(pdl); | |
4100 | if (addralign > this->max_align_) | |
4101 | this->max_align_ = addralign; | |
4102 | } | |
a445fddf | 4103 | this->is_max_align_known_ = true; |
ead1e424 ILT |
4104 | } |
4105 | ||
a445fddf | 4106 | return this->max_align_; |
75f65a3e ILT |
4107 | } |
4108 | ||
ead1e424 ILT |
4109 | // Return the maximum alignment of a list of Output_data. |
4110 | ||
4111 | uint64_t | |
a445fddf | 4112 | Output_segment::maximum_alignment_list(const Output_data_list* pdl) |
ead1e424 ILT |
4113 | { |
4114 | uint64_t ret = 0; | |
4115 | for (Output_data_list::const_iterator p = pdl->begin(); | |
4116 | p != pdl->end(); | |
4117 | ++p) | |
4118 | { | |
2ea97941 ILT |
4119 | uint64_t addralign = (*p)->addralign(); |
4120 | if (addralign > ret) | |
4121 | ret = addralign; | |
ead1e424 ILT |
4122 | } |
4123 | return ret; | |
4124 | } | |
4125 | ||
22f0da72 | 4126 | // Return whether this segment has any dynamic relocs. |
4f4c5f80 | 4127 | |
22f0da72 ILT |
4128 | bool |
4129 | Output_segment::has_dynamic_reloc() const | |
4f4c5f80 | 4130 | { |
22f0da72 ILT |
4131 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) |
4132 | if (this->has_dynamic_reloc_list(&this->output_lists_[i])) | |
4133 | return true; | |
4134 | return false; | |
4f4c5f80 ILT |
4135 | } |
4136 | ||
22f0da72 | 4137 | // Return whether this Output_data_list has any dynamic relocs. |
4f4c5f80 | 4138 | |
22f0da72 ILT |
4139 | bool |
4140 | Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const | |
4f4c5f80 | 4141 | { |
4f4c5f80 ILT |
4142 | for (Output_data_list::const_iterator p = pdl->begin(); |
4143 | p != pdl->end(); | |
4144 | ++p) | |
22f0da72 ILT |
4145 | if ((*p)->has_dynamic_reloc()) |
4146 | return true; | |
4147 | return false; | |
4f4c5f80 ILT |
4148 | } |
4149 | ||
a445fddf ILT |
4150 | // Set the section addresses for an Output_segment. If RESET is true, |
4151 | // reset the addresses first. ADDR is the address and *POFF is the | |
4152 | // file offset. Set the section indexes starting with *PSHNDX. | |
5bc2f5be CC |
4153 | // INCREASE_RELRO is the size of the portion of the first non-relro |
4154 | // section that should be included in the PT_GNU_RELRO segment. | |
4155 | // If this segment has relro sections, and has been aligned for | |
4156 | // that purpose, set *HAS_RELRO to TRUE. Return the address of | |
4157 | // the immediately following segment. Update *HAS_RELRO, *POFF, | |
4158 | // and *PSHNDX. | |
75f65a3e ILT |
4159 | |
4160 | uint64_t | |
cdc29364 | 4161 | Output_segment::set_section_addresses(Layout* layout, bool reset, |
1a2dff53 | 4162 | uint64_t addr, |
fd064a5b | 4163 | unsigned int* increase_relro, |
fc497986 | 4164 | bool* has_relro, |
1a2dff53 | 4165 | off_t* poff, |
ead1e424 | 4166 | unsigned int* pshndx) |
75f65a3e | 4167 | { |
a3ad94ed | 4168 | gold_assert(this->type_ == elfcpp::PT_LOAD); |
75f65a3e | 4169 | |
fc497986 | 4170 | uint64_t last_relro_pad = 0; |
1a2dff53 ILT |
4171 | off_t orig_off = *poff; |
4172 | ||
5bc2f5be CC |
4173 | bool in_tls = false; |
4174 | ||
1a2dff53 ILT |
4175 | // If we have relro sections, we need to pad forward now so that the |
4176 | // relro sections plus INCREASE_RELRO end on a common page boundary. | |
4177 | if (parameters->options().relro() | |
4178 | && this->is_first_section_relro() | |
4179 | && (!this->are_addresses_set_ || reset)) | |
4180 | { | |
4181 | uint64_t relro_size = 0; | |
4182 | off_t off = *poff; | |
fc497986 | 4183 | uint64_t max_align = 0; |
5bc2f5be | 4184 | for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i) |
1a2dff53 | 4185 | { |
22f0da72 ILT |
4186 | Output_data_list* pdl = &this->output_lists_[i]; |
4187 | Output_data_list::iterator p; | |
4188 | for (p = pdl->begin(); p != pdl->end(); ++p) | |
1a2dff53 | 4189 | { |
22f0da72 ILT |
4190 | if (!(*p)->is_section()) |
4191 | break; | |
fc497986 CC |
4192 | uint64_t align = (*p)->addralign(); |
4193 | if (align > max_align) | |
4194 | max_align = align; | |
5bc2f5be CC |
4195 | if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)) |
4196 | in_tls = true; | |
4197 | else if (in_tls) | |
4198 | { | |
4199 | // Align the first non-TLS section to the alignment | |
4200 | // of the TLS segment. | |
4201 | align = max_align; | |
4202 | in_tls = false; | |
4203 | } | |
fc497986 | 4204 | relro_size = align_address(relro_size, align); |
5bc2f5be CC |
4205 | // Ignore the size of the .tbss section. |
4206 | if ((*p)->is_section_flag_set(elfcpp::SHF_TLS) | |
4207 | && (*p)->is_section_type(elfcpp::SHT_NOBITS)) | |
4208 | continue; | |
22f0da72 ILT |
4209 | if ((*p)->is_address_valid()) |
4210 | relro_size += (*p)->data_size(); | |
4211 | else | |
4212 | { | |
4213 | // FIXME: This could be faster. | |
4214 | (*p)->set_address_and_file_offset(addr + relro_size, | |
4215 | off + relro_size); | |
4216 | relro_size += (*p)->data_size(); | |
4217 | (*p)->reset_address_and_file_offset(); | |
4218 | } | |
1a2dff53 | 4219 | } |
22f0da72 ILT |
4220 | if (p != pdl->end()) |
4221 | break; | |
1a2dff53 | 4222 | } |
fd064a5b | 4223 | relro_size += *increase_relro; |
fc497986 CC |
4224 | // Pad the total relro size to a multiple of the maximum |
4225 | // section alignment seen. | |
4226 | uint64_t aligned_size = align_address(relro_size, max_align); | |
4227 | // Note the amount of padding added after the last relro section. | |
4228 | last_relro_pad = aligned_size - relro_size; | |
fc497986 | 4229 | *has_relro = true; |
1a2dff53 ILT |
4230 | |
4231 | uint64_t page_align = parameters->target().common_pagesize(); | |
4232 | ||
4233 | // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0. | |
fc497986 | 4234 | uint64_t desired_align = page_align - (aligned_size % page_align); |
1a2dff53 ILT |
4235 | if (desired_align < *poff % page_align) |
4236 | *poff += page_align - *poff % page_align; | |
4237 | *poff += desired_align - *poff % page_align; | |
4238 | addr += *poff - orig_off; | |
4239 | orig_off = *poff; | |
4240 | } | |
4241 | ||
a445fddf ILT |
4242 | if (!reset && this->are_addresses_set_) |
4243 | { | |
4244 | gold_assert(this->paddr_ == addr); | |
4245 | addr = this->vaddr_; | |
4246 | } | |
4247 | else | |
4248 | { | |
4249 | this->vaddr_ = addr; | |
4250 | this->paddr_ = addr; | |
4251 | this->are_addresses_set_ = true; | |
4252 | } | |
75f65a3e | 4253 | |
5bc2f5be | 4254 | in_tls = false; |
96a2b4e4 | 4255 | |
75f65a3e ILT |
4256 | this->offset_ = orig_off; |
4257 | ||
22f0da72 ILT |
4258 | off_t off = 0; |
4259 | uint64_t ret; | |
4260 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) | |
4261 | { | |
fc497986 CC |
4262 | if (i == static_cast<int>(ORDER_RELRO_LAST)) |
4263 | { | |
4264 | *poff += last_relro_pad; | |
4265 | addr += last_relro_pad; | |
fd064a5b CC |
4266 | if (this->output_lists_[i].empty()) |
4267 | { | |
4268 | // If there is nothing in the ORDER_RELRO_LAST list, | |
4269 | // the padding will occur at the end of the relro | |
4270 | // segment, and we need to add it to *INCREASE_RELRO. | |
4271 | *increase_relro += last_relro_pad; | |
4272 | } | |
fc497986 | 4273 | } |
5bc2f5be CC |
4274 | addr = this->set_section_list_addresses(layout, reset, |
4275 | &this->output_lists_[i], | |
4276 | addr, poff, pshndx, &in_tls); | |
22f0da72 ILT |
4277 | if (i < static_cast<int>(ORDER_SMALL_BSS)) |
4278 | { | |
4279 | this->filesz_ = *poff - orig_off; | |
4280 | off = *poff; | |
4281 | } | |
75f65a3e | 4282 | |
22f0da72 ILT |
4283 | ret = addr; |
4284 | } | |
96a2b4e4 ILT |
4285 | |
4286 | // If the last section was a TLS section, align upward to the | |
4287 | // alignment of the TLS segment, so that the overall size of the TLS | |
4288 | // segment is aligned. | |
4289 | if (in_tls) | |
4290 | { | |
4291 | uint64_t segment_align = layout->tls_segment()->maximum_alignment(); | |
4292 | *poff = align_address(*poff, segment_align); | |
4293 | } | |
4294 | ||
75f65a3e ILT |
4295 | this->memsz_ = *poff - orig_off; |
4296 | ||
4297 | // Ignore the file offset adjustments made by the BSS Output_data | |
4298 | // objects. | |
4299 | *poff = off; | |
61ba1cf9 ILT |
4300 | |
4301 | return ret; | |
75f65a3e ILT |
4302 | } |
4303 | ||
b8e6aad9 ILT |
4304 | // Set the addresses and file offsets in a list of Output_data |
4305 | // structures. | |
75f65a3e ILT |
4306 | |
4307 | uint64_t | |
cdc29364 | 4308 | Output_segment::set_section_list_addresses(Layout* layout, bool reset, |
96a2b4e4 | 4309 | Output_data_list* pdl, |
ead1e424 | 4310 | uint64_t addr, off_t* poff, |
96a2b4e4 | 4311 | unsigned int* pshndx, |
1a2dff53 | 4312 | bool* in_tls) |
75f65a3e | 4313 | { |
ead1e424 | 4314 | off_t startoff = *poff; |
cdc29364 CC |
4315 | // For incremental updates, we may allocate non-fixed sections from |
4316 | // free space in the file. This keeps track of the high-water mark. | |
4317 | off_t maxoff = startoff; | |
75f65a3e | 4318 | |
ead1e424 | 4319 | off_t off = startoff; |
75f65a3e ILT |
4320 | for (Output_data_list::iterator p = pdl->begin(); |
4321 | p != pdl->end(); | |
4322 | ++p) | |
4323 | { | |
a445fddf ILT |
4324 | if (reset) |
4325 | (*p)->reset_address_and_file_offset(); | |
4326 | ||
cdc29364 CC |
4327 | // When doing an incremental update or when using a linker script, |
4328 | // the section will most likely already have an address. | |
a445fddf | 4329 | if (!(*p)->is_address_valid()) |
3802b2dd | 4330 | { |
96a2b4e4 ILT |
4331 | uint64_t align = (*p)->addralign(); |
4332 | ||
4333 | if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)) | |
4334 | { | |
4335 | // Give the first TLS section the alignment of the | |
4336 | // entire TLS segment. Otherwise the TLS segment as a | |
4337 | // whole may be misaligned. | |
4338 | if (!*in_tls) | |
4339 | { | |
4340 | Output_segment* tls_segment = layout->tls_segment(); | |
4341 | gold_assert(tls_segment != NULL); | |
4342 | uint64_t segment_align = tls_segment->maximum_alignment(); | |
4343 | gold_assert(segment_align >= align); | |
4344 | align = segment_align; | |
4345 | ||
4346 | *in_tls = true; | |
4347 | } | |
4348 | } | |
4349 | else | |
4350 | { | |
4351 | // If this is the first section after the TLS segment, | |
4352 | // align it to at least the alignment of the TLS | |
4353 | // segment, so that the size of the overall TLS segment | |
4354 | // is aligned. | |
4355 | if (*in_tls) | |
4356 | { | |
4357 | uint64_t segment_align = | |
4358 | layout->tls_segment()->maximum_alignment(); | |
4359 | if (segment_align > align) | |
4360 | align = segment_align; | |
4361 | ||
4362 | *in_tls = false; | |
4363 | } | |
4364 | } | |
4365 | ||
fb0e076f | 4366 | if (!parameters->incremental_update()) |
cdc29364 CC |
4367 | { |
4368 | off = align_address(off, align); | |
4369 | (*p)->set_address_and_file_offset(addr + (off - startoff), off); | |
4370 | } | |
4371 | else | |
4372 | { | |
4373 | // Incremental update: allocate file space from free list. | |
4374 | (*p)->pre_finalize_data_size(); | |
4375 | off_t current_size = (*p)->current_data_size(); | |
4376 | off = layout->allocate(current_size, align, startoff); | |
4377 | if (off == -1) | |
4378 | { | |
4379 | gold_assert((*p)->output_section() != NULL); | |
e6455dfb CC |
4380 | gold_fallback(_("out of patch space for section %s; " |
4381 | "relink with --incremental-full"), | |
4382 | (*p)->output_section()->name()); | |
cdc29364 CC |
4383 | } |
4384 | (*p)->set_address_and_file_offset(addr + (off - startoff), off); | |
4385 | if ((*p)->data_size() > current_size) | |
4386 | { | |
4387 | gold_assert((*p)->output_section() != NULL); | |
e6455dfb CC |
4388 | gold_fallback(_("%s: section changed size; " |
4389 | "relink with --incremental-full"), | |
4390 | (*p)->output_section()->name()); | |
cdc29364 CC |
4391 | } |
4392 | } | |
3802b2dd | 4393 | } |
cdc29364 CC |
4394 | else if (parameters->incremental_update()) |
4395 | { | |
4396 | // For incremental updates, use the fixed offset for the | |
4397 | // high-water mark computation. | |
4398 | off = (*p)->offset(); | |
4399 | } | |
a445fddf ILT |
4400 | else |
4401 | { | |
4402 | // The script may have inserted a skip forward, but it | |
4403 | // better not have moved backward. | |
661be1e2 ILT |
4404 | if ((*p)->address() >= addr + (off - startoff)) |
4405 | off += (*p)->address() - (addr + (off - startoff)); | |
4406 | else | |
4407 | { | |
4408 | if (!layout->script_options()->saw_sections_clause()) | |
4409 | gold_unreachable(); | |
4410 | else | |
4411 | { | |
4412 | Output_section* os = (*p)->output_section(); | |
64b1ae37 DK |
4413 | |
4414 | // Cast to unsigned long long to avoid format warnings. | |
4415 | unsigned long long previous_dot = | |
4416 | static_cast<unsigned long long>(addr + (off - startoff)); | |
4417 | unsigned long long dot = | |
4418 | static_cast<unsigned long long>((*p)->address()); | |
4419 | ||
661be1e2 ILT |
4420 | if (os == NULL) |
4421 | gold_error(_("dot moves backward in linker script " | |
64b1ae37 | 4422 | "from 0x%llx to 0x%llx"), previous_dot, dot); |
661be1e2 ILT |
4423 | else |
4424 | gold_error(_("address of section '%s' moves backward " | |
4425 | "from 0x%llx to 0x%llx"), | |
64b1ae37 | 4426 | os->name(), previous_dot, dot); |
661be1e2 ILT |
4427 | } |
4428 | } | |
a445fddf ILT |
4429 | (*p)->set_file_offset(off); |
4430 | (*p)->finalize_data_size(); | |
4431 | } | |
ead1e424 | 4432 | |
9fbd3822 CC |
4433 | if (parameters->incremental_update()) |
4434 | gold_debug(DEBUG_INCREMENTAL, | |
4435 | "set_section_list_addresses: %08lx %08lx %s", | |
4436 | static_cast<long>(off), | |
4437 | static_cast<long>((*p)->data_size()), | |
4438 | ((*p)->output_section() != NULL | |
4439 | ? (*p)->output_section()->name() : "(special)")); | |
4440 | ||
4441 | // We want to ignore the size of a SHF_TLS SHT_NOBITS | |
96a2b4e4 ILT |
4442 | // section. Such a section does not affect the size of a |
4443 | // PT_LOAD segment. | |
4444 | if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS) | |
ead1e424 ILT |
4445 | || !(*p)->is_section_type(elfcpp::SHT_NOBITS)) |
4446 | off += (*p)->data_size(); | |
75f65a3e | 4447 | |
cdc29364 CC |
4448 | if (off > maxoff) |
4449 | maxoff = off; | |
4450 | ||
ead1e424 ILT |
4451 | if ((*p)->is_section()) |
4452 | { | |
4453 | (*p)->set_out_shndx(*pshndx); | |
4454 | ++*pshndx; | |
4455 | } | |
75f65a3e ILT |
4456 | } |
4457 | ||
cdc29364 CC |
4458 | *poff = maxoff; |
4459 | return addr + (maxoff - startoff); | |
75f65a3e ILT |
4460 | } |
4461 | ||
4462 | // For a non-PT_LOAD segment, set the offset from the sections, if | |
1a2dff53 | 4463 | // any. Add INCREASE to the file size and the memory size. |
75f65a3e ILT |
4464 | |
4465 | void | |
1a2dff53 | 4466 | Output_segment::set_offset(unsigned int increase) |
75f65a3e | 4467 | { |
a3ad94ed | 4468 | gold_assert(this->type_ != elfcpp::PT_LOAD); |
75f65a3e | 4469 | |
a445fddf ILT |
4470 | gold_assert(!this->are_addresses_set_); |
4471 | ||
22f0da72 ILT |
4472 | // A non-load section only uses output_lists_[0]. |
4473 | ||
4474 | Output_data_list* pdl = &this->output_lists_[0]; | |
4475 | ||
4476 | if (pdl->empty()) | |
75f65a3e | 4477 | { |
1a2dff53 | 4478 | gold_assert(increase == 0); |
75f65a3e ILT |
4479 | this->vaddr_ = 0; |
4480 | this->paddr_ = 0; | |
a445fddf | 4481 | this->are_addresses_set_ = true; |
75f65a3e | 4482 | this->memsz_ = 0; |
a445fddf | 4483 | this->min_p_align_ = 0; |
75f65a3e ILT |
4484 | this->offset_ = 0; |
4485 | this->filesz_ = 0; | |
4486 | return; | |
4487 | } | |
4488 | ||
22f0da72 | 4489 | // Find the first and last section by address. |
5f1ab67a ILT |
4490 | const Output_data* first = NULL; |
4491 | const Output_data* last_data = NULL; | |
4492 | const Output_data* last_bss = NULL; | |
22f0da72 ILT |
4493 | for (Output_data_list::const_iterator p = pdl->begin(); |
4494 | p != pdl->end(); | |
4495 | ++p) | |
4496 | { | |
4497 | if (first == NULL | |
4498 | || (*p)->address() < first->address() | |
4499 | || ((*p)->address() == first->address() | |
4500 | && (*p)->data_size() < first->data_size())) | |
4501 | first = *p; | |
4502 | const Output_data** plast; | |
4503 | if ((*p)->is_section() | |
4504 | && (*p)->output_section()->type() == elfcpp::SHT_NOBITS) | |
4505 | plast = &last_bss; | |
4506 | else | |
4507 | plast = &last_data; | |
4508 | if (*plast == NULL | |
4509 | || (*p)->address() > (*plast)->address() | |
4510 | || ((*p)->address() == (*plast)->address() | |
4511 | && (*p)->data_size() > (*plast)->data_size())) | |
4512 | *plast = *p; | |
4513 | } | |
5f1ab67a | 4514 | |
75f65a3e | 4515 | this->vaddr_ = first->address(); |
a445fddf ILT |
4516 | this->paddr_ = (first->has_load_address() |
4517 | ? first->load_address() | |
4518 | : this->vaddr_); | |
4519 | this->are_addresses_set_ = true; | |
75f65a3e ILT |
4520 | this->offset_ = first->offset(); |
4521 | ||
22f0da72 | 4522 | if (last_data == NULL) |
75f65a3e ILT |
4523 | this->filesz_ = 0; |
4524 | else | |
5f1ab67a ILT |
4525 | this->filesz_ = (last_data->address() |
4526 | + last_data->data_size() | |
4527 | - this->vaddr_); | |
75f65a3e | 4528 | |
5f1ab67a | 4529 | const Output_data* last = last_bss != NULL ? last_bss : last_data; |
75f65a3e ILT |
4530 | this->memsz_ = (last->address() |
4531 | + last->data_size() | |
4532 | - this->vaddr_); | |
96a2b4e4 | 4533 | |
1a2dff53 ILT |
4534 | this->filesz_ += increase; |
4535 | this->memsz_ += increase; | |
4536 | ||
fd064a5b CC |
4537 | // If this is a RELRO segment, verify that the segment ends at a |
4538 | // page boundary. | |
4539 | if (this->type_ == elfcpp::PT_GNU_RELRO) | |
4540 | { | |
4541 | uint64_t page_align = parameters->target().common_pagesize(); | |
4542 | uint64_t segment_end = this->vaddr_ + this->memsz_; | |
cdc29364 CC |
4543 | if (parameters->incremental_update()) |
4544 | { | |
4545 | // The INCREASE_RELRO calculation is bypassed for an incremental | |
4546 | // update, so we need to adjust the segment size manually here. | |
4547 | segment_end = align_address(segment_end, page_align); | |
4548 | this->memsz_ = segment_end - this->vaddr_; | |
4549 | } | |
4550 | else | |
4551 | gold_assert(segment_end == align_address(segment_end, page_align)); | |
fd064a5b CC |
4552 | } |
4553 | ||
96a2b4e4 ILT |
4554 | // If this is a TLS segment, align the memory size. The code in |
4555 | // set_section_list ensures that the section after the TLS segment | |
4556 | // is aligned to give us room. | |
4557 | if (this->type_ == elfcpp::PT_TLS) | |
4558 | { | |
4559 | uint64_t segment_align = this->maximum_alignment(); | |
4560 | gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align)); | |
4561 | this->memsz_ = align_address(this->memsz_, segment_align); | |
4562 | } | |
75f65a3e ILT |
4563 | } |
4564 | ||
7bf1f802 ILT |
4565 | // Set the TLS offsets of the sections in the PT_TLS segment. |
4566 | ||
4567 | void | |
4568 | Output_segment::set_tls_offsets() | |
4569 | { | |
4570 | gold_assert(this->type_ == elfcpp::PT_TLS); | |
4571 | ||
22f0da72 ILT |
4572 | for (Output_data_list::iterator p = this->output_lists_[0].begin(); |
4573 | p != this->output_lists_[0].end(); | |
7bf1f802 ILT |
4574 | ++p) |
4575 | (*p)->set_tls_offset(this->vaddr_); | |
4576 | } | |
4577 | ||
22f0da72 | 4578 | // Return the load address of the first section. |
a445fddf ILT |
4579 | |
4580 | uint64_t | |
4581 | Output_segment::first_section_load_address() const | |
4582 | { | |
22f0da72 ILT |
4583 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) |
4584 | { | |
4585 | const Output_data_list* pdl = &this->output_lists_[i]; | |
4586 | for (Output_data_list::const_iterator p = pdl->begin(); | |
4587 | p != pdl->end(); | |
4588 | ++p) | |
4589 | { | |
4590 | if ((*p)->is_section()) | |
4591 | return ((*p)->has_load_address() | |
4592 | ? (*p)->load_address() | |
4593 | : (*p)->address()); | |
4594 | } | |
4595 | } | |
a445fddf ILT |
4596 | gold_unreachable(); |
4597 | } | |
4598 | ||
75f65a3e ILT |
4599 | // Return the number of Output_sections in an Output_segment. |
4600 | ||
4601 | unsigned int | |
4602 | Output_segment::output_section_count() const | |
4603 | { | |
22f0da72 ILT |
4604 | unsigned int ret = 0; |
4605 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) | |
4606 | ret += this->output_section_count_list(&this->output_lists_[i]); | |
4607 | return ret; | |
75f65a3e ILT |
4608 | } |
4609 | ||
4610 | // Return the number of Output_sections in an Output_data_list. | |
4611 | ||
4612 | unsigned int | |
4613 | Output_segment::output_section_count_list(const Output_data_list* pdl) const | |
4614 | { | |
4615 | unsigned int count = 0; | |
4616 | for (Output_data_list::const_iterator p = pdl->begin(); | |
4617 | p != pdl->end(); | |
4618 | ++p) | |
4619 | { | |
4620 | if ((*p)->is_section()) | |
4621 | ++count; | |
4622 | } | |
4623 | return count; | |
a2fb1b05 ILT |
4624 | } |
4625 | ||
1c4f3631 ILT |
4626 | // Return the section attached to the list segment with the lowest |
4627 | // load address. This is used when handling a PHDRS clause in a | |
4628 | // linker script. | |
4629 | ||
4630 | Output_section* | |
4631 | Output_segment::section_with_lowest_load_address() const | |
4632 | { | |
4633 | Output_section* found = NULL; | |
4634 | uint64_t found_lma = 0; | |
22f0da72 ILT |
4635 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) |
4636 | this->lowest_load_address_in_list(&this->output_lists_[i], &found, | |
4637 | &found_lma); | |
1c4f3631 ILT |
4638 | return found; |
4639 | } | |
4640 | ||
4641 | // Look through a list for a section with a lower load address. | |
4642 | ||
4643 | void | |
4644 | Output_segment::lowest_load_address_in_list(const Output_data_list* pdl, | |
4645 | Output_section** found, | |
4646 | uint64_t* found_lma) const | |
4647 | { | |
4648 | for (Output_data_list::const_iterator p = pdl->begin(); | |
4649 | p != pdl->end(); | |
4650 | ++p) | |
4651 | { | |
4652 | if (!(*p)->is_section()) | |
4653 | continue; | |
4654 | Output_section* os = static_cast<Output_section*>(*p); | |
4655 | uint64_t lma = (os->has_load_address() | |
4656 | ? os->load_address() | |
4657 | : os->address()); | |
4658 | if (*found == NULL || lma < *found_lma) | |
4659 | { | |
4660 | *found = os; | |
4661 | *found_lma = lma; | |
4662 | } | |
4663 | } | |
4664 | } | |
4665 | ||
61ba1cf9 ILT |
4666 | // Write the segment data into *OPHDR. |
4667 | ||
4668 | template<int size, bool big_endian> | |
4669 | void | |
ead1e424 | 4670 | Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr) |
61ba1cf9 ILT |
4671 | { |
4672 | ophdr->put_p_type(this->type_); | |
4673 | ophdr->put_p_offset(this->offset_); | |
4674 | ophdr->put_p_vaddr(this->vaddr_); | |
4675 | ophdr->put_p_paddr(this->paddr_); | |
4676 | ophdr->put_p_filesz(this->filesz_); | |
4677 | ophdr->put_p_memsz(this->memsz_); | |
4678 | ophdr->put_p_flags(this->flags_); | |
a445fddf | 4679 | ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment())); |
61ba1cf9 ILT |
4680 | } |
4681 | ||
4682 | // Write the section headers into V. | |
4683 | ||
4684 | template<int size, bool big_endian> | |
4685 | unsigned char* | |
16649710 ILT |
4686 | Output_segment::write_section_headers(const Layout* layout, |
4687 | const Stringpool* secnamepool, | |
ead1e424 | 4688 | unsigned char* v, |
ca09d69a | 4689 | unsigned int* pshndx) const |
5482377d | 4690 | { |
ead1e424 ILT |
4691 | // Every section that is attached to a segment must be attached to a |
4692 | // PT_LOAD segment, so we only write out section headers for PT_LOAD | |
4693 | // segments. | |
4694 | if (this->type_ != elfcpp::PT_LOAD) | |
4695 | return v; | |
4696 | ||
22f0da72 ILT |
4697 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) |
4698 | { | |
4699 | const Output_data_list* pdl = &this->output_lists_[i]; | |
4700 | v = this->write_section_headers_list<size, big_endian>(layout, | |
4701 | secnamepool, | |
4702 | pdl, | |
4703 | v, pshndx); | |
4704 | } | |
4705 | ||
61ba1cf9 ILT |
4706 | return v; |
4707 | } | |
4708 | ||
4709 | template<int size, bool big_endian> | |
4710 | unsigned char* | |
16649710 ILT |
4711 | Output_segment::write_section_headers_list(const Layout* layout, |
4712 | const Stringpool* secnamepool, | |
61ba1cf9 | 4713 | const Output_data_list* pdl, |
ead1e424 | 4714 | unsigned char* v, |
7d1a9ebb | 4715 | unsigned int* pshndx) const |
61ba1cf9 ILT |
4716 | { |
4717 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
4718 | for (Output_data_list::const_iterator p = pdl->begin(); | |
4719 | p != pdl->end(); | |
4720 | ++p) | |
4721 | { | |
4722 | if ((*p)->is_section()) | |
4723 | { | |
5482377d | 4724 | const Output_section* ps = static_cast<const Output_section*>(*p); |
a3ad94ed | 4725 | gold_assert(*pshndx == ps->out_shndx()); |
61ba1cf9 | 4726 | elfcpp::Shdr_write<size, big_endian> oshdr(v); |
16649710 | 4727 | ps->write_header(layout, secnamepool, &oshdr); |
61ba1cf9 | 4728 | v += shdr_size; |
ead1e424 | 4729 | ++*pshndx; |
61ba1cf9 ILT |
4730 | } |
4731 | } | |
4732 | return v; | |
4733 | } | |
4734 | ||
7d9e3d98 ILT |
4735 | // Print the output sections to the map file. |
4736 | ||
4737 | void | |
4738 | Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const | |
4739 | { | |
4740 | if (this->type() != elfcpp::PT_LOAD) | |
4741 | return; | |
22f0da72 ILT |
4742 | for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i) |
4743 | this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]); | |
7d9e3d98 ILT |
4744 | } |
4745 | ||
4746 | // Print an output section list to the map file. | |
4747 | ||
4748 | void | |
4749 | Output_segment::print_section_list_to_mapfile(Mapfile* mapfile, | |
4750 | const Output_data_list* pdl) const | |
4751 | { | |
4752 | for (Output_data_list::const_iterator p = pdl->begin(); | |
4753 | p != pdl->end(); | |
4754 | ++p) | |
4755 | (*p)->print_to_mapfile(mapfile); | |
4756 | } | |
4757 | ||
a2fb1b05 ILT |
4758 | // Output_file methods. |
4759 | ||
14144f39 ILT |
4760 | Output_file::Output_file(const char* name) |
4761 | : name_(name), | |
61ba1cf9 ILT |
4762 | o_(-1), |
4763 | file_size_(0), | |
c420411f | 4764 | base_(NULL), |
516cb3d0 | 4765 | map_is_anonymous_(false), |
88597d34 | 4766 | map_is_allocated_(false), |
516cb3d0 | 4767 | is_temporary_(false) |
61ba1cf9 ILT |
4768 | { |
4769 | } | |
4770 | ||
404c2abb | 4771 | // Try to open an existing file. Returns false if the file doesn't |
aa92d6ed CC |
4772 | // exist, has a size of 0 or can't be mmapped. If BASE_NAME is not |
4773 | // NULL, open that file as the base for incremental linking, and | |
4774 | // copy its contents to the new output file. This routine can | |
4775 | // be called for incremental updates, in which case WRITABLE should | |
4776 | // be true, or by the incremental-dump utility, in which case | |
4777 | // WRITABLE should be false. | |
404c2abb ILT |
4778 | |
4779 | bool | |
aa92d6ed | 4780 | Output_file::open_base_file(const char* base_name, bool writable) |
404c2abb ILT |
4781 | { |
4782 | // The name "-" means "stdout". | |
4783 | if (strcmp(this->name_, "-") == 0) | |
4784 | return false; | |
4785 | ||
aa92d6ed CC |
4786 | bool use_base_file = base_name != NULL; |
4787 | if (!use_base_file) | |
4788 | base_name = this->name_; | |
4789 | else if (strcmp(base_name, this->name_) == 0) | |
4790 | gold_fatal(_("%s: incremental base and output file name are the same"), | |
4791 | base_name); | |
4792 | ||
404c2abb ILT |
4793 | // Don't bother opening files with a size of zero. |
4794 | struct stat s; | |
aa92d6ed CC |
4795 | if (::stat(base_name, &s) != 0) |
4796 | { | |
4797 | gold_info(_("%s: stat: %s"), base_name, strerror(errno)); | |
4798 | return false; | |
4799 | } | |
4800 | if (s.st_size == 0) | |
4801 | { | |
4802 | gold_info(_("%s: incremental base file is empty"), base_name); | |
4803 | return false; | |
4804 | } | |
4805 | ||
4806 | // If we're using a base file, we want to open it read-only. | |
4807 | if (use_base_file) | |
4808 | writable = false; | |
404c2abb | 4809 | |
aa92d6ed CC |
4810 | int oflags = writable ? O_RDWR : O_RDONLY; |
4811 | int o = open_descriptor(-1, base_name, oflags, 0); | |
404c2abb | 4812 | if (o < 0) |
aa92d6ed CC |
4813 | { |
4814 | gold_info(_("%s: open: %s"), base_name, strerror(errno)); | |
4815 | return false; | |
4816 | } | |
4817 | ||
4818 | // If the base file and the output file are different, open a | |
4819 | // new output file and read the contents from the base file into | |
4820 | // the newly-mapped region. | |
4821 | if (use_base_file) | |
4822 | { | |
4823 | this->open(s.st_size); | |
dfb45471 CC |
4824 | ssize_t bytes_to_read = s.st_size; |
4825 | unsigned char* p = this->base_; | |
4826 | while (bytes_to_read > 0) | |
4827 | { | |
4828 | ssize_t len = ::read(o, p, bytes_to_read); | |
4829 | if (len < 0) | |
4830 | { | |
4831 | gold_info(_("%s: read failed: %s"), base_name, strerror(errno)); | |
4832 | return false; | |
4833 | } | |
4834 | if (len == 0) | |
4835 | { | |
4836 | gold_info(_("%s: file too short: read only %lld of %lld bytes"), | |
4837 | base_name, | |
4838 | static_cast<long long>(s.st_size - bytes_to_read), | |
4839 | static_cast<long long>(s.st_size)); | |
4840 | return false; | |
4841 | } | |
4842 | p += len; | |
4843 | bytes_to_read -= len; | |
4844 | } | |
aa92d6ed CC |
4845 | ::close(o); |
4846 | return true; | |
4847 | } | |
4848 | ||
404c2abb ILT |
4849 | this->o_ = o; |
4850 | this->file_size_ = s.st_size; | |
4851 | ||
aa92d6ed | 4852 | if (!this->map_no_anonymous(writable)) |
404c2abb ILT |
4853 | { |
4854 | release_descriptor(o, true); | |
4855 | this->o_ = -1; | |
4856 | this->file_size_ = 0; | |
4857 | return false; | |
4858 | } | |
4859 | ||
4860 | return true; | |
4861 | } | |
4862 | ||
61ba1cf9 ILT |
4863 | // Open the output file. |
4864 | ||
a2fb1b05 | 4865 | void |
61ba1cf9 | 4866 | Output_file::open(off_t file_size) |
a2fb1b05 | 4867 | { |
61ba1cf9 ILT |
4868 | this->file_size_ = file_size; |
4869 | ||
4e9d8586 ILT |
4870 | // Unlink the file first; otherwise the open() may fail if the file |
4871 | // is busy (e.g. it's an executable that's currently being executed). | |
4872 | // | |
4873 | // However, the linker may be part of a system where a zero-length | |
4874 | // file is created for it to write to, with tight permissions (gcc | |
4875 | // 2.95 did something like this). Unlinking the file would work | |
4876 | // around those permission controls, so we only unlink if the file | |
4877 | // has a non-zero size. We also unlink only regular files to avoid | |
4878 | // trouble with directories/etc. | |
4879 | // | |
4880 | // If we fail, continue; this command is merely a best-effort attempt | |
4881 | // to improve the odds for open(). | |
4882 | ||
42a1b686 | 4883 | // We let the name "-" mean "stdout" |
516cb3d0 | 4884 | if (!this->is_temporary_) |
42a1b686 | 4885 | { |
516cb3d0 ILT |
4886 | if (strcmp(this->name_, "-") == 0) |
4887 | this->o_ = STDOUT_FILENO; | |
4888 | else | |
4889 | { | |
4890 | struct stat s; | |
6a89f575 CC |
4891 | if (::stat(this->name_, &s) == 0 |
4892 | && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode))) | |
4893 | { | |
4894 | if (s.st_size != 0) | |
4895 | ::unlink(this->name_); | |
4896 | else if (!parameters->options().relocatable()) | |
4897 | { | |
4898 | // If we don't unlink the existing file, add execute | |
4899 | // permission where read permissions already exist | |
4900 | // and where the umask permits. | |
4901 | int mask = ::umask(0); | |
4902 | ::umask(mask); | |
4903 | s.st_mode |= (s.st_mode & 0444) >> 2; | |
4904 | ::chmod(this->name_, s.st_mode & ~mask); | |
4905 | } | |
4906 | } | |
516cb3d0 | 4907 | |
8851ecca | 4908 | int mode = parameters->options().relocatable() ? 0666 : 0777; |
2a00e4fb ILT |
4909 | int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC, |
4910 | mode); | |
516cb3d0 ILT |
4911 | if (o < 0) |
4912 | gold_fatal(_("%s: open: %s"), this->name_, strerror(errno)); | |
4913 | this->o_ = o; | |
4914 | } | |
42a1b686 | 4915 | } |
61ba1cf9 | 4916 | |
27bc2bce ILT |
4917 | this->map(); |
4918 | } | |
4919 | ||
4920 | // Resize the output file. | |
4921 | ||
4922 | void | |
4923 | Output_file::resize(off_t file_size) | |
4924 | { | |
c420411f ILT |
4925 | // If the mmap is mapping an anonymous memory buffer, this is easy: |
4926 | // just mremap to the new size. If it's mapping to a file, we want | |
4927 | // to unmap to flush to the file, then remap after growing the file. | |
4928 | if (this->map_is_anonymous_) | |
4929 | { | |
88597d34 ILT |
4930 | void* base; |
4931 | if (!this->map_is_allocated_) | |
4932 | { | |
4933 | base = ::mremap(this->base_, this->file_size_, file_size, | |
4934 | MREMAP_MAYMOVE); | |
4935 | if (base == MAP_FAILED) | |
4936 | gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno)); | |
4937 | } | |
4938 | else | |
4939 | { | |
4940 | base = realloc(this->base_, file_size); | |
4941 | if (base == NULL) | |
4942 | gold_nomem(); | |
4943 | if (file_size > this->file_size_) | |
4944 | memset(static_cast<char*>(base) + this->file_size_, 0, | |
4945 | file_size - this->file_size_); | |
4946 | } | |
c420411f ILT |
4947 | this->base_ = static_cast<unsigned char*>(base); |
4948 | this->file_size_ = file_size; | |
4949 | } | |
4950 | else | |
4951 | { | |
4952 | this->unmap(); | |
4953 | this->file_size_ = file_size; | |
aa92d6ed | 4954 | if (!this->map_no_anonymous(true)) |
fdcac5af | 4955 | gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno)); |
c420411f | 4956 | } |
27bc2bce ILT |
4957 | } |
4958 | ||
404c2abb ILT |
4959 | // Map an anonymous block of memory which will later be written to the |
4960 | // file. Return whether the map succeeded. | |
26736d8e | 4961 | |
404c2abb | 4962 | bool |
26736d8e ILT |
4963 | Output_file::map_anonymous() |
4964 | { | |
404c2abb ILT |
4965 | void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE, |
4966 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
88597d34 | 4967 | if (base == MAP_FAILED) |
404c2abb | 4968 | { |
88597d34 ILT |
4969 | base = malloc(this->file_size_); |
4970 | if (base == NULL) | |
4971 | return false; | |
4972 | memset(base, 0, this->file_size_); | |
4973 | this->map_is_allocated_ = true; | |
404c2abb | 4974 | } |
88597d34 ILT |
4975 | this->base_ = static_cast<unsigned char*>(base); |
4976 | this->map_is_anonymous_ = true; | |
4977 | return true; | |
26736d8e ILT |
4978 | } |
4979 | ||
404c2abb | 4980 | // Map the file into memory. Return whether the mapping succeeded. |
aa92d6ed | 4981 | // If WRITABLE is true, map with write access. |
27bc2bce | 4982 | |
404c2abb | 4983 | bool |
aa92d6ed | 4984 | Output_file::map_no_anonymous(bool writable) |
27bc2bce | 4985 | { |
c420411f | 4986 | const int o = this->o_; |
61ba1cf9 | 4987 | |
c420411f ILT |
4988 | // If the output file is not a regular file, don't try to mmap it; |
4989 | // instead, we'll mmap a block of memory (an anonymous buffer), and | |
4990 | // then later write the buffer to the file. | |
4991 | void* base; | |
4992 | struct stat statbuf; | |
42a1b686 ILT |
4993 | if (o == STDOUT_FILENO || o == STDERR_FILENO |
4994 | || ::fstat(o, &statbuf) != 0 | |
516cb3d0 ILT |
4995 | || !S_ISREG(statbuf.st_mode) |
4996 | || this->is_temporary_) | |
404c2abb ILT |
4997 | return false; |
4998 | ||
4999 | // Ensure that we have disk space available for the file. If we | |
5000 | // don't do this, it is possible that we will call munmap, close, | |
5001 | // and exit with dirty buffers still in the cache with no assigned | |
5002 | // disk blocks. If the disk is out of space at that point, the | |
5003 | // output file will wind up incomplete, but we will have already | |
5004 | // exited. The alternative to fallocate would be to use fdatasync, | |
5005 | // but that would be a more significant performance hit. | |
bab9090f CC |
5006 | if (writable) |
5007 | { | |
5008 | int err = ::posix_fallocate(o, 0, this->file_size_); | |
5009 | if (err != 0) | |
5010 | gold_fatal(_("%s: %s"), this->name_, strerror(err)); | |
5011 | } | |
404c2abb ILT |
5012 | |
5013 | // Map the file into memory. | |
aa92d6ed CC |
5014 | int prot = PROT_READ; |
5015 | if (writable) | |
5016 | prot |= PROT_WRITE; | |
5017 | base = ::mmap(NULL, this->file_size_, prot, MAP_SHARED, o, 0); | |
404c2abb ILT |
5018 | |
5019 | // The mmap call might fail because of file system issues: the file | |
5020 | // system might not support mmap at all, or it might not support | |
5021 | // mmap with PROT_WRITE. | |
61ba1cf9 | 5022 | if (base == MAP_FAILED) |
404c2abb ILT |
5023 | return false; |
5024 | ||
5025 | this->map_is_anonymous_ = false; | |
61ba1cf9 | 5026 | this->base_ = static_cast<unsigned char*>(base); |
404c2abb ILT |
5027 | return true; |
5028 | } | |
5029 | ||
5030 | // Map the file into memory. | |
5031 | ||
5032 | void | |
5033 | Output_file::map() | |
5034 | { | |
aa92d6ed | 5035 | if (this->map_no_anonymous(true)) |
404c2abb ILT |
5036 | return; |
5037 | ||
5038 | // The mmap call might fail because of file system issues: the file | |
5039 | // system might not support mmap at all, or it might not support | |
5040 | // mmap with PROT_WRITE. I'm not sure which errno values we will | |
5041 | // see in all cases, so if the mmap fails for any reason and we | |
5042 | // don't care about file contents, try for an anonymous map. | |
5043 | if (this->map_anonymous()) | |
5044 | return; | |
5045 | ||
5046 | gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"), | |
5047 | this->name_, static_cast<unsigned long>(this->file_size_), | |
5048 | strerror(errno)); | |
61ba1cf9 ILT |
5049 | } |
5050 | ||
c420411f | 5051 | // Unmap the file from memory. |
61ba1cf9 ILT |
5052 | |
5053 | void | |
c420411f | 5054 | Output_file::unmap() |
61ba1cf9 | 5055 | { |
88597d34 ILT |
5056 | if (this->map_is_anonymous_) |
5057 | { | |
5058 | // We've already written out the data, so there is no reason to | |
5059 | // waste time unmapping or freeing the memory. | |
5060 | } | |
5061 | else | |
5062 | { | |
5063 | if (::munmap(this->base_, this->file_size_) < 0) | |
5064 | gold_error(_("%s: munmap: %s"), this->name_, strerror(errno)); | |
5065 | } | |
61ba1cf9 | 5066 | this->base_ = NULL; |
c420411f ILT |
5067 | } |
5068 | ||
5069 | // Close the output file. | |
5070 | ||
5071 | void | |
5072 | Output_file::close() | |
5073 | { | |
5074 | // If the map isn't file-backed, we need to write it now. | |
516cb3d0 | 5075 | if (this->map_is_anonymous_ && !this->is_temporary_) |
c420411f ILT |
5076 | { |
5077 | size_t bytes_to_write = this->file_size_; | |
6d1e3092 | 5078 | size_t offset = 0; |
c420411f ILT |
5079 | while (bytes_to_write > 0) |
5080 | { | |
6d1e3092 CD |
5081 | ssize_t bytes_written = ::write(this->o_, this->base_ + offset, |
5082 | bytes_to_write); | |
c420411f ILT |
5083 | if (bytes_written == 0) |
5084 | gold_error(_("%s: write: unexpected 0 return-value"), this->name_); | |
5085 | else if (bytes_written < 0) | |
5086 | gold_error(_("%s: write: %s"), this->name_, strerror(errno)); | |
5087 | else | |
6d1e3092 CD |
5088 | { |
5089 | bytes_to_write -= bytes_written; | |
5090 | offset += bytes_written; | |
5091 | } | |
c420411f ILT |
5092 | } |
5093 | } | |
5094 | this->unmap(); | |
61ba1cf9 | 5095 | |
42a1b686 | 5096 | // We don't close stdout or stderr |
516cb3d0 ILT |
5097 | if (this->o_ != STDOUT_FILENO |
5098 | && this->o_ != STDERR_FILENO | |
5099 | && !this->is_temporary_) | |
42a1b686 ILT |
5100 | if (::close(this->o_) < 0) |
5101 | gold_error(_("%s: close: %s"), this->name_, strerror(errno)); | |
61ba1cf9 | 5102 | this->o_ = -1; |
a2fb1b05 ILT |
5103 | } |
5104 | ||
5105 | // Instantiate the templates we need. We could use the configure | |
5106 | // script to restrict this to only the ones for implemented targets. | |
5107 | ||
193a53d9 | 5108 | #ifdef HAVE_TARGET_32_LITTLE |
a2fb1b05 ILT |
5109 | template |
5110 | off_t | |
5111 | Output_section::add_input_section<32, false>( | |
6e9ba2ca | 5112 | Layout* layout, |
6fa2a40b | 5113 | Sized_relobj_file<32, false>* object, |
2ea97941 | 5114 | unsigned int shndx, |
a2fb1b05 | 5115 | const char* secname, |
730cdc88 | 5116 | const elfcpp::Shdr<32, false>& shdr, |
a445fddf ILT |
5117 | unsigned int reloc_shndx, |
5118 | bool have_sections_script); | |
193a53d9 | 5119 | #endif |
a2fb1b05 | 5120 | |
193a53d9 | 5121 | #ifdef HAVE_TARGET_32_BIG |
a2fb1b05 ILT |
5122 | template |
5123 | off_t | |
5124 | Output_section::add_input_section<32, true>( | |
6e9ba2ca | 5125 | Layout* layout, |
6fa2a40b | 5126 | Sized_relobj_file<32, true>* object, |
2ea97941 | 5127 | unsigned int shndx, |
a2fb1b05 | 5128 | const char* secname, |
730cdc88 | 5129 | const elfcpp::Shdr<32, true>& shdr, |
a445fddf ILT |
5130 | unsigned int reloc_shndx, |
5131 | bool have_sections_script); | |
193a53d9 | 5132 | #endif |
a2fb1b05 | 5133 | |
193a53d9 | 5134 | #ifdef HAVE_TARGET_64_LITTLE |
a2fb1b05 ILT |
5135 | template |
5136 | off_t | |
5137 | Output_section::add_input_section<64, false>( | |
6e9ba2ca | 5138 | Layout* layout, |
6fa2a40b | 5139 | Sized_relobj_file<64, false>* object, |
2ea97941 | 5140 | unsigned int shndx, |
a2fb1b05 | 5141 | const char* secname, |
730cdc88 | 5142 | const elfcpp::Shdr<64, false>& shdr, |
a445fddf ILT |
5143 | unsigned int reloc_shndx, |
5144 | bool have_sections_script); | |
193a53d9 | 5145 | #endif |
a2fb1b05 | 5146 | |
193a53d9 | 5147 | #ifdef HAVE_TARGET_64_BIG |
a2fb1b05 ILT |
5148 | template |
5149 | off_t | |
5150 | Output_section::add_input_section<64, true>( | |
6e9ba2ca | 5151 | Layout* layout, |
6fa2a40b | 5152 | Sized_relobj_file<64, true>* object, |
2ea97941 | 5153 | unsigned int shndx, |
a2fb1b05 | 5154 | const char* secname, |
730cdc88 | 5155 | const elfcpp::Shdr<64, true>& shdr, |
a445fddf ILT |
5156 | unsigned int reloc_shndx, |
5157 | bool have_sections_script); | |
193a53d9 | 5158 | #endif |
a2fb1b05 | 5159 | |
bbbfea06 CC |
5160 | #ifdef HAVE_TARGET_32_LITTLE |
5161 | template | |
5162 | class Output_reloc<elfcpp::SHT_REL, false, 32, false>; | |
5163 | #endif | |
5164 | ||
5165 | #ifdef HAVE_TARGET_32_BIG | |
5166 | template | |
5167 | class Output_reloc<elfcpp::SHT_REL, false, 32, true>; | |
5168 | #endif | |
5169 | ||
5170 | #ifdef HAVE_TARGET_64_LITTLE | |
5171 | template | |
5172 | class Output_reloc<elfcpp::SHT_REL, false, 64, false>; | |
5173 | #endif | |
5174 | ||
5175 | #ifdef HAVE_TARGET_64_BIG | |
5176 | template | |
5177 | class Output_reloc<elfcpp::SHT_REL, false, 64, true>; | |
5178 | #endif | |
5179 | ||
5180 | #ifdef HAVE_TARGET_32_LITTLE | |
5181 | template | |
5182 | class Output_reloc<elfcpp::SHT_REL, true, 32, false>; | |
5183 | #endif | |
5184 | ||
5185 | #ifdef HAVE_TARGET_32_BIG | |
5186 | template | |
5187 | class Output_reloc<elfcpp::SHT_REL, true, 32, true>; | |
5188 | #endif | |
5189 | ||
5190 | #ifdef HAVE_TARGET_64_LITTLE | |
5191 | template | |
5192 | class Output_reloc<elfcpp::SHT_REL, true, 64, false>; | |
5193 | #endif | |
5194 | ||
5195 | #ifdef HAVE_TARGET_64_BIG | |
5196 | template | |
5197 | class Output_reloc<elfcpp::SHT_REL, true, 64, true>; | |
5198 | #endif | |
5199 | ||
5200 | #ifdef HAVE_TARGET_32_LITTLE | |
5201 | template | |
5202 | class Output_reloc<elfcpp::SHT_RELA, false, 32, false>; | |
5203 | #endif | |
5204 | ||
5205 | #ifdef HAVE_TARGET_32_BIG | |
5206 | template | |
5207 | class Output_reloc<elfcpp::SHT_RELA, false, 32, true>; | |
5208 | #endif | |
5209 | ||
5210 | #ifdef HAVE_TARGET_64_LITTLE | |
5211 | template | |
5212 | class Output_reloc<elfcpp::SHT_RELA, false, 64, false>; | |
5213 | #endif | |
5214 | ||
5215 | #ifdef HAVE_TARGET_64_BIG | |
5216 | template | |
5217 | class Output_reloc<elfcpp::SHT_RELA, false, 64, true>; | |
5218 | #endif | |
5219 | ||
5220 | #ifdef HAVE_TARGET_32_LITTLE | |
5221 | template | |
5222 | class Output_reloc<elfcpp::SHT_RELA, true, 32, false>; | |
5223 | #endif | |
5224 | ||
5225 | #ifdef HAVE_TARGET_32_BIG | |
5226 | template | |
5227 | class Output_reloc<elfcpp::SHT_RELA, true, 32, true>; | |
5228 | #endif | |
5229 | ||
5230 | #ifdef HAVE_TARGET_64_LITTLE | |
5231 | template | |
5232 | class Output_reloc<elfcpp::SHT_RELA, true, 64, false>; | |
5233 | #endif | |
5234 | ||
5235 | #ifdef HAVE_TARGET_64_BIG | |
5236 | template | |
5237 | class Output_reloc<elfcpp::SHT_RELA, true, 64, true>; | |
5238 | #endif | |
5239 | ||
193a53d9 | 5240 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
5241 | template |
5242 | class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>; | |
193a53d9 | 5243 | #endif |
c06b7b0b | 5244 | |
193a53d9 | 5245 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
5246 | template |
5247 | class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>; | |
193a53d9 | 5248 | #endif |
c06b7b0b | 5249 | |
193a53d9 | 5250 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
5251 | template |
5252 | class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>; | |
193a53d9 | 5253 | #endif |
c06b7b0b | 5254 | |
193a53d9 | 5255 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
5256 | template |
5257 | class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>; | |
193a53d9 | 5258 | #endif |
c06b7b0b | 5259 | |
193a53d9 | 5260 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
5261 | template |
5262 | class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>; | |
193a53d9 | 5263 | #endif |
c06b7b0b | 5264 | |
193a53d9 | 5265 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
5266 | template |
5267 | class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>; | |
193a53d9 | 5268 | #endif |
c06b7b0b | 5269 | |
193a53d9 | 5270 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
5271 | template |
5272 | class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>; | |
193a53d9 | 5273 | #endif |
c06b7b0b | 5274 | |
193a53d9 | 5275 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
5276 | template |
5277 | class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>; | |
193a53d9 | 5278 | #endif |
c06b7b0b | 5279 | |
193a53d9 | 5280 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
5281 | template |
5282 | class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>; | |
193a53d9 | 5283 | #endif |
c06b7b0b | 5284 | |
193a53d9 | 5285 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
5286 | template |
5287 | class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>; | |
193a53d9 | 5288 | #endif |
c06b7b0b | 5289 | |
193a53d9 | 5290 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
5291 | template |
5292 | class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>; | |
193a53d9 | 5293 | #endif |
c06b7b0b | 5294 | |
193a53d9 | 5295 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
5296 | template |
5297 | class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>; | |
193a53d9 | 5298 | #endif |
c06b7b0b | 5299 | |
193a53d9 | 5300 | #ifdef HAVE_TARGET_32_LITTLE |
c06b7b0b ILT |
5301 | template |
5302 | class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>; | |
193a53d9 | 5303 | #endif |
c06b7b0b | 5304 | |
193a53d9 | 5305 | #ifdef HAVE_TARGET_32_BIG |
c06b7b0b ILT |
5306 | template |
5307 | class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>; | |
193a53d9 | 5308 | #endif |
c06b7b0b | 5309 | |
193a53d9 | 5310 | #ifdef HAVE_TARGET_64_LITTLE |
c06b7b0b ILT |
5311 | template |
5312 | class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>; | |
193a53d9 | 5313 | #endif |
c06b7b0b | 5314 | |
193a53d9 | 5315 | #ifdef HAVE_TARGET_64_BIG |
c06b7b0b ILT |
5316 | template |
5317 | class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>; | |
193a53d9 | 5318 | #endif |
c06b7b0b | 5319 | |
6a74a719 ILT |
5320 | #ifdef HAVE_TARGET_32_LITTLE |
5321 | template | |
5322 | class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>; | |
5323 | #endif | |
5324 | ||
5325 | #ifdef HAVE_TARGET_32_BIG | |
5326 | template | |
5327 | class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>; | |
5328 | #endif | |
5329 | ||
5330 | #ifdef HAVE_TARGET_64_LITTLE | |
5331 | template | |
5332 | class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>; | |
5333 | #endif | |
5334 | ||
5335 | #ifdef HAVE_TARGET_64_BIG | |
5336 | template | |
5337 | class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>; | |
5338 | #endif | |
5339 | ||
5340 | #ifdef HAVE_TARGET_32_LITTLE | |
5341 | template | |
5342 | class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>; | |
5343 | #endif | |
5344 | ||
5345 | #ifdef HAVE_TARGET_32_BIG | |
5346 | template | |
5347 | class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>; | |
5348 | #endif | |
5349 | ||
5350 | #ifdef HAVE_TARGET_64_LITTLE | |
5351 | template | |
5352 | class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>; | |
5353 | #endif | |
5354 | ||
5355 | #ifdef HAVE_TARGET_64_BIG | |
5356 | template | |
5357 | class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>; | |
5358 | #endif | |
5359 | ||
5360 | #ifdef HAVE_TARGET_32_LITTLE | |
5361 | template | |
5362 | class Output_data_group<32, false>; | |
5363 | #endif | |
5364 | ||
5365 | #ifdef HAVE_TARGET_32_BIG | |
5366 | template | |
5367 | class Output_data_group<32, true>; | |
5368 | #endif | |
5369 | ||
5370 | #ifdef HAVE_TARGET_64_LITTLE | |
5371 | template | |
5372 | class Output_data_group<64, false>; | |
5373 | #endif | |
5374 | ||
5375 | #ifdef HAVE_TARGET_64_BIG | |
5376 | template | |
5377 | class Output_data_group<64, true>; | |
5378 | #endif | |
5379 | ||
193a53d9 | 5380 | #ifdef HAVE_TARGET_32_LITTLE |
ead1e424 | 5381 | template |
dbe717ef | 5382 | class Output_data_got<32, false>; |
193a53d9 | 5383 | #endif |
ead1e424 | 5384 | |
193a53d9 | 5385 | #ifdef HAVE_TARGET_32_BIG |
ead1e424 | 5386 | template |
dbe717ef | 5387 | class Output_data_got<32, true>; |
193a53d9 | 5388 | #endif |
ead1e424 | 5389 | |
193a53d9 | 5390 | #ifdef HAVE_TARGET_64_LITTLE |
ead1e424 | 5391 | template |
dbe717ef | 5392 | class Output_data_got<64, false>; |
193a53d9 | 5393 | #endif |
ead1e424 | 5394 | |
193a53d9 | 5395 | #ifdef HAVE_TARGET_64_BIG |
ead1e424 | 5396 | template |
dbe717ef | 5397 | class Output_data_got<64, true>; |
193a53d9 | 5398 | #endif |
ead1e424 | 5399 | |
a2fb1b05 | 5400 | } // End namespace gold. |