Automatic date update in version.in
[deliverable/binutils-gdb.git] / ld / ldwrite.c
CommitLineData
252b5132 1/* ldwrite.c -- write out the linked file
82704155 2 Copyright (C) 1991-2019 Free Software Foundation, Inc.
252b5132
RH
3 Written by Steve Chamberlain sac@cygnus.com
4
f96b4a7b
NC
5 This file is part of the GNU Binutils.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132 21
252b5132 22#include "sysdep.h"
3db64b00 23#include "bfd.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libiberty.h"
1ff6de03 26#include "ctf-api.h"
29ca8dc5 27#include "safe-ctype.h"
252b5132
RH
28
29#include "ld.h"
30#include "ldexp.h"
31#include "ldlang.h"
32#include "ldwrite.h"
33#include "ldmisc.h"
df2a7313 34#include <ldgram.h>
252b5132
RH
35#include "ldmain.h"
36
252b5132
RH
37/* Build link_order structures for the BFD linker. */
38
39static void
1579bae1 40build_link_order (lang_statement_union_type *statement)
252b5132
RH
41{
42 switch (statement->header.type)
43 {
44 case lang_data_statement_enum:
45 {
46 asection *output_section;
47 struct bfd_link_order *link_order;
48 bfd_vma value;
b34976b6 49 bfd_boolean big_endian = FALSE;
252b5132
RH
50
51 output_section = statement->data_statement.output_section;
f13a99db 52 ASSERT (output_section->owner == link_info.output_bfd);
252b5132 53
2b42b063
AM
54 if (!((output_section->flags & SEC_HAS_CONTENTS) != 0
55 || ((output_section->flags & SEC_LOAD) != 0
56 && (output_section->flags & SEC_THREAD_LOCAL))))
57 break;
58
f13a99db 59 link_order = bfd_new_link_order (link_info.output_bfd, output_section);
252b5132 60 if (link_order == NULL)
df5f2391 61 einfo (_("%F%P: bfd_new_link_order failed\n"));
252b5132
RH
62
63 link_order->type = bfd_data_link_order;
7fabd029 64 link_order->offset = statement->data_statement.output_offset;
1e9cc1c2 65 link_order->u.data.contents = (bfd_byte *) xmalloc (QUAD_SIZE);
252b5132
RH
66
67 value = statement->data_statement.value;
68
69 /* If the endianness of the output BFD is not known, then we
70 base the endianness of the data on the first input file.
71 By convention, the bfd_put routines for an unknown
72 endianness are big endian, so we must swap here if the
73 input file is little endian. */
f13a99db 74 if (bfd_big_endian (link_info.output_bfd))
b34976b6 75 big_endian = TRUE;
f13a99db 76 else if (bfd_little_endian (link_info.output_bfd))
b34976b6 77 big_endian = FALSE;
252b5132
RH
78 else
79 {
b34976b6 80 bfd_boolean swap;
252b5132 81
b34976b6 82 swap = FALSE;
252b5132 83 if (command_line.endian == ENDIAN_BIG)
b34976b6 84 big_endian = TRUE;
252b5132
RH
85 else if (command_line.endian == ENDIAN_LITTLE)
86 {
b34976b6
AM
87 big_endian = FALSE;
88 swap = TRUE;
252b5132
RH
89 }
90 else if (command_line.endian == ENDIAN_UNSET)
91 {
b34976b6 92 big_endian = TRUE;
252b5132
RH
93 {
94 LANG_FOR_EACH_INPUT_STATEMENT (s)
0aa7f586
AM
95 {
96 if (s->the_bfd != NULL)
97 {
98 if (bfd_little_endian (s->the_bfd))
99 {
100 big_endian = FALSE;
101 swap = TRUE;
102 }
103 break;
104 }
105 }
252b5132
RH
106 }
107 }
108
109 if (swap)
110 {
111 bfd_byte buffer[8];
112
113 switch (statement->data_statement.type)
114 {
115 case QUAD:
116 case SQUAD:
117 if (sizeof (bfd_vma) >= QUAD_SIZE)
118 {
119 bfd_putl64 (value, buffer);
120 value = bfd_getb64 (buffer);
121 break;
122 }
123 /* Fall through. */
124 case LONG:
125 bfd_putl32 (value, buffer);
126 value = bfd_getb32 (buffer);
127 break;
128 case SHORT:
129 bfd_putl16 (value, buffer);
130 value = bfd_getb16 (buffer);
131 break;
132 case BYTE:
133 break;
134 default:
135 abort ();
136 }
137 }
138 }
139
f13a99db 140 ASSERT (output_section->owner == link_info.output_bfd);
252b5132
RH
141 switch (statement->data_statement.type)
142 {
143 case QUAD:
144 case SQUAD:
145 if (sizeof (bfd_vma) >= QUAD_SIZE)
f13a99db
AM
146 bfd_put_64 (link_info.output_bfd, value,
147 link_order->u.data.contents);
252b5132
RH
148 else
149 {
150 bfd_vma high;
151
152 if (statement->data_statement.type == QUAD)
153 high = 0;
154 else if ((value & 0x80000000) == 0)
155 high = 0;
156 else
157 high = (bfd_vma) -1;
f13a99db 158 bfd_put_32 (link_info.output_bfd, high,
252b5132
RH
159 (link_order->u.data.contents
160 + (big_endian ? 0 : 4)));
f13a99db 161 bfd_put_32 (link_info.output_bfd, value,
252b5132
RH
162 (link_order->u.data.contents
163 + (big_endian ? 4 : 0)));
164 }
165 link_order->size = QUAD_SIZE;
166 break;
167 case LONG:
f13a99db
AM
168 bfd_put_32 (link_info.output_bfd, value,
169 link_order->u.data.contents);
252b5132
RH
170 link_order->size = LONG_SIZE;
171 break;
172 case SHORT:
f13a99db
AM
173 bfd_put_16 (link_info.output_bfd, value,
174 link_order->u.data.contents);
252b5132
RH
175 link_order->size = SHORT_SIZE;
176 break;
177 case BYTE:
f13a99db
AM
178 bfd_put_8 (link_info.output_bfd, value,
179 link_order->u.data.contents);
252b5132
RH
180 link_order->size = BYTE_SIZE;
181 break;
182 default:
183 abort ();
184 }
b7761f11 185 link_order->u.data.size = link_order->size;
252b5132
RH
186 }
187 break;
188
189 case lang_reloc_statement_enum:
190 {
191 lang_reloc_statement_type *rs;
192 asection *output_section;
193 struct bfd_link_order *link_order;
194
195 rs = &statement->reloc_statement;
196
197 output_section = rs->output_section;
f13a99db 198 ASSERT (output_section->owner == link_info.output_bfd);
252b5132 199
2b42b063
AM
200 if (!((output_section->flags & SEC_HAS_CONTENTS) != 0
201 || ((output_section->flags & SEC_LOAD) != 0
202 && (output_section->flags & SEC_THREAD_LOCAL))))
203 break;
204
f13a99db 205 link_order = bfd_new_link_order (link_info.output_bfd, output_section);
252b5132 206 if (link_order == NULL)
df5f2391 207 einfo (_("%F%P: bfd_new_link_order failed\n"));
252b5132 208
7fabd029 209 link_order->offset = rs->output_offset;
252b5132
RH
210 link_order->size = bfd_get_reloc_size (rs->howto);
211
1e9cc1c2 212 link_order->u.reloc.p = (struct bfd_link_order_reloc *)
0aa7f586 213 xmalloc (sizeof (struct bfd_link_order_reloc));
252b5132
RH
214
215 link_order->u.reloc.p->reloc = rs->reloc;
216 link_order->u.reloc.p->addend = rs->addend_value;
217
218 if (rs->name == NULL)
219 {
220 link_order->type = bfd_section_reloc_link_order;
f13a99db 221 if (rs->section->owner == link_info.output_bfd)
252b5132
RH
222 link_order->u.reloc.p->u.section = rs->section;
223 else
224 {
225 link_order->u.reloc.p->u.section = rs->section->output_section;
226 link_order->u.reloc.p->addend += rs->section->output_offset;
227 }
228 }
229 else
230 {
231 link_order->type = bfd_symbol_reloc_link_order;
232 link_order->u.reloc.p->u.name = rs->name;
233 }
234 }
235 break;
236
237 case lang_input_section_enum:
7b986e99
AM
238 {
239 /* Create a new link_order in the output section with this
240 attached */
241 asection *i = statement->input_section.section;
252b5132 242
dbaa2011 243 if (i->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
7b986e99
AM
244 && (i->flags & SEC_EXCLUDE) == 0)
245 {
246 asection *output_section = i->output_section;
2b42b063 247 struct bfd_link_order *link_order;
252b5132 248
f13a99db 249 ASSERT (output_section->owner == link_info.output_bfd);
252b5132 250
2b42b063
AM
251 if (!((output_section->flags & SEC_HAS_CONTENTS) != 0
252 || ((output_section->flags & SEC_LOAD) != 0
253 && (output_section->flags & SEC_THREAD_LOCAL))))
254 break;
252b5132 255
2b42b063
AM
256 link_order = bfd_new_link_order (link_info.output_bfd,
257 output_section);
7b2d2094 258 if (link_order == NULL)
df5f2391 259 einfo (_("%F%P: bfd_new_link_order failed\n"));
7b986e99 260
2b42b063
AM
261 if ((i->flags & SEC_NEVER_LOAD) != 0
262 && (i->flags & SEC_DEBUGGING) == 0)
263 {
264 /* We've got a never load section inside one which is
265 going to be output, we'll change it into a fill. */
266 link_order->type = bfd_data_link_order;
267 link_order->u.data.contents = (unsigned char *) "";
268 link_order->u.data.size = 1;
7b986e99 269 }
2b42b063
AM
270 else
271 {
272 link_order->type = bfd_indirect_link_order;
273 link_order->u.indirect.section = i;
274 ASSERT (i->output_section == output_section);
275 }
276 link_order->size = i->size;
277 link_order->offset = i->output_offset;
7b986e99
AM
278 }
279 }
252b5132
RH
280 break;
281
282 case lang_padding_statement_enum:
283 /* Make a new link_order with the right filler */
284 {
285 asection *output_section;
286 struct bfd_link_order *link_order;
287
288 output_section = statement->padding_statement.output_section;
289 ASSERT (statement->padding_statement.output_section->owner
f13a99db 290 == link_info.output_bfd);
2b42b063
AM
291
292 if (!((output_section->flags & SEC_HAS_CONTENTS) != 0
293 || ((output_section->flags & SEC_LOAD) != 0
294 && (output_section->flags & SEC_THREAD_LOCAL))))
295 break;
296
297 link_order = bfd_new_link_order (link_info.output_bfd,
298 output_section);
7b2d2094 299 if (link_order == NULL)
df5f2391 300 einfo (_("%F%P: bfd_new_link_order failed\n"));
2b42b063
AM
301 link_order->type = bfd_data_link_order;
302 link_order->size = statement->padding_statement.size;
303 link_order->offset = statement->padding_statement.output_offset;
304 link_order->u.data.contents = statement->padding_statement.fill->data;
305 link_order->u.data.size = statement->padding_statement.fill->size;
252b5132
RH
306 }
307 break;
308
309 default:
310 /* All the other ones fall through */
311 break;
312 }
313}
314
29ca8dc5
NS
315/* Return true if NAME is the name of an unsplittable section. These
316 are the stabs strings, dwarf strings. */
317
318static bfd_boolean
319unsplittable_name (const char *name)
320{
0112cd26 321 if (CONST_STRNEQ (name, ".stab"))
29ca8dc5
NS
322 {
323 /* There are several stab like string sections. We pattern match on
324 ".stab...str" */
325 unsigned len = strlen (name);
326 if (strcmp (&name[len-3], "str") == 0)
327 return TRUE;
328 }
329 else if (strcmp (name, "$GDB_STRINGS$") == 0)
330 return TRUE;
331 return FALSE;
332}
333
252b5132
RH
334/* Wander around the input sections, make sure that
335 we'll never try and create an output section with more relocs
336 than will fit.. Do this by always assuming the worst case, and
a854a4a7 337 creating new output sections with all the right bits. */
252b5132
RH
338#define TESTIT 1
339static asection *
1579bae1 340clone_section (bfd *abfd, asection *s, const char *name, int *count)
252b5132 341{
29ca8dc5 342 char *tname;
a854a4a7 343 char *sname;
e4492aa0 344 unsigned int len;
252b5132
RH
345 asection *n;
346 struct bfd_link_hash_entry *h;
252b5132 347
29ca8dc5
NS
348 /* Invent a section name from the section name and a dotted numeric
349 suffix. */
350 len = strlen (name);
1e9cc1c2 351 tname = (char *) xmalloc (len + 1);
29ca8dc5
NS
352 memcpy (tname, name, len + 1);
353 /* Remove a dotted number suffix, from a previous split link. */
354 while (len && ISDIGIT (tname[len-1]))
355 len--;
356 if (len > 1 && tname[len-1] == '.')
357 /* It was a dotted number. */
358 tname[len-1] = 0;
359
360 /* We want to use the whole of the original section name for the
361 split name, but coff can be restricted to 8 character names. */
362 if (bfd_family_coff (abfd) && strlen (tname) > 5)
363 {
364 /* Some section names cannot be truncated, as the name is
365 used to locate some other section. */
0112cd26 366 if (CONST_STRNEQ (name, ".stab")
29ca8dc5
NS
367 || strcmp (name, "$GDB_SYMBOLS$") == 0)
368 {
369 einfo (_ ("%F%P: cannot create split section name for %s\n"), name);
370 /* Silence gcc warnings. einfo exits, so we never reach here. */
371 return NULL;
372 }
373 tname[5] = 0;
374 }
3dbcc61d 375
29ca8dc5 376 if ((sname = bfd_get_unique_section_name (abfd, tname, count)) == NULL
b3ea3584
AM
377 || (n = bfd_make_section_anyway (abfd, sname)) == NULL
378 || (h = bfd_link_hash_lookup (link_info.hash,
b34976b6 379 sname, TRUE, TRUE, FALSE)) == NULL)
e2eb67d9
AM
380 {
381 einfo (_("%F%P: clone section failed: %E\n"));
382 /* Silence gcc warnings. einfo exits, so we never reach here. */
383 return NULL;
384 }
29ca8dc5 385 free (tname);
3dbcc61d 386
b3ea3584 387 /* Set up section symbol. */
252b5132
RH
388 h->type = bfd_link_hash_defined;
389 h->u.def.value = 0;
a854a4a7 390 h->u.def.section = n;
252b5132
RH
391
392 n->flags = s->flags;
393 n->vma = s->vma;
394 n->user_set_vma = s->user_set_vma;
395 n->lma = s->lma;
eea6121a 396 n->size = 0;
252b5132
RH
397 n->output_offset = s->output_offset;
398 n->output_section = n;
399 n->orelocation = 0;
400 n->reloc_count = 0;
401 n->alignment_power = s->alignment_power;
3dbcc61d
NC
402
403 bfd_copy_private_section_data (abfd, s, abfd, n);
404
252b5132
RH
405 return n;
406}
407
408#if TESTING
6d5e62f8 409static void
1579bae1 410ds (asection *s)
252b5132 411{
8423293d 412 struct bfd_link_order *l = s->map_head.link_order;
eea6121a 413 printf ("vma %x size %x\n", s->vma, s->size);
252b5132
RH
414 while (l)
415 {
416 if (l->type == bfd_indirect_link_order)
0aa7f586 417 printf ("%8x %s\n", l->offset, l->u.indirect.section->owner->filename);
252b5132 418 else
0aa7f586 419 printf (_("%8x something else\n"), l->offset);
252b5132
RH
420 l = l->next;
421 }
422 printf ("\n");
423}
6d5e62f8 424
1579bae1 425dump (char *s, asection *a1, asection *a2)
252b5132
RH
426{
427 printf ("%s\n", s);
428 ds (a1);
429 ds (a2);
430}
431
6d5e62f8 432static void
1579bae1 433sanity_check (bfd *abfd)
252b5132
RH
434{
435 asection *s;
436 for (s = abfd->sections; s; s = s->next)
437 {
438 struct bfd_link_order *p;
439 bfd_vma prev = 0;
8423293d 440 for (p = s->map_head.link_order; p; p = p->next)
252b5132
RH
441 {
442 if (p->offset > 100000)
443 abort ();
444 if (p->offset < prev)
445 abort ();
446 prev = p->offset;
447 }
448 }
449}
450#else
451#define sanity_check(a)
452#define dump(a, b, c)
453#endif
454
6d5e62f8 455static void
1579bae1 456split_sections (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
457{
458 asection *original_sec;
459 int nsecs = abfd->section_count;
460 sanity_check (abfd);
a854a4a7 461 /* Look through all the original sections. */
252b5132
RH
462 for (original_sec = abfd->sections;
463 original_sec && nsecs;
464 original_sec = original_sec->next, nsecs--)
465 {
252b5132 466 int count = 0;
a854a4a7
AM
467 unsigned int lines = 0;
468 unsigned int relocs = 0;
469 bfd_size_type sec_size = 0;
470 struct bfd_link_order *l;
471 struct bfd_link_order *p;
252b5132 472 bfd_vma vma = original_sec->vma;
252b5132
RH
473 asection *cursor = original_sec;
474
a854a4a7
AM
475 /* Count up the relocations and line entries to see if anything
476 would be too big to fit. Accumulate section size too. */
8423293d 477 for (l = NULL, p = cursor->map_head.link_order; p != NULL; p = l->next)
252b5132 478 {
a854a4a7
AM
479 unsigned int thislines = 0;
480 unsigned int thisrelocs = 0;
481 bfd_size_type thissize = 0;
252b5132
RH
482 if (p->type == bfd_indirect_link_order)
483 {
484 asection *sec;
485
486 sec = p->u.indirect.section;
487
488 if (info->strip == strip_none
489 || info->strip == strip_some)
490 thislines = sec->lineno_count;
491
0e1862bb 492 if (bfd_link_relocatable (info))
252b5132
RH
493 thisrelocs = sec->reloc_count;
494
eea6121a 495 thissize = sec->size;
a854a4a7 496
252b5132 497 }
0e1862bb 498 else if (bfd_link_relocatable (info)
252b5132
RH
499 && (p->type == bfd_section_reloc_link_order
500 || p->type == bfd_symbol_reloc_link_order))
501 thisrelocs++;
502
a854a4a7
AM
503 if (l != NULL
504 && (thisrelocs + relocs >= config.split_by_reloc
505 || thislines + lines >= config.split_by_reloc
29ca8dc5
NS
506 || (thissize + sec_size >= config.split_by_file))
507 && !unsplittable_name (cursor->name))
252b5132 508 {
a854a4a7
AM
509 /* Create a new section and put this link order and the
510 following link orders into it. */
511 bfd_vma shift_offset;
512 asection *n;
252b5132 513
a854a4a7 514 n = clone_section (abfd, cursor, original_sec->name, &count);
252b5132 515
a854a4a7
AM
516 /* Attach the link orders to the new section and snip
517 them off from the old section. */
8423293d
AM
518 n->map_head.link_order = p;
519 n->map_tail.link_order = cursor->map_tail.link_order;
520 cursor->map_tail.link_order = l;
a854a4a7
AM
521 l->next = NULL;
522 l = p;
252b5132 523
a854a4a7
AM
524 /* Change the size of the original section and
525 update the vma of the new one. */
252b5132 526
a854a4a7 527 dump ("before snip", cursor, n);
252b5132 528
a854a4a7 529 shift_offset = p->offset;
eea6121a
AM
530 n->size = cursor->size - shift_offset;
531 cursor->size = shift_offset;
252b5132 532
a854a4a7
AM
533 vma += shift_offset;
534 n->lma = n->vma = vma;
252b5132 535
a854a4a7
AM
536 /* Run down the chain and change the output section to
537 the right one, update the offsets too. */
538 do
252b5132 539 {
a854a4a7
AM
540 p->offset -= shift_offset;
541 if (p->type == bfd_indirect_link_order)
252b5132 542 {
a854a4a7
AM
543 p->u.indirect.section->output_section = n;
544 p->u.indirect.section->output_offset = p->offset;
252b5132 545 }
a854a4a7 546 p = p->next;
252b5132 547 }
a854a4a7
AM
548 while (p);
549
252b5132
RH
550 dump ("after snip", cursor, n);
551 cursor = n;
552 relocs = thisrelocs;
553 lines = thislines;
a854a4a7 554 sec_size = thissize;
252b5132
RH
555 }
556 else
557 {
a854a4a7 558 l = p;
252b5132
RH
559 relocs += thisrelocs;
560 lines += thislines;
a854a4a7 561 sec_size += thissize;
252b5132 562 }
252b5132
RH
563 }
564 }
565 sanity_check (abfd);
566}
6d5e62f8 567
1579bae1 568/* Call BFD to write out the linked file. */
6d5e62f8 569
252b5132 570void
1579bae1 571ldwrite (void)
252b5132
RH
572{
573 /* Reset error indicator, which can typically something like invalid
a854a4a7 574 format from opening up the .o files. */
252b5132 575 bfd_set_error (bfd_error_no_error);
18cd5bce 576 lang_clear_os_map ();
252b5132
RH
577 lang_for_each_statement (build_link_order);
578
a854a4a7
AM
579 if (config.split_by_reloc != (unsigned) -1
580 || config.split_by_file != (bfd_size_type) -1)
f13a99db
AM
581 split_sections (link_info.output_bfd, &link_info);
582 if (!bfd_final_link (link_info.output_bfd, &link_info))
252b5132
RH
583 {
584 /* If there was an error recorded, print it out. Otherwise assume
585 an appropriate error message like unknown symbol was printed
586 out. */
587
588 if (bfd_get_error () != bfd_error_no_error)
b3ea3584 589 einfo (_("%F%P: final link failed: %E\n"));
252b5132 590 else
6d5e62f8 591 xexit (1);
252b5132
RH
592 }
593}
This page took 0.966965 seconds and 4 git commands to generate.