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