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