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