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