This commit was generated by cvs2svn to track changes on a CVS vendor
[deliverable/binutils-gdb.git] / ld / ldwrite.c
1 /* ldwrite.c -- write out the linked file
2 Copyright (C) 1993 Free Software Foundation, Inc.
3 Written by Steve Chamberlain sac@cygnus.com
4
5 This file is part of GLD, the Gnu Linker.
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24
25 #include "ld.h"
26 #include "ldexp.h"
27 #include "ldlang.h"
28 #include "ldwrite.h"
29 #include "ldmisc.h"
30 #include "ldgram.h"
31 #include "ldmain.h"
32
33 static void build_link_order PARAMS ((lang_statement_union_type *));
34 static void print_symbol_table PARAMS ((void));
35 static void print_file_stuff PARAMS ((lang_input_statement_type *));
36 static boolean print_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
37
38 extern char *strdup();
39
40 /* Build link_order structures for the BFD linker. */
41
42 static void
43 build_link_order (statement)
44 lang_statement_union_type *statement;
45 {
46 switch (statement->header.type)
47 {
48 case lang_data_statement_enum:
49 {
50 asection *output_section;
51 struct bfd_link_order *link_order;
52 bfd_vma value;
53
54 output_section = statement->data_statement.output_section;
55 ASSERT (output_section->owner == output_bfd);
56
57 link_order = bfd_new_link_order (output_bfd, output_section);
58 if (link_order == NULL)
59 einfo ("%P%F: bfd_new_link_order failed\n");
60
61 link_order->type = bfd_data_link_order;
62 link_order->offset = statement->data_statement.output_vma;
63 link_order->u.data.contents = (bfd_byte *) xmalloc (QUAD_SIZE);
64
65 value = statement->data_statement.value;
66
67 /* If the endianness of the output BFD is not known, then we
68 base the endianness of the data on the first input file.
69 By convention, the bfd_put routines for an unknown
70 endianness are big endian, so we must swap here if the
71 input file is little endian. */
72 if (! bfd_big_endian (output_bfd)
73 && ! bfd_little_endian (output_bfd))
74 {
75 boolean swap;
76
77 swap = false;
78 if (command_line.endian == ENDIAN_LITTLE)
79 swap = true;
80 else if (command_line.endian == ENDIAN_UNSET)
81 {
82 LANG_FOR_EACH_INPUT_STATEMENT (s)
83 {
84 if (s->the_bfd != NULL)
85 {
86 if (bfd_little_endian (s->the_bfd))
87 swap = true;
88 break;
89 }
90 }
91 }
92
93 if (swap)
94 {
95 bfd_byte buffer[8];
96
97 switch (statement->data_statement.type)
98 {
99 case QUAD:
100 bfd_putl64 (value, buffer);
101 value = bfd_getb64 (buffer);
102 break;
103 case LONG:
104 bfd_putl32 (value, buffer);
105 value = bfd_getb32 (buffer);
106 break;
107 case SHORT:
108 bfd_putl16 (value, buffer);
109 value = bfd_getb16 (buffer);
110 break;
111 case BYTE:
112 break;
113 default:
114 abort ();
115 }
116 }
117 }
118
119 ASSERT (output_section->owner == output_bfd);
120 switch (statement->data_statement.type)
121 {
122 case QUAD:
123 bfd_put_64 (output_bfd, value, link_order->u.data.contents);
124 link_order->size = QUAD_SIZE;
125 break;
126 case LONG:
127 bfd_put_32 (output_bfd, value, link_order->u.data.contents);
128 link_order->size = LONG_SIZE;
129 break;
130 case SHORT:
131 bfd_put_16 (output_bfd, value, link_order->u.data.contents);
132 link_order->size = SHORT_SIZE;
133 break;
134 case BYTE:
135 bfd_put_8 (output_bfd, value, link_order->u.data.contents);
136 link_order->size = BYTE_SIZE;
137 break;
138 default:
139 abort ();
140 }
141 }
142 break;
143
144 case lang_reloc_statement_enum:
145 {
146 lang_reloc_statement_type *rs;
147 asection *output_section;
148 struct bfd_link_order *link_order;
149
150 rs = &statement->reloc_statement;
151
152 output_section = rs->output_section;
153 ASSERT (output_section->owner == output_bfd);
154
155 link_order = bfd_new_link_order (output_bfd, output_section);
156 if (link_order == NULL)
157 einfo ("%P%F: bfd_new_link_order failed\n");
158
159 link_order->offset = rs->output_vma;
160 link_order->size = bfd_get_reloc_size (rs->howto);
161
162 link_order->u.reloc.p =
163 ((struct bfd_link_order_reloc *)
164 xmalloc (sizeof (struct bfd_link_order_reloc)));
165
166 link_order->u.reloc.p->reloc = rs->reloc;
167 link_order->u.reloc.p->addend = rs->addend_value;
168
169 if (rs->name == NULL)
170 {
171 link_order->type = bfd_section_reloc_link_order;
172 if (rs->section->owner == output_bfd)
173 link_order->u.reloc.p->u.section = rs->section;
174 else
175 {
176 link_order->u.reloc.p->u.section = rs->section->output_section;
177 link_order->u.reloc.p->addend += rs->section->output_offset;
178 }
179 }
180 else
181 {
182 link_order->type = bfd_symbol_reloc_link_order;
183 link_order->u.reloc.p->u.name = rs->name;
184 }
185 }
186 break;
187
188 case lang_input_section_enum:
189 /* Create a new link_order in the output section with this
190 attached */
191 if (statement->input_section.ifile->just_syms_flag == false)
192 {
193 asection *i = statement->input_section.section;
194 asection *output_section = i->output_section;
195
196 ASSERT (output_section->owner == output_bfd);
197
198 if ((output_section->flags & SEC_HAS_CONTENTS) != 0)
199 {
200 struct bfd_link_order *link_order;
201
202 link_order = bfd_new_link_order (output_bfd, output_section);
203
204 if (i->flags & SEC_NEVER_LOAD)
205 {
206 /* We've got a never load section inside one which
207 is going to be output, we'll change it into a
208 fill link_order */
209 link_order->type = bfd_fill_link_order;
210 link_order->u.fill.value = 0;
211 }
212 else
213 {
214 link_order->type = bfd_indirect_link_order;
215 link_order->u.indirect.section = i;
216 ASSERT (i->output_section == output_section);
217 }
218 if (i->_cooked_size)
219 link_order->size = i->_cooked_size;
220 else
221 link_order->size = bfd_get_section_size_before_reloc (i);
222 link_order->offset = i->output_offset;
223 }
224 }
225 break;
226
227 case lang_padding_statement_enum:
228 /* Make a new link_order with the right filler */
229 {
230 asection *output_section;
231 struct bfd_link_order *link_order;
232
233 output_section = statement->padding_statement.output_section;
234 ASSERT (statement->padding_statement.output_section->owner
235 == output_bfd);
236 if ((output_section->flags & SEC_HAS_CONTENTS) != 0)
237 {
238 link_order = bfd_new_link_order (output_bfd, output_section);
239 link_order->type = bfd_fill_link_order;
240 link_order->size = statement->padding_statement.size;
241 link_order->offset = statement->padding_statement.output_offset;
242 link_order->u.fill.value = statement->padding_statement.fill;
243 }
244 }
245 break;
246
247 default:
248 /* All the other ones fall through */
249 break;
250 }
251 }
252
253 /* Call BFD to write out the linked file. */
254
255
256 /**********************************************************************/
257
258
259 /* Wander around the input sections, make sure that
260 we'll never try and create an output section with more relocs
261 than will fit.. Do this by always assuming the worst case, and
262 creating new output sections with all the right bits */
263 #define TESTIT 1
264 static asection *
265 clone_section (abfd, s, count)
266 bfd *abfd;
267 asection *s;
268 int *count;
269 {
270 #define SSIZE 8
271 char sname[SSIZE]; /* ?? find the name for this size */
272 asection *n;
273 struct bfd_link_hash_entry *h;
274 /* Invent a section name - use first five
275 chars of base section name and a digit suffix */
276 do
277 {
278 unsigned int i;
279 char b[6];
280 for (i = 0; i < sizeof (b) - 1 && s->name[i]; i++)
281 b[i] = s->name[i];
282 b[i] = 0;
283 sprintf (sname, "%s%d", b, (*count)++);
284 }
285 while (bfd_get_section_by_name (abfd, sname));
286
287 n = bfd_make_section_anyway (abfd, strdup (sname));
288
289 /* Create a symbol of the same name */
290
291 h = bfd_link_hash_lookup (link_info.hash,
292 sname, true, true, false);
293 h->type = bfd_link_hash_defined;
294 h->u.def.value = 0;
295 h->u.def.section = n ;
296
297
298 n->flags = s->flags;
299 n->vma = s->vma;
300 n->user_set_vma = s->user_set_vma;
301 n->lma = s->lma;
302 n->_cooked_size = 0;
303 n->_raw_size = 0;
304 n->output_offset = s->output_offset;
305 n->output_section = n;
306 n->orelocation = 0;
307 n->reloc_count = 0;
308 n->alignment_power = s->alignment_power;
309 return n;
310 }
311
312 #if TESTING
313 static void
314 ds (s)
315 asection *s;
316 {
317 struct bfd_link_order *l = s->link_order_head;
318 printf ("vma %x size %x\n", s->vma, s->_raw_size);
319 while (l)
320 {
321 if (l->type == bfd_indirect_link_order)
322 {
323 printf ("%8x %s\n", l->offset, l->u.indirect.section->owner->filename);
324 }
325 else
326 {
327 printf ("%8x something else\n", l->offset);
328 }
329 l = l->next;
330 }
331 printf ("\n");
332 }
333 dump (s, a1, a2)
334 char *s;
335 asection *a1;
336 asection *a2;
337 {
338 printf ("%s\n", s);
339 ds (a1);
340 ds (a2);
341 }
342
343 static void
344 sanity_check (abfd)
345 bfd *abfd;
346 {
347 asection *s;
348 for (s = abfd->sections; s; s = s->next)
349 {
350 struct bfd_link_order *p;
351 bfd_vma prev = 0;
352 for (p = s->link_order_head; p; p = p->next)
353 {
354 if (p->offset > 100000)
355 abort ();
356 if (p->offset < prev)
357 abort ();
358 prev = p->offset;
359 }
360 }
361 }
362 #else
363 #define sanity_check(a)
364 #define dump(a, b, c)
365 #endif
366
367
368 void
369 split_sections (abfd, info)
370 bfd *abfd;
371 struct bfd_link_info *info;
372 {
373 asection *original_sec;
374 int nsecs = abfd->section_count;
375 sanity_check (abfd);
376 /* look through all the original sections */
377 for (original_sec = abfd->sections;
378 original_sec && nsecs;
379 original_sec = original_sec->next, nsecs--)
380 {
381 boolean first = true;
382 int count = 0;
383 int lines = 0;
384 int relocs = 0;
385 struct bfd_link_order **pp;
386 bfd_vma vma = original_sec->vma;
387 bfd_vma shift_offset = 0;
388 asection *cursor = original_sec;
389
390 /* count up the relocations and line entries to see if
391 anything would be too big to fit */
392 for (pp = &(cursor->link_order_head); *pp; pp = &((*pp)->next))
393 {
394 struct bfd_link_order *p = *pp;
395 int thislines = 0;
396 int thisrelocs = 0;
397 if (p->type == bfd_indirect_link_order)
398 {
399 asection *sec;
400
401 sec = p->u.indirect.section;
402
403 if (info->strip == strip_none
404 || info->strip == strip_some)
405 thislines = sec->lineno_count;
406
407 if (info->relocateable)
408 thisrelocs = sec->reloc_count;
409
410 }
411 else if (info->relocateable
412 && (p->type == bfd_section_reloc_link_order
413 || p->type == bfd_symbol_reloc_link_order))
414 thisrelocs++;
415
416 if (! first
417 && (thisrelocs + relocs > config.split_by_reloc
418 || thislines + lines > config.split_by_reloc
419 || config.split_by_file))
420 {
421 /* create a new section and put this link order and the
422 following link orders into it */
423 struct bfd_link_order *l = p;
424 asection *n = clone_section (abfd, cursor, &count);
425 *pp = NULL; /* Snip off link orders from old section */
426 n->link_order_head = l; /* attach to new section */
427 pp = &n->link_order_head;
428
429 /* change the size of the original section and
430 update the vma of the new one */
431
432 dump ("before snip", cursor, n);
433
434 n->_raw_size = cursor->_raw_size - l->offset;
435 cursor->_raw_size = l->offset;
436
437 vma += cursor->_raw_size;
438 n->lma = n->vma = vma;
439
440 shift_offset = l->offset;
441
442 /* run down the chain and change the output section to
443 the right one, update the offsets too */
444
445 while (l)
446 {
447 l->offset -= shift_offset;
448 if (l->type == bfd_indirect_link_order)
449 {
450 l->u.indirect.section->output_section = n;
451 l->u.indirect.section->output_offset = l->offset;
452 }
453 l = l->next;
454 }
455 dump ("after snip", cursor, n);
456 cursor = n;
457 relocs = thisrelocs;
458 lines = thislines;
459 }
460 else
461 {
462 relocs += thisrelocs;
463 lines += thislines;
464 }
465
466 first = false;
467 }
468 }
469 sanity_check (abfd);
470 }
471 /**********************************************************************/
472 void
473 ldwrite ()
474 {
475 /* Reset error indicator, which can typically something like invalid
476 format from openning up the .o files */
477 bfd_set_error (bfd_error_no_error);
478 lang_for_each_statement (build_link_order);
479
480 if (config.split_by_reloc || config.split_by_file)
481 split_sections (output_bfd, &link_info);
482 if (!bfd_final_link (output_bfd, &link_info))
483 {
484 /* If there was an error recorded, print it out. Otherwise assume
485 an appropriate error message like unknown symbol was printed
486 out. */
487
488 if (bfd_get_error () != bfd_error_no_error)
489 einfo ("%F%P: final link failed: %E\n", output_bfd);
490 else
491 xexit(1);
492 }
493
494 if (config.map_file)
495 {
496 print_symbol_table ();
497 lang_map ();
498 }
499 }
500
501 /* Print the symbol table. */
502
503 static void
504 print_symbol_table ()
505 {
506 fprintf (config.map_file, "\n**FILES**\n\n");
507 lang_for_each_file (print_file_stuff);
508
509 fprintf (config.map_file, "**GLOBAL SYMBOLS**\n\n");
510 fprintf (config.map_file, "offset section offset symbol\n");
511 bfd_link_hash_traverse (link_info.hash, print_symbol, (PTR) NULL);
512 }
513
514 /* Print information about a file. */
515
516 static void
517 print_file_stuff (f)
518 lang_input_statement_type *f;
519 {
520 fprintf (config.map_file, " %s\n", f->filename);
521 if (f->just_syms_flag)
522 {
523 fprintf (config.map_file, " symbols only\n");
524 }
525 else
526 {
527 asection *s;
528 if (true)
529 {
530 for (s = f->the_bfd->sections;
531 s != (asection *) NULL;
532 s = s->next)
533 {
534 #ifdef WINDOWS_NT
535 /* Don't include any information that goes into the '.junk'
536 section. This includes the code view .debug$ data and
537 stuff from .drectve sections */
538 if (strcmp (s->name, ".drectve") == 0 ||
539 strncmp (s->name, ".debug$", 7) == 0)
540 continue;
541 #endif
542 print_address (s->output_offset);
543 if (s->reloc_done)
544 {
545 fprintf (config.map_file, " %08x 2**%2ud %s\n",
546 (unsigned) bfd_get_section_size_after_reloc (s),
547 s->alignment_power, s->name);
548 }
549
550 else
551 {
552 fprintf (config.map_file, " %08x 2**%2ud %s\n",
553 (unsigned) bfd_get_section_size_before_reloc (s),
554 s->alignment_power, s->name);
555 }
556 }
557 }
558 else
559 {
560 for (s = f->the_bfd->sections;
561 s != (asection *) NULL;
562 s = s->next)
563 {
564 fprintf (config.map_file, "%s ", s->name);
565 print_address (s->output_offset);
566 fprintf (config.map_file, "(%x)",
567 (unsigned) bfd_get_section_size_after_reloc (s));
568 }
569 fprintf (config.map_file, "hex \n");
570 }
571 }
572 print_nl ();
573 }
574
575 /* Print a symbol. */
576
577 /*ARGSUSED*/
578 static boolean
579 print_symbol (p, ignore)
580 struct bfd_link_hash_entry *p;
581 PTR ignore;
582 {
583 while (p->type == bfd_link_hash_indirect
584 || p->type == bfd_link_hash_warning)
585 p = p->u.i.link;
586
587 switch (p->type)
588 {
589 case bfd_link_hash_new:
590 abort ();
591
592 case bfd_link_hash_undefined:
593 fprintf (config.map_file, "undefined ");
594 fprintf (config.map_file, "%s ", p->root.string);
595 print_nl ();
596 break;
597
598 case bfd_link_hash_undefweak:
599 fprintf (config.map_file, "weak ");
600 fprintf (config.map_file, "%s ", p->root.string);
601 print_nl ();
602 break;
603
604 case bfd_link_hash_defined:
605 case bfd_link_hash_defweak:
606 {
607 asection *defsec = p->u.def.section;
608
609 print_address (p->u.def.value);
610 if (defsec)
611 {
612 fprintf (config.map_file, " %-10s",
613 bfd_section_name (output_bfd, defsec));
614 print_space ();
615 print_address (p->u.def.value + defsec->vma);
616 }
617 else
618 {
619 fprintf (config.map_file, " .......");
620 }
621 fprintf (config.map_file, " %s", p->root.string);
622 if (p->type == bfd_link_hash_defweak)
623 fprintf (config.map_file, " [weak]");
624 }
625 print_nl ();
626 break;
627
628 case bfd_link_hash_common:
629 fprintf (config.map_file, "common ");
630 print_address (p->u.c.size);
631 fprintf (config.map_file, " %s ", p->root.string);
632 print_nl ();
633 break;
634
635 default:
636 abort ();
637 }
638
639 return true;
640 }
This page took 0.041158 seconds and 5 git commands to generate.