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