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