ldwrite.c revision 77298
1229997Sken/* ldwrite.c -- write out the linked file
2229997Sken   Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 2000
3232604Strasz   Free Software Foundation, Inc.
4288348Smav   Written by Steve Chamberlain sac@cygnus.com
5229997Sken
6229997SkenThis file is part of GLD, the Gnu Linker.
7232604Strasz
8232604StraszThis program is free software; you can redistribute it and/or modify
9232604Straszit under the terms of the GNU General Public License as published by
10229997Skenthe Free Software Foundation; either version 2 of the License, or
11229997Sken(at your option) any later version.
12229997Sken
13229997SkenThis program is distributed in the hope that it will be useful,
14229997Skenbut WITHOUT ANY WARRANTY; without even the implied warranty of
15229997SkenMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16229997SkenGNU General Public License for more details.
17229997Sken
18229997SkenYou should have received a copy of the GNU General Public License
19229997Skenalong with this program; if not, write to the Free Software
20229997SkenFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21229997Sken
22229997Sken#include "bfd.h"
23229997Sken#include "sysdep.h"
24229997Sken#include "bfdlink.h"
25229997Sken#include "libiberty.h"
26229997Sken
27229997Sken#include "ld.h"
28229997Sken#include "ldexp.h"
29229997Sken#include "ldlang.h"
30229997Sken#include "ldwrite.h"
31229997Sken#include "ldmisc.h"
32229997Sken#include "ldgram.h"
33229997Sken#include "ldmain.h"
34229997Sken
35229997Skenstatic void build_link_order PARAMS ((lang_statement_union_type *));
36229997Skenstatic asection *clone_section PARAMS ((bfd *, asection *, const char *, int *));
37229997Skenstatic void split_sections PARAMS ((bfd *, struct bfd_link_info *));
38229997Sken
39229997Sken/* Build link_order structures for the BFD linker.  */
40229997Sken
41229997Skenstatic void
42229997Skenbuild_link_order (statement)
43229997Sken     lang_statement_union_type *statement;
44229997Sken{
45229997Sken  switch (statement->header.type)
46229997Sken    {
47229997Sken    case lang_data_statement_enum:
48229997Sken      {
49229997Sken	asection *output_section;
50229997Sken	struct bfd_link_order *link_order;
51229997Sken	bfd_vma value;
52229997Sken	boolean big_endian = false;
53229997Sken
54264886Smav	output_section = statement->data_statement.output_section;
55229997Sken	ASSERT (output_section->owner == output_bfd);
56229997Sken
57229997Sken	link_order = bfd_new_link_order (output_bfd, output_section);
58229997Sken	if (link_order == NULL)
59229997Sken	  einfo (_("%P%F: bfd_new_link_order failed\n"));
60287621Smav
61229997Sken	link_order->type = bfd_data_link_order;
62229997Sken	link_order->offset = statement->data_statement.output_vma;
63287621Smav	link_order->u.data.contents = (bfd_byte *) xmalloc (QUAD_SIZE);
64229997Sken
65229997Sken	value = statement->data_statement.value;
66229997Sken
67229997Sken	/* If the endianness of the output BFD is not known, then we
68229997Sken	   base the endianness of the data on the first input file.
69229997Sken	   By convention, the bfd_put routines for an unknown
70287621Smav	   endianness are big endian, so we must swap here if the
71287621Smav	   input file is little endian.  */
72229997Sken	if (bfd_big_endian (output_bfd))
73229997Sken	  big_endian = true;
74229997Sken	else if (bfd_little_endian (output_bfd))
75229997Sken	  big_endian = false;
76229997Sken	else
77229997Sken	  {
78229997Sken	    boolean swap;
79229997Sken
80229997Sken	    swap = false;
81287500Smav	    if (command_line.endian == ENDIAN_BIG)
82264886Smav	      big_endian = true;
83229997Sken	    else if (command_line.endian == ENDIAN_LITTLE)
84229997Sken	      {
85229997Sken		big_endian = false;
86229997Sken		swap = true;
87229997Sken	      }
88287499Smav	    else if (command_line.endian == ENDIAN_UNSET)
89264886Smav	      {
90264886Smav		big_endian = true;
91264886Smav		{
92267877Smav		  LANG_FOR_EACH_INPUT_STATEMENT (s)
93229997Sken		    {
94229997Sken		      if (s->the_bfd != NULL)
95229997Sken			{
96229997Sken			  if (bfd_little_endian (s->the_bfd))
97229997Sken			    {
98229997Sken			      big_endian = false;
99229997Sken			      swap = true;
100229997Sken			    }
101229997Sken			  break;
102229997Sken			}
103229997Sken		    }
104229997Sken		}
105229997Sken	      }
106229997Sken
107229997Sken	    if (swap)
108229997Sken	      {
109287621Smav		bfd_byte buffer[8];
110229997Sken
111229997Sken		switch (statement->data_statement.type)
112229997Sken		  {
113229997Sken		  case QUAD:
114229997Sken		  case SQUAD:
115264886Smav		    if (sizeof (bfd_vma) >= QUAD_SIZE)
116229997Sken		      {
117229997Sken			bfd_putl64 (value, buffer);
118229997Sken			value = bfd_getb64 (buffer);
119229997Sken			break;
120229997Sken		      }
121287499Smav		    /* Fall through.  */
122232604Strasz		  case LONG:
123232604Strasz		    bfd_putl32 (value, buffer);
124264886Smav		    value = bfd_getb32 (buffer);
125229997Sken		    break;
126229997Sken		  case SHORT:
127229997Sken		    bfd_putl16 (value, buffer);
128229997Sken		    value = bfd_getb16 (buffer);
129229997Sken		    break;
130229997Sken		  case BYTE:
131229997Sken		    break;
132229997Sken		  default:
133230334Sken		    abort ();
134230334Sken		  }
135230334Sken	      }
136230334Sken	  }
137230334Sken
138230334Sken	ASSERT (output_section->owner == output_bfd);
139230334Sken	switch (statement->data_statement.type)
140230334Sken	  {
141229997Sken	  case QUAD:
142229997Sken	  case SQUAD:
143229997Sken	    if (sizeof (bfd_vma) >= QUAD_SIZE)
144229997Sken	      bfd_put_64 (output_bfd, value, link_order->u.data.contents);
145229997Sken	    else
146229997Sken	      {
147229997Sken		bfd_vma high;
148229997Sken
149288220Smav		if (statement->data_statement.type == QUAD)
150229997Sken		  high = 0;
151240993Strasz		else if ((value & 0x80000000) == 0)
152229997Sken		  high = 0;
153229997Sken		else
154229997Sken		  high = (bfd_vma) -1;
155267877Smav		bfd_put_32 (output_bfd, high,
156229997Sken			    (link_order->u.data.contents
157264886Smav			     + (big_endian ? 0 : 4)));
158229997Sken		bfd_put_32 (output_bfd, value,
159229997Sken			    (link_order->u.data.contents
160229997Sken			     + (big_endian ? 4 : 0)));
161229997Sken	      }
162229997Sken	    link_order->size = QUAD_SIZE;
163240993Strasz	    break;
164229997Sken	  case LONG:
165229997Sken	    bfd_put_32 (output_bfd, value, link_order->u.data.contents);
166229997Sken	    link_order->size = LONG_SIZE;
167229997Sken	    break;
168229997Sken	  case SHORT:
169229997Sken	    bfd_put_16 (output_bfd, value, link_order->u.data.contents);
170229997Sken	    link_order->size = SHORT_SIZE;
171229997Sken	    break;
172229997Sken	  case BYTE:
173229997Sken	    bfd_put_8 (output_bfd, value, link_order->u.data.contents);
174229997Sken	    link_order->size = BYTE_SIZE;
175229997Sken	    break;
176288220Smav	  default:
177229997Sken	    abort ();
178229997Sken	  }
179229997Sken      }
180229997Sken      break;
181229997Sken
182229997Sken    case lang_reloc_statement_enum:
183288427Smav      {
184229997Sken	lang_reloc_statement_type *rs;
185229997Sken	asection *output_section;
186229997Sken	struct bfd_link_order *link_order;
187229997Sken
188229997Sken	rs = &statement->reloc_statement;
189229997Sken
190287499Smav	output_section = rs->output_section;
191287499Smav	ASSERT (output_section->owner == output_bfd);
192229997Sken
193229997Sken	link_order = bfd_new_link_order (output_bfd, output_section);
194229997Sken	if (link_order == NULL)
195229997Sken	  einfo (_("%P%F: bfd_new_link_order failed\n"));
196229997Sken
197229997Sken	link_order->offset = rs->output_vma;
198229997Sken	link_order->size = bfd_get_reloc_size (rs->howto);
199229997Sken
200229997Sken	link_order->u.reloc.p =
201229997Sken	  ((struct bfd_link_order_reloc *)
202229997Sken	   xmalloc (sizeof (struct bfd_link_order_reloc)));
203229997Sken
204229997Sken	link_order->u.reloc.p->reloc = rs->reloc;
205229997Sken	link_order->u.reloc.p->addend = rs->addend_value;
206229997Sken
207229997Sken	if (rs->name == NULL)
208229997Sken	  {
209229997Sken	    link_order->type = bfd_section_reloc_link_order;
210229997Sken	    if (rs->section->owner == output_bfd)
211229997Sken	      link_order->u.reloc.p->u.section = rs->section;
212229997Sken	    else
213229997Sken	      {
214287499Smav		link_order->u.reloc.p->u.section = rs->section->output_section;
215264886Smav		link_order->u.reloc.p->addend += rs->section->output_offset;
216229997Sken	      }
217229997Sken	  }
218229997Sken	else
219229997Sken	  {
220229997Sken	    link_order->type = bfd_symbol_reloc_link_order;
221287499Smav	    link_order->u.reloc.p->u.name = rs->name;
222264886Smav	  }
223287499Smav      }
224264886Smav      break;
225288215Smav
226264886Smav    case lang_input_section_enum:
227264886Smav      /* Create a new link_order in the output section with this
228288215Smav	 attached */
229264886Smav      if (statement->input_section.ifile->just_syms_flag == false)
230264886Smav	{
231264886Smav	  asection *i = statement->input_section.section;
232264886Smav	  asection *output_section = i->output_section;
233275058Smav
234275058Smav	  ASSERT (output_section->owner == output_bfd);
235275058Smav
236275058Smav	  if ((output_section->flags & SEC_HAS_CONTENTS) != 0)
237267519Smav	    {
238267877Smav	      struct bfd_link_order *link_order;
239264886Smav
240264886Smav	      link_order = bfd_new_link_order (output_bfd, output_section);
241267877Smav
242264886Smav	      if (i->flags & SEC_NEVER_LOAD)
243264886Smav		{
244264886Smav		  /* We've got a never load section inside one which
245264886Smav		     is going to be output, we'll change it into a
246275009Smav		     fill link_order */
247275058Smav		  link_order->type = bfd_fill_link_order;
248275058Smav		  link_order->u.fill.value = 0;
249275058Smav		}
250229997Sken	      else
251229997Sken		{
252229997Sken		  link_order->type = bfd_indirect_link_order;
253229997Sken		  link_order->u.indirect.section = i;
254229997Sken		  ASSERT (i->output_section == output_section);
255229997Sken		}
256229997Sken	      if (i->_cooked_size)
257229997Sken		link_order->size = i->_cooked_size;
258229997Sken	      else
259229997Sken		link_order->size = bfd_get_section_size_before_reloc (i);
260229997Sken	      link_order->offset = i->output_offset;
261229997Sken	    }
262229997Sken	}
263267537Smav      break;
264229997Sken
265229997Sken    case lang_padding_statement_enum:
266229997Sken      /* Make a new link_order with the right filler */
267229997Sken      {
268229997Sken	asection *output_section;
269229997Sken	struct bfd_link_order *link_order;
270287499Smav
271267537Smav	output_section = statement->padding_statement.output_section;
272229997Sken	ASSERT (statement->padding_statement.output_section->owner
273287499Smav		== output_bfd);
274267519Smav	if ((output_section->flags & SEC_HAS_CONTENTS) != 0)
275267537Smav	  {
276267537Smav	    link_order = bfd_new_link_order (output_bfd, output_section);
277267537Smav	    link_order->type = bfd_fill_link_order;
278267537Smav	    link_order->size = statement->padding_statement.size;
279267537Smav	    link_order->offset = statement->padding_statement.output_offset;
280267537Smav	    link_order->u.fill.value = statement->padding_statement.fill;
281267519Smav	  }
282287499Smav      }
283264886Smav      break;
284264886Smav
285264886Smav    default:
286229997Sken      /* All the other ones fall through */
287264886Smav      break;
288264886Smav    }
289264886Smav}
290264886Smav
291264886Smav/* Call BFD to write out the linked file.  */
292264886Smav
293264886Smav/**********************************************************************/
294264886Smav
295264886Smav/* Wander around the input sections, make sure that
296229997Sken   we'll never try and create an output section with more relocs
297264886Smav   than will fit.. Do this by always assuming the worst case, and
298267519Smav   creating new output sections with all the right bits.  */
299229997Sken#define TESTIT 1
300264886Smavstatic asection *
301264886Smavclone_section (abfd, s, name, count)
302229997Sken     bfd *abfd;
303264886Smav     asection *s;
304229997Sken     const char *name;
305229997Sken     int *count;
306264886Smav{
307229997Sken  char templ[6];
308275953Smav  char *sname;
309264886Smav  asection *n;
310229997Sken  struct bfd_link_hash_entry *h;
311229997Sken
312264886Smav  /* Invent a section name from the first five chars of the base
313264886Smav     section name and a digit suffix.  */
314229997Sken  strncpy (templ, name, sizeof (templ) - 1);
315264886Smav  templ[sizeof (templ) - 1] = '\0';
316229997Sken  if ((sname = bfd_get_unique_section_name (abfd, templ, count)) == NULL
317264886Smav      || (n = bfd_make_section_anyway (abfd, sname)) == NULL
318264886Smav      || (h = bfd_link_hash_lookup (link_info.hash,
319264886Smav				    sname, true, true, false)) == NULL)
320264886Smav    {
321229997Sken      einfo (_("%F%P: clone section failed: %E\n"));
322267514Smav      /* Silence gcc warnings.  einfo exits, so we never reach here.  */
323267514Smav      return NULL;
324264886Smav    }
325264886Smav
326264886Smav  /* Set up section symbol.  */
327267519Smav  h->type = bfd_link_hash_defined;
328229997Sken  h->u.def.value = 0;
329288215Smav  h->u.def.section = n;
330229997Sken
331229997Sken  n->flags = s->flags;
332264886Smav  n->vma = s->vma;
333229997Sken  n->user_set_vma = s->user_set_vma;
334264886Smav  n->lma = s->lma;
335264886Smav  n->_cooked_size = 0;
336264886Smav  n->_raw_size = 0;
337264886Smav  n->output_offset = s->output_offset;
338264886Smav  n->output_section = n;
339264886Smav  n->orelocation = 0;
340264886Smav  n->reloc_count = 0;
341264886Smav  n->alignment_power = s->alignment_power;
342267877Smav  return n;
343264886Smav}
344264886Smav
345264886Smav#if TESTING
346264886Smavstatic void
347264886Smavds (s)
348267877Smav     asection *s;
349264886Smav{
350267877Smav  struct bfd_link_order *l = s->link_order_head;
351264886Smav  printf ("vma %x size %x\n", s->vma, s->_raw_size);
352264886Smav  while (l)
353264886Smav    {
354264886Smav      if (l->type == bfd_indirect_link_order)
355264886Smav	{
356264886Smav	  printf ("%8x %s\n", l->offset, l->u.indirect.section->owner->filename);
357264886Smav	}
358264886Smav      else
359264886Smav	{
360267877Smav	  printf (_("%8x something else\n"), l->offset);
361229997Sken	}
362229997Sken      l = l->next;
363229997Sken    }
364229997Sken  printf ("\n");
365229997Sken}
366229997Sken
367288220Smavdump (s, a1, a2)
368288220Smav     char *s;
369229997Sken     asection *a1;
370229997Sken     asection *a2;
371229997Sken{
372229997Sken  printf ("%s\n", s);
373288220Smav  ds (a1);
374229997Sken  ds (a2);
375229997Sken}
376229997Sken
377287499Smavstatic void
378229997Skensanity_check (abfd)
379229997Sken     bfd *abfd;
380229997Sken{
381229997Sken  asection *s;
382232604Strasz  for (s = abfd->sections; s; s = s->next)
383232604Strasz    {
384232604Strasz      struct bfd_link_order *p;
385229997Sken      bfd_vma prev = 0;
386229997Sken      for (p = s->link_order_head; p; p = p->next)
387229997Sken	{
388229997Sken	  if (p->offset > 100000)
389229997Sken	    abort ();
390229997Sken	  if (p->offset < prev)
391229997Sken	    abort ();
392229997Sken	  prev = p->offset;
393229997Sken	}
394229997Sken    }
395229997Sken}
396229997Sken#else
397229997Sken#define sanity_check(a)
398229997Sken#define dump(a, b, c)
399229997Sken#endif
400229997Sken
401229997Skenstatic void
402229997Skensplit_sections (abfd, info)
403229997Sken     bfd *abfd;
404229997Sken     struct bfd_link_info *info;
405229997Sken{
406229997Sken  asection *original_sec;
407229997Sken  int nsecs = abfd->section_count;
408229997Sken  sanity_check (abfd);
409229997Sken  /* Look through all the original sections.  */
410229997Sken  for (original_sec = abfd->sections;
411229997Sken       original_sec && nsecs;
412287499Smav       original_sec = original_sec->next, nsecs--)
413229997Sken    {
414229997Sken      int count = 0;
415229997Sken      unsigned int lines = 0;
416229997Sken      unsigned int relocs = 0;
417229997Sken      bfd_size_type sec_size = 0;
418229997Sken      struct bfd_link_order *l;
419229997Sken      struct bfd_link_order *p;
420229997Sken      bfd_vma vma = original_sec->vma;
421229997Sken      asection *cursor = original_sec;
422229997Sken
423287499Smav      /* Count up the relocations and line entries to see if anything
424229997Sken	 would be too big to fit.  Accumulate section size too.  */
425229997Sken      for (l = NULL, p = cursor->link_order_head; p != NULL; p = l->next)
426229997Sken	{
427229997Sken	  unsigned int thislines = 0;
428229997Sken	  unsigned int thisrelocs = 0;
429229997Sken	  bfd_size_type thissize = 0;
430229997Sken	  if (p->type == bfd_indirect_link_order)
431229997Sken	    {
432229997Sken	      asection *sec;
433229997Sken
434229997Sken	      sec = p->u.indirect.section;
435229997Sken
436229997Sken	      if (info->strip == strip_none
437229997Sken		  || info->strip == strip_some)
438229997Sken		thislines = sec->lineno_count;
439229997Sken
440229997Sken	      if (info->relocateable)
441229997Sken		thisrelocs = sec->reloc_count;
442229997Sken
443287499Smav	      if (sec->_cooked_size != 0)
444229997Sken		thissize = sec->_cooked_size;
445229997Sken	      else
446229997Sken		thissize = sec->_raw_size;
447229997Sken
448252569Smav	    }
449252569Smav	  else if (info->relocateable
450252569Smav		   && (p->type == bfd_section_reloc_link_order
451229997Sken		       || p->type == bfd_symbol_reloc_link_order))
452229997Sken	    thisrelocs++;
453229997Sken
454229997Sken	  if (l != NULL
455229997Sken	      && (thisrelocs + relocs >= config.split_by_reloc
456229997Sken		  || thislines + lines >= config.split_by_reloc
457288220Smav		  || thissize + sec_size >= config.split_by_file))
458229997Sken	    {
459229997Sken	      /* Create a new section and put this link order and the
460229997Sken		 following link orders into it.  */
461229997Sken	      bfd_vma shift_offset;
462229997Sken	      asection *n;
463229997Sken
464229997Sken	      n = clone_section (abfd, cursor, original_sec->name, &count);
465229997Sken
466229997Sken	      /* Attach the link orders to the new section and snip
467229997Sken		 them off from the old section.  */
468229997Sken	      n->link_order_head = p;
469229997Sken	      n->link_order_tail = cursor->link_order_tail;
470229997Sken	      cursor->link_order_tail = l;
471229997Sken	      l->next = NULL;
472229997Sken	      l = p;
473229997Sken
474229997Sken	      /* Change the size of the original section and
475229997Sken		 update the vma of the new one.  */
476264886Smav
477287670Smav	      dump ("before snip", cursor, n);
478264886Smav
479287499Smav	      shift_offset = p->offset;
480267877Smav	      if (cursor->_cooked_size != 0)
481229997Sken		{
482264886Smav		  n->_cooked_size = cursor->_cooked_size - shift_offset;
483229997Sken		  cursor->_cooked_size = shift_offset;
484229997Sken		}
485229997Sken	      n->_raw_size = cursor->_raw_size - shift_offset;
486229997Sken	      cursor->_raw_size = shift_offset;
487229997Sken
488229997Sken	      vma += shift_offset;
489229997Sken	      n->lma = n->vma = vma;
490229997Sken
491229997Sken	      /* Run down the chain and change the output section to
492229997Sken		 the right one, update the offsets too.  */
493229997Sken	      do
494287499Smav		{
495229997Sken		  p->offset -= shift_offset;
496229997Sken		  if (p->type == bfd_indirect_link_order)
497287499Smav		    {
498229997Sken		      p->u.indirect.section->output_section = n;
499267481Smav		      p->u.indirect.section->output_offset = p->offset;
500229997Sken		    }
501287499Smav		  p = p->next;
502229997Sken		}
503229997Sken	      while (p);
504229997Sken
505229997Sken	      dump ("after snip", cursor, n);
506287499Smav	      cursor = n;
507287499Smav	      relocs = thisrelocs;
508287499Smav	      lines = thislines;
509287500Smav	      sec_size = thissize;
510287499Smav	    }
511264886Smav	  else
512287499Smav	    {
513229997Sken	      l = p;
514229997Sken	      relocs += thisrelocs;
515287499Smav	      lines += thislines;
516229997Sken	      sec_size += thissize;
517287499Smav	    }
518287499Smav	}
519287621Smav    }
520287621Smav  sanity_check (abfd);
521287621Smav}
522287621Smav
523287621Smav/**********************************************************************/
524287621Smav
525287621Smavvoid
526229997Skenldwrite ()
527288310Smav{
528288310Smav  /* Reset error indicator, which can typically something like invalid
529287499Smav     format from opening up the .o files.  */
530287499Smav  bfd_set_error (bfd_error_no_error);
531288310Smav  lang_for_each_statement (build_link_order);
532288310Smav
533287499Smav  if (config.split_by_reloc != (unsigned) -1
534287499Smav      || config.split_by_file != (bfd_size_type) -1)
535287499Smav    split_sections (output_bfd, &link_info);
536229997Sken  if (!bfd_final_link (output_bfd, &link_info))
537229997Sken    {
538287499Smav      /* If there was an error recorded, print it out.  Otherwise assume
539229997Sken	 an appropriate error message like unknown symbol was printed
540229997Sken	 out.  */
541287499Smav
542287499Smav      if (bfd_get_error () != bfd_error_no_error)
543287499Smav	einfo (_("%F%P: final link failed: %E\n"));
544287499Smav      else
545287499Smav	xexit (1);
546229997Sken    }
547229997Sken}
548229997Sken