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