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