1 /* ldwrite.c -- write out the linked file
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain sac@cygnus.com
6 This file is part of GLD, the Gnu Linker.
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 2 of the License, or
11 (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "libiberty.h"
26 #include "safe-ctype.h"
36 /* Build link_order structures for the BFD linker. */
39 build_link_order (lang_statement_union_type
*statement
)
41 switch (statement
->header
.type
)
43 case lang_data_statement_enum
:
45 asection
*output_section
;
46 struct bfd_link_order
*link_order
;
48 bfd_boolean big_endian
= FALSE
;
50 output_section
= statement
->data_statement
.output_section
;
51 ASSERT (output_section
->owner
== output_bfd
);
53 link_order
= bfd_new_link_order (output_bfd
, output_section
);
54 if (link_order
== NULL
)
55 einfo (_("%P%F: bfd_new_link_order failed\n"));
57 link_order
->type
= bfd_data_link_order
;
58 link_order
->offset
= statement
->data_statement
.output_vma
;
59 link_order
->u
.data
.contents
= xmalloc (QUAD_SIZE
);
61 value
= statement
->data_statement
.value
;
63 /* If the endianness of the output BFD is not known, then we
64 base the endianness of the data on the first input file.
65 By convention, the bfd_put routines for an unknown
66 endianness are big endian, so we must swap here if the
67 input file is little endian. */
68 if (bfd_big_endian (output_bfd
))
70 else if (bfd_little_endian (output_bfd
))
77 if (command_line
.endian
== ENDIAN_BIG
)
79 else if (command_line
.endian
== ENDIAN_LITTLE
)
84 else if (command_line
.endian
== ENDIAN_UNSET
)
88 LANG_FOR_EACH_INPUT_STATEMENT (s
)
90 if (s
->the_bfd
!= NULL
)
92 if (bfd_little_endian (s
->the_bfd
))
107 switch (statement
->data_statement
.type
)
111 if (sizeof (bfd_vma
) >= QUAD_SIZE
)
113 bfd_putl64 (value
, buffer
);
114 value
= bfd_getb64 (buffer
);
119 bfd_putl32 (value
, buffer
);
120 value
= bfd_getb32 (buffer
);
123 bfd_putl16 (value
, buffer
);
124 value
= bfd_getb16 (buffer
);
134 ASSERT (output_section
->owner
== output_bfd
);
135 switch (statement
->data_statement
.type
)
139 if (sizeof (bfd_vma
) >= QUAD_SIZE
)
140 bfd_put_64 (output_bfd
, value
, link_order
->u
.data
.contents
);
145 if (statement
->data_statement
.type
== QUAD
)
147 else if ((value
& 0x80000000) == 0)
151 bfd_put_32 (output_bfd
, high
,
152 (link_order
->u
.data
.contents
153 + (big_endian
? 0 : 4)));
154 bfd_put_32 (output_bfd
, value
,
155 (link_order
->u
.data
.contents
156 + (big_endian
? 4 : 0)));
158 link_order
->size
= QUAD_SIZE
;
161 bfd_put_32 (output_bfd
, value
, link_order
->u
.data
.contents
);
162 link_order
->size
= LONG_SIZE
;
165 bfd_put_16 (output_bfd
, value
, link_order
->u
.data
.contents
);
166 link_order
->size
= SHORT_SIZE
;
169 bfd_put_8 (output_bfd
, value
, link_order
->u
.data
.contents
);
170 link_order
->size
= BYTE_SIZE
;
178 case lang_reloc_statement_enum
:
180 lang_reloc_statement_type
*rs
;
181 asection
*output_section
;
182 struct bfd_link_order
*link_order
;
184 rs
= &statement
->reloc_statement
;
186 output_section
= rs
->output_section
;
187 ASSERT (output_section
->owner
== output_bfd
);
189 link_order
= bfd_new_link_order (output_bfd
, output_section
);
190 if (link_order
== NULL
)
191 einfo (_("%P%F: bfd_new_link_order failed\n"));
193 link_order
->offset
= rs
->output_vma
;
194 link_order
->size
= bfd_get_reloc_size (rs
->howto
);
196 link_order
->u
.reloc
.p
= xmalloc (sizeof (struct bfd_link_order_reloc
));
198 link_order
->u
.reloc
.p
->reloc
= rs
->reloc
;
199 link_order
->u
.reloc
.p
->addend
= rs
->addend_value
;
201 if (rs
->name
== NULL
)
203 link_order
->type
= bfd_section_reloc_link_order
;
204 if (rs
->section
->owner
== output_bfd
)
205 link_order
->u
.reloc
.p
->u
.section
= rs
->section
;
208 link_order
->u
.reloc
.p
->u
.section
= rs
->section
->output_section
;
209 link_order
->u
.reloc
.p
->addend
+= rs
->section
->output_offset
;
214 link_order
->type
= bfd_symbol_reloc_link_order
;
215 link_order
->u
.reloc
.p
->u
.name
= rs
->name
;
220 case lang_input_section_enum
:
221 /* Create a new link_order in the output section with this
223 if (!statement
->input_section
.ifile
->just_syms_flag
)
225 asection
*i
= statement
->input_section
.section
;
226 asection
*output_section
= i
->output_section
;
228 ASSERT (output_section
->owner
== output_bfd
);
230 if ((output_section
->flags
& SEC_HAS_CONTENTS
) != 0
231 || ((output_section
->flags
& SEC_LOAD
) != 0
232 && (output_section
->flags
& SEC_THREAD_LOCAL
)))
234 struct bfd_link_order
*link_order
;
236 link_order
= bfd_new_link_order (output_bfd
, output_section
);
238 if (i
->flags
& SEC_NEVER_LOAD
)
240 /* We've got a never load section inside one which
241 is going to be output, we'll change it into a
243 link_order
->type
= bfd_data_link_order
;
244 link_order
->u
.data
.contents
= "";
245 link_order
->u
.data
.size
= 1;
249 link_order
->type
= bfd_indirect_link_order
;
250 link_order
->u
.indirect
.section
= i
;
251 ASSERT (i
->output_section
== output_section
);
254 link_order
->size
= i
->_cooked_size
;
256 link_order
->size
= bfd_get_section_size_before_reloc (i
);
257 link_order
->offset
= i
->output_offset
;
262 case lang_padding_statement_enum
:
263 /* Make a new link_order with the right filler */
265 asection
*output_section
;
266 struct bfd_link_order
*link_order
;
268 output_section
= statement
->padding_statement
.output_section
;
269 ASSERT (statement
->padding_statement
.output_section
->owner
271 if ((output_section
->flags
& SEC_HAS_CONTENTS
) != 0)
273 link_order
= bfd_new_link_order (output_bfd
, output_section
);
274 link_order
->type
= bfd_data_link_order
;
275 link_order
->size
= statement
->padding_statement
.size
;
276 link_order
->offset
= statement
->padding_statement
.output_offset
;
277 link_order
->u
.data
.contents
= statement
->padding_statement
.fill
->data
;
278 link_order
->u
.data
.size
= statement
->padding_statement
.fill
->size
;
284 /* All the other ones fall through */
289 /* Return true if NAME is the name of an unsplittable section. These
290 are the stabs strings, dwarf strings. */
293 unsplittable_name (const char *name
)
295 if (strncmp (name
, ".stab", 5) == 0)
297 /* There are several stab like string sections. We pattern match on
299 unsigned len
= strlen (name
);
300 if (strcmp (&name
[len
-3], "str") == 0)
303 else if (strcmp (name
, "$GDB_STRINGS$") == 0)
308 /* Wander around the input sections, make sure that
309 we'll never try and create an output section with more relocs
310 than will fit.. Do this by always assuming the worst case, and
311 creating new output sections with all the right bits. */
314 clone_section (bfd
*abfd
, asection
*s
, const char *name
, int *count
)
320 struct bfd_link_hash_entry
*h
;
322 /* Invent a section name from the section name and a dotted numeric
325 tname
= xmalloc (len
+ 1);
326 memcpy (tname
, name
, len
+ 1);
327 /* Remove a dotted number suffix, from a previous split link. */
328 while (len
&& ISDIGIT (tname
[len
-1]))
330 if (len
> 1 && tname
[len
-1] == '.')
331 /* It was a dotted number. */
334 /* We want to use the whole of the original section name for the
335 split name, but coff can be restricted to 8 character names. */
336 if (bfd_family_coff (abfd
) && strlen (tname
) > 5)
338 /* Some section names cannot be truncated, as the name is
339 used to locate some other section. */
340 if (strncmp (name
, ".stab", 5) == 0
341 || strcmp (name
, "$GDB_SYMBOLS$") == 0)
343 einfo (_ ("%F%P: cannot create split section name for %s\n"), name
);
344 /* Silence gcc warnings. einfo exits, so we never reach here. */
350 if ((sname
= bfd_get_unique_section_name (abfd
, tname
, count
)) == NULL
351 || (n
= bfd_make_section_anyway (abfd
, sname
)) == NULL
352 || (h
= bfd_link_hash_lookup (link_info
.hash
,
353 sname
, TRUE
, TRUE
, FALSE
)) == NULL
)
355 einfo (_("%F%P: clone section failed: %E\n"));
356 /* Silence gcc warnings. einfo exits, so we never reach here. */
361 /* Set up section symbol. */
362 h
->type
= bfd_link_hash_defined
;
364 h
->u
.def
.section
= n
;
368 n
->user_set_vma
= s
->user_set_vma
;
372 n
->output_offset
= s
->output_offset
;
373 n
->output_section
= n
;
376 n
->alignment_power
= s
->alignment_power
;
384 struct bfd_link_order
*l
= s
->link_order_head
;
385 printf ("vma %x size %x\n", s
->vma
, s
->_raw_size
);
388 if (l
->type
== bfd_indirect_link_order
)
390 printf ("%8x %s\n", l
->offset
, l
->u
.indirect
.section
->owner
->filename
);
394 printf (_("%8x something else\n"), l
->offset
);
401 dump (char *s
, asection
*a1
, asection
*a2
)
409 sanity_check (bfd
*abfd
)
412 for (s
= abfd
->sections
; s
; s
= s
->next
)
414 struct bfd_link_order
*p
;
416 for (p
= s
->link_order_head
; p
; p
= p
->next
)
418 if (p
->offset
> 100000)
420 if (p
->offset
< prev
)
427 #define sanity_check(a)
428 #define dump(a, b, c)
432 split_sections (bfd
*abfd
, struct bfd_link_info
*info
)
434 asection
*original_sec
;
435 int nsecs
= abfd
->section_count
;
437 /* Look through all the original sections. */
438 for (original_sec
= abfd
->sections
;
439 original_sec
&& nsecs
;
440 original_sec
= original_sec
->next
, nsecs
--)
443 unsigned int lines
= 0;
444 unsigned int relocs
= 0;
445 bfd_size_type sec_size
= 0;
446 struct bfd_link_order
*l
;
447 struct bfd_link_order
*p
;
448 bfd_vma vma
= original_sec
->vma
;
449 asection
*cursor
= original_sec
;
451 /* Count up the relocations and line entries to see if anything
452 would be too big to fit. Accumulate section size too. */
453 for (l
= NULL
, p
= cursor
->link_order_head
; p
!= NULL
; p
= l
->next
)
455 unsigned int thislines
= 0;
456 unsigned int thisrelocs
= 0;
457 bfd_size_type thissize
= 0;
458 if (p
->type
== bfd_indirect_link_order
)
462 sec
= p
->u
.indirect
.section
;
464 if (info
->strip
== strip_none
465 || info
->strip
== strip_some
)
466 thislines
= sec
->lineno_count
;
468 if (info
->relocatable
)
469 thisrelocs
= sec
->reloc_count
;
471 if (sec
->_cooked_size
!= 0)
472 thissize
= sec
->_cooked_size
;
474 thissize
= sec
->_raw_size
;
477 else if (info
->relocatable
478 && (p
->type
== bfd_section_reloc_link_order
479 || p
->type
== bfd_symbol_reloc_link_order
))
483 && (thisrelocs
+ relocs
>= config
.split_by_reloc
484 || thislines
+ lines
>= config
.split_by_reloc
485 || (thissize
+ sec_size
>= config
.split_by_file
))
486 && !unsplittable_name (cursor
->name
))
488 /* Create a new section and put this link order and the
489 following link orders into it. */
490 bfd_vma shift_offset
;
493 n
= clone_section (abfd
, cursor
, original_sec
->name
, &count
);
495 /* Attach the link orders to the new section and snip
496 them off from the old section. */
497 n
->link_order_head
= p
;
498 n
->link_order_tail
= cursor
->link_order_tail
;
499 cursor
->link_order_tail
= l
;
503 /* Change the size of the original section and
504 update the vma of the new one. */
506 dump ("before snip", cursor
, n
);
508 shift_offset
= p
->offset
;
509 if (cursor
->_cooked_size
!= 0)
511 n
->_cooked_size
= cursor
->_cooked_size
- shift_offset
;
512 cursor
->_cooked_size
= shift_offset
;
514 n
->_raw_size
= cursor
->_raw_size
- shift_offset
;
515 cursor
->_raw_size
= shift_offset
;
518 n
->lma
= n
->vma
= vma
;
520 /* Run down the chain and change the output section to
521 the right one, update the offsets too. */
524 p
->offset
-= shift_offset
;
525 if (p
->type
== bfd_indirect_link_order
)
527 p
->u
.indirect
.section
->output_section
= n
;
528 p
->u
.indirect
.section
->output_offset
= p
->offset
;
534 dump ("after snip", cursor
, n
);
543 relocs
+= thisrelocs
;
545 sec_size
+= thissize
;
552 /* Call BFD to write out the linked file. */
557 /* Reset error indicator, which can typically something like invalid
558 format from opening up the .o files. */
559 bfd_set_error (bfd_error_no_error
);
560 lang_for_each_statement (build_link_order
);
562 if (config
.split_by_reloc
!= (unsigned) -1
563 || config
.split_by_file
!= (bfd_size_type
) -1)
564 split_sections (output_bfd
, &link_info
);
565 if (!bfd_final_link (output_bfd
, &link_info
))
567 /* If there was an error recorded, print it out. Otherwise assume
568 an appropriate error message like unknown symbol was printed
571 if (bfd_get_error () != bfd_error_no_error
)
572 einfo (_("%F%P: final link failed: %E\n"));