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"
35 /* Build link_order structures for the BFD linker. */
38 build_link_order (lang_statement_union_type
*statement
)
40 switch (statement
->header
.type
)
42 case lang_data_statement_enum
:
44 asection
*output_section
;
45 struct bfd_link_order
*link_order
;
47 bfd_boolean big_endian
= FALSE
;
49 output_section
= statement
->data_statement
.output_section
;
50 ASSERT (output_section
->owner
== output_bfd
);
52 link_order
= bfd_new_link_order (output_bfd
, output_section
);
53 if (link_order
== NULL
)
54 einfo (_("%P%F: bfd_new_link_order failed\n"));
56 link_order
->type
= bfd_data_link_order
;
57 link_order
->offset
= statement
->data_statement
.output_vma
;
58 link_order
->u
.data
.contents
= xmalloc (QUAD_SIZE
);
60 value
= statement
->data_statement
.value
;
62 /* If the endianness of the output BFD is not known, then we
63 base the endianness of the data on the first input file.
64 By convention, the bfd_put routines for an unknown
65 endianness are big endian, so we must swap here if the
66 input file is little endian. */
67 if (bfd_big_endian (output_bfd
))
69 else if (bfd_little_endian (output_bfd
))
76 if (command_line
.endian
== ENDIAN_BIG
)
78 else if (command_line
.endian
== ENDIAN_LITTLE
)
83 else if (command_line
.endian
== ENDIAN_UNSET
)
87 LANG_FOR_EACH_INPUT_STATEMENT (s
)
89 if (s
->the_bfd
!= NULL
)
91 if (bfd_little_endian (s
->the_bfd
))
106 switch (statement
->data_statement
.type
)
110 if (sizeof (bfd_vma
) >= QUAD_SIZE
)
112 bfd_putl64 (value
, buffer
);
113 value
= bfd_getb64 (buffer
);
118 bfd_putl32 (value
, buffer
);
119 value
= bfd_getb32 (buffer
);
122 bfd_putl16 (value
, buffer
);
123 value
= bfd_getb16 (buffer
);
133 ASSERT (output_section
->owner
== output_bfd
);
134 switch (statement
->data_statement
.type
)
138 if (sizeof (bfd_vma
) >= QUAD_SIZE
)
139 bfd_put_64 (output_bfd
, value
, link_order
->u
.data
.contents
);
144 if (statement
->data_statement
.type
== QUAD
)
146 else if ((value
& 0x80000000) == 0)
150 bfd_put_32 (output_bfd
, high
,
151 (link_order
->u
.data
.contents
152 + (big_endian
? 0 : 4)));
153 bfd_put_32 (output_bfd
, value
,
154 (link_order
->u
.data
.contents
155 + (big_endian
? 4 : 0)));
157 link_order
->size
= QUAD_SIZE
;
160 bfd_put_32 (output_bfd
, value
, link_order
->u
.data
.contents
);
161 link_order
->size
= LONG_SIZE
;
164 bfd_put_16 (output_bfd
, value
, link_order
->u
.data
.contents
);
165 link_order
->size
= SHORT_SIZE
;
168 bfd_put_8 (output_bfd
, value
, link_order
->u
.data
.contents
);
169 link_order
->size
= BYTE_SIZE
;
177 case lang_reloc_statement_enum
:
179 lang_reloc_statement_type
*rs
;
180 asection
*output_section
;
181 struct bfd_link_order
*link_order
;
183 rs
= &statement
->reloc_statement
;
185 output_section
= rs
->output_section
;
186 ASSERT (output_section
->owner
== output_bfd
);
188 link_order
= bfd_new_link_order (output_bfd
, output_section
);
189 if (link_order
== NULL
)
190 einfo (_("%P%F: bfd_new_link_order failed\n"));
192 link_order
->offset
= rs
->output_vma
;
193 link_order
->size
= bfd_get_reloc_size (rs
->howto
);
195 link_order
->u
.reloc
.p
= xmalloc (sizeof (struct bfd_link_order_reloc
));
197 link_order
->u
.reloc
.p
->reloc
= rs
->reloc
;
198 link_order
->u
.reloc
.p
->addend
= rs
->addend_value
;
200 if (rs
->name
== NULL
)
202 link_order
->type
= bfd_section_reloc_link_order
;
203 if (rs
->section
->owner
== output_bfd
)
204 link_order
->u
.reloc
.p
->u
.section
= rs
->section
;
207 link_order
->u
.reloc
.p
->u
.section
= rs
->section
->output_section
;
208 link_order
->u
.reloc
.p
->addend
+= rs
->section
->output_offset
;
213 link_order
->type
= bfd_symbol_reloc_link_order
;
214 link_order
->u
.reloc
.p
->u
.name
= rs
->name
;
219 case lang_input_section_enum
:
220 /* Create a new link_order in the output section with this
222 if (!statement
->input_section
.ifile
->just_syms_flag
)
224 asection
*i
= statement
->input_section
.section
;
225 asection
*output_section
= i
->output_section
;
227 ASSERT (output_section
->owner
== output_bfd
);
229 if ((output_section
->flags
& SEC_HAS_CONTENTS
) != 0
230 || ((output_section
->flags
& SEC_LOAD
) != 0
231 && (output_section
->flags
& SEC_THREAD_LOCAL
)))
233 struct bfd_link_order
*link_order
;
235 link_order
= bfd_new_link_order (output_bfd
, output_section
);
237 if (i
->flags
& SEC_NEVER_LOAD
)
239 /* We've got a never load section inside one which
240 is going to be output, we'll change it into a
242 link_order
->type
= bfd_data_link_order
;
243 link_order
->u
.data
.contents
= "";
244 link_order
->u
.data
.size
= 1;
248 link_order
->type
= bfd_indirect_link_order
;
249 link_order
->u
.indirect
.section
= i
;
250 ASSERT (i
->output_section
== output_section
);
253 link_order
->size
= i
->_cooked_size
;
255 link_order
->size
= bfd_get_section_size_before_reloc (i
);
256 link_order
->offset
= i
->output_offset
;
261 case lang_padding_statement_enum
:
262 /* Make a new link_order with the right filler */
264 asection
*output_section
;
265 struct bfd_link_order
*link_order
;
267 output_section
= statement
->padding_statement
.output_section
;
268 ASSERT (statement
->padding_statement
.output_section
->owner
270 if ((output_section
->flags
& SEC_HAS_CONTENTS
) != 0)
272 link_order
= bfd_new_link_order (output_bfd
, output_section
);
273 link_order
->type
= bfd_data_link_order
;
274 link_order
->size
= statement
->padding_statement
.size
;
275 link_order
->offset
= statement
->padding_statement
.output_offset
;
276 link_order
->u
.data
.contents
= statement
->padding_statement
.fill
->data
;
277 link_order
->u
.data
.size
= statement
->padding_statement
.fill
->size
;
283 /* All the other ones fall through */
288 /* Wander around the input sections, make sure that
289 we'll never try and create an output section with more relocs
290 than will fit.. Do this by always assuming the worst case, and
291 creating new output sections with all the right bits. */
294 clone_section (bfd
*abfd
, asection
*s
, const char *name
, int *count
)
299 struct bfd_link_hash_entry
*h
;
301 /* Invent a section name from the first five chars of the base
302 section name and a digit suffix. */
303 strncpy (templ
, name
, sizeof (templ
) - 1);
304 templ
[sizeof (templ
) - 1] = '\0';
305 if ((sname
= bfd_get_unique_section_name (abfd
, templ
, count
)) == NULL
306 || (n
= bfd_make_section_anyway (abfd
, sname
)) == NULL
307 || (h
= bfd_link_hash_lookup (link_info
.hash
,
308 sname
, TRUE
, TRUE
, FALSE
)) == NULL
)
310 einfo (_("%F%P: clone section failed: %E\n"));
311 /* Silence gcc warnings. einfo exits, so we never reach here. */
315 /* Set up section symbol. */
316 h
->type
= bfd_link_hash_defined
;
318 h
->u
.def
.section
= n
;
322 n
->user_set_vma
= s
->user_set_vma
;
326 n
->output_offset
= s
->output_offset
;
327 n
->output_section
= n
;
330 n
->alignment_power
= s
->alignment_power
;
338 struct bfd_link_order
*l
= s
->link_order_head
;
339 printf ("vma %x size %x\n", s
->vma
, s
->_raw_size
);
342 if (l
->type
== bfd_indirect_link_order
)
344 printf ("%8x %s\n", l
->offset
, l
->u
.indirect
.section
->owner
->filename
);
348 printf (_("%8x something else\n"), l
->offset
);
355 dump (char *s
, asection
*a1
, asection
*a2
)
363 sanity_check (bfd
*abfd
)
366 for (s
= abfd
->sections
; s
; s
= s
->next
)
368 struct bfd_link_order
*p
;
370 for (p
= s
->link_order_head
; p
; p
= p
->next
)
372 if (p
->offset
> 100000)
374 if (p
->offset
< prev
)
381 #define sanity_check(a)
382 #define dump(a, b, c)
386 split_sections (bfd
*abfd
, struct bfd_link_info
*info
)
388 asection
*original_sec
;
389 int nsecs
= abfd
->section_count
;
391 /* Look through all the original sections. */
392 for (original_sec
= abfd
->sections
;
393 original_sec
&& nsecs
;
394 original_sec
= original_sec
->next
, nsecs
--)
397 unsigned int lines
= 0;
398 unsigned int relocs
= 0;
399 bfd_size_type sec_size
= 0;
400 struct bfd_link_order
*l
;
401 struct bfd_link_order
*p
;
402 bfd_vma vma
= original_sec
->vma
;
403 asection
*cursor
= original_sec
;
405 /* Count up the relocations and line entries to see if anything
406 would be too big to fit. Accumulate section size too. */
407 for (l
= NULL
, p
= cursor
->link_order_head
; p
!= NULL
; p
= l
->next
)
409 unsigned int thislines
= 0;
410 unsigned int thisrelocs
= 0;
411 bfd_size_type thissize
= 0;
412 if (p
->type
== bfd_indirect_link_order
)
416 sec
= p
->u
.indirect
.section
;
418 if (info
->strip
== strip_none
419 || info
->strip
== strip_some
)
420 thislines
= sec
->lineno_count
;
422 if (info
->relocatable
)
423 thisrelocs
= sec
->reloc_count
;
425 if (sec
->_cooked_size
!= 0)
426 thissize
= sec
->_cooked_size
;
428 thissize
= sec
->_raw_size
;
431 else if (info
->relocatable
432 && (p
->type
== bfd_section_reloc_link_order
433 || p
->type
== bfd_symbol_reloc_link_order
))
437 && (thisrelocs
+ relocs
>= config
.split_by_reloc
438 || thislines
+ lines
>= config
.split_by_reloc
439 || thissize
+ sec_size
>= config
.split_by_file
))
441 /* Create a new section and put this link order and the
442 following link orders into it. */
443 bfd_vma shift_offset
;
446 n
= clone_section (abfd
, cursor
, original_sec
->name
, &count
);
448 /* Attach the link orders to the new section and snip
449 them off from the old section. */
450 n
->link_order_head
= p
;
451 n
->link_order_tail
= cursor
->link_order_tail
;
452 cursor
->link_order_tail
= l
;
456 /* Change the size of the original section and
457 update the vma of the new one. */
459 dump ("before snip", cursor
, n
);
461 shift_offset
= p
->offset
;
462 if (cursor
->_cooked_size
!= 0)
464 n
->_cooked_size
= cursor
->_cooked_size
- shift_offset
;
465 cursor
->_cooked_size
= shift_offset
;
467 n
->_raw_size
= cursor
->_raw_size
- shift_offset
;
468 cursor
->_raw_size
= shift_offset
;
471 n
->lma
= n
->vma
= vma
;
473 /* Run down the chain and change the output section to
474 the right one, update the offsets too. */
477 p
->offset
-= shift_offset
;
478 if (p
->type
== bfd_indirect_link_order
)
480 p
->u
.indirect
.section
->output_section
= n
;
481 p
->u
.indirect
.section
->output_offset
= p
->offset
;
487 dump ("after snip", cursor
, n
);
496 relocs
+= thisrelocs
;
498 sec_size
+= thissize
;
505 /* Call BFD to write out the linked file. */
510 /* Reset error indicator, which can typically something like invalid
511 format from opening up the .o files. */
512 bfd_set_error (bfd_error_no_error
);
513 lang_for_each_statement (build_link_order
);
515 if (config
.split_by_reloc
!= (unsigned) -1
516 || config
.split_by_file
!= (bfd_size_type
) -1)
517 split_sections (output_bfd
, &link_info
);
518 if (!bfd_final_link (output_bfd
, &link_info
))
520 /* If there was an error recorded, print it out. Otherwise assume
521 an appropriate error message like unknown symbol was printed
524 if (bfd_get_error () != bfd_error_no_error
)
525 einfo (_("%F%P: final link failed: %E\n"));