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