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