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