* ldlang.c: When traversing lang_statement_union_type lists,
[platform/upstream/binutils.git] / ld / mpw-eppcmac.c
1 /* This file is is generated by a shell script.  DO NOT EDIT! */
2
3 /* AIX emulation code for ppcmacos
4    Copyright 1991, 1993, 1995, 1996, 1997, 2000
5    Free Software Foundation, Inc.
6    Written by Steve Chamberlain <sac@cygnus.com>
7    AIX support by Ian Lance Taylor <ian@cygnus.com>
8
9 This file is part of GLD, the Gnu Linker.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
24
25 #define TARGET_IS_ppcmacos
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libiberty.h"
30 #include "getopt.h"
31 #include "bfdlink.h"
32
33 #include <ctype.h>
34
35 #include "ld.h"
36 #include "ldmain.h"
37 #include "ldmisc.h"
38 #include "ldexp.h"
39 #include "ldlang.h"
40 #include "ldctor.h"
41 #include "ldgram.h"
42 #include "ldfile.h"
43 #include "ldemul.h"
44
45 static void gldppcmacos_before_parse PARAMS ((void));
46 static int gldppcmacos_parse_args PARAMS ((int, char **));
47 static void gldppcmacos_after_open PARAMS ((void));
48 static void gldppcmacos_before_allocation PARAMS ((void));
49 static void gldppcmacos_read_file PARAMS ((const char *, boolean));
50 static void gldppcmacos_free PARAMS ((PTR));
51 static void gldppcmacos_find_relocs
52   PARAMS ((lang_statement_union_type *));
53 static void gldppcmacos_find_exp_assignment PARAMS ((etree_type *));
54 static char *gldppcmacos_get_script PARAMS ((int *isfile));
55
56 /* The file alignment required for each section.  */
57 static unsigned long file_align;
58
59 /* The maximum size the stack is permitted to grow.  This is stored in
60    the a.out header.  */
61 static unsigned long maxstack;
62
63 /* The maximum data size.  This is stored in the a.out header.  */
64 static unsigned long maxdata;
65
66 /* Whether to perform garbage collection.  */
67 static int gc = 1;
68
69 /* The module type to use.  */
70 static unsigned short modtype = ('1' << 8) | 'L';
71
72 /* Whether the .text section must be read-only (i.e., no relocs
73    permitted).  */
74 static int textro;
75
76 /* Whether to implement Unix like linker semantics.  */
77 static int unix_ld;
78
79 /* Structure used to hold import file list.  */
80
81 struct filelist
82 {
83   struct filelist *next;
84   const char *name;
85 };
86
87 /* List of import files.  */
88 static struct filelist *import_files;
89
90 /* List of export symbols read from the export files.  */
91
92 struct export_symbol_list
93 {
94   struct export_symbol_list *next;
95   const char *name;
96   boolean syscall;
97 };
98
99 static struct export_symbol_list *export_symbols;
100
101 /* This routine is called before anything else is done.  */
102
103 static void
104 gldppcmacos_before_parse()
105 {
106 #ifndef TARGET_                 /* I.e., if not generic.  */
107   ldfile_output_architecture = bfd_arch_powerpc;
108 #endif /* not TARGET_ */
109 }
110
111 /* Handle AIX specific options.  */
112
113 static int
114 gldppcmacos_parse_args (argc, argv)
115      int argc;
116      char **argv;
117 {
118   int prevoptind = optind;
119   int prevopterr = opterr;
120   int indx;
121   int longind;
122   int optc;
123   long val;
124   char *end;
125
126 #define OPTION_IGNORE (300)
127 #define OPTION_AUTOIMP (OPTION_IGNORE + 1)
128 #define OPTION_ERNOTOK (OPTION_AUTOIMP + 1)
129 #define OPTION_EROK (OPTION_ERNOTOK + 1)
130 #define OPTION_EXPORT (OPTION_EROK + 1)
131 #define OPTION_IMPORT (OPTION_EXPORT + 1)
132 #define OPTION_LOADMAP (OPTION_IMPORT + 1)
133 #define OPTION_MAXDATA (OPTION_LOADMAP + 1)
134 #define OPTION_MAXSTACK (OPTION_MAXDATA + 1)
135 #define OPTION_MODTYPE (OPTION_MAXSTACK + 1)
136 #define OPTION_NOAUTOIMP (OPTION_MODTYPE + 1)
137 #define OPTION_NOSTRCMPCT (OPTION_NOAUTOIMP + 1)
138 #define OPTION_PD (OPTION_NOSTRCMPCT + 1)
139 #define OPTION_PT (OPTION_PD + 1)
140 #define OPTION_STRCMPCT (OPTION_PT + 1)
141 #define OPTION_UNIX (OPTION_STRCMPCT + 1)
142
143   static struct option longopts[] = {
144     {"basis", no_argument, NULL, OPTION_IGNORE},
145     {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
146     {"bcomprld", no_argument, NULL, OPTION_IGNORE},
147     {"bcrld", no_argument, NULL, OPTION_IGNORE},
148     {"bcror31", no_argument, NULL, OPTION_IGNORE},
149     {"bD", required_argument, NULL, OPTION_MAXDATA},
150     {"bE", required_argument, NULL, OPTION_EXPORT},
151     {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
152     {"berok", no_argument, NULL, OPTION_EROK},
153     {"berrmsg", no_argument, NULL, OPTION_IGNORE},
154     {"bexport", required_argument, NULL, OPTION_EXPORT},
155     {"bf", no_argument, NULL, OPTION_ERNOTOK},
156     {"bgc", no_argument, &gc, 1},
157     {"bh", required_argument, NULL, OPTION_IGNORE},
158     {"bhalt", required_argument, NULL, OPTION_IGNORE},
159     {"bI", required_argument, NULL, OPTION_IMPORT},
160     {"bimport", required_argument, NULL, OPTION_IMPORT},
161     {"bl", required_argument, NULL, OPTION_LOADMAP},
162     {"bloadmap", required_argument, NULL, OPTION_LOADMAP},
163     {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
164     {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
165     {"bM", required_argument, NULL, OPTION_MODTYPE},
166     {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
167     {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
168     {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
169     {"bnoentry", no_argument, NULL, OPTION_IGNORE},
170     {"bnogc", no_argument, &gc, 0},
171     {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
172     {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
173     {"bnotextro", no_argument, &textro, 0},
174     {"bnro", no_argument, &textro, 0},
175     {"bpD", required_argument, NULL, OPTION_PD},
176     {"bpT", required_argument, NULL, OPTION_PT},
177     {"bro", no_argument, &textro, 1},
178     {"bS", required_argument, NULL, OPTION_MAXSTACK},
179     {"bso", no_argument, NULL, OPTION_AUTOIMP},
180     {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
181     {"btextro", no_argument, &textro, 1},
182     {"static", no_argument, NULL, OPTION_NOAUTOIMP},
183     {"unix", no_argument, NULL, OPTION_UNIX},
184     {NULL, no_argument, NULL, 0}
185   };
186
187   /* Options supported by the AIX linker which we do not support: -f,
188      -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
189      -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
190      -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
191      -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
192      -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
193      -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
194      -bx, -bX, -bxref.  */
195
196   /* If the current option starts with -b, change the first : to an =.
197      The AIX linker uses : to separate the option from the argument;
198      changing it to = lets us treat it as a getopt option.  */
199   indx = optind;
200   if (indx == 0)
201     indx = 1;
202   if (indx < argc && strncmp (argv[indx], "-b", 2) == 0)
203     {
204       char *s;
205
206       for (s = argv[indx]; *s != '\0'; s++)
207         {
208           if (*s == ':')
209             {
210               *s = '=';
211               break;
212             }
213         }
214     }
215
216   opterr = 0;
217   optc = getopt_long_only (argc, argv, "-D:H:KT:z", longopts, &longind);
218   opterr = prevopterr;
219
220   switch (optc)
221     {
222     default:
223       optind = prevoptind;
224       return 0;
225
226     case 0:
227       /* Long option which just sets a flag.  */
228       break;
229
230     case 'D':
231       val = strtol (optarg, &end, 0);
232       if (*end != '\0')
233         einfo (_("%P: warning: ignoring invalid -D number %s\n"), optarg);
234       else if (val != -1)
235         lang_section_start (".data", exp_intop (val));
236       break;
237
238     case 'H':
239       val = strtoul (optarg, &end, 0);
240       if (*end != '\0'
241           || (val & (val - 1)) != 0)
242         einfo (_("%P: warning: ignoring invalid -H number %s\n"), optarg);
243       else
244         file_align = val;
245       break;
246
247     case 'K':
248     case 'z':
249       /* FIXME: This should use the page size for the target system.  */
250       file_align = 4096;
251       break;
252
253     case 'T':
254       /* On AIX this is the same as GNU ld -Ttext.  When we see -T
255          number, we assume the AIX option is intended.  Otherwise, we
256          assume the usual GNU ld -T option is intended.  We can't just
257          ignore the AIX option, because gcc passes it to the linker.  */
258       val = strtoul (optarg, &end, 0);
259       if (*end != '\0')
260         {
261           optind = prevoptind;
262           return 0;
263         }
264       lang_section_start (".text", exp_intop (val));
265       break;
266
267     case OPTION_IGNORE:
268       break;
269
270     case OPTION_AUTOIMP:
271       link_info.static_link = false;
272       break;
273
274     case OPTION_ERNOTOK:
275       force_make_executable = false;
276       break;
277
278     case OPTION_EROK:
279       force_make_executable = true;
280       break;
281
282     case OPTION_EXPORT:
283       gldppcmacos_read_file (optarg, false);
284       break;
285
286     case OPTION_IMPORT:
287       {
288         struct filelist *n;
289         struct filelist **flpp;
290
291         n = (struct filelist *) xmalloc (sizeof (struct filelist));
292         n->next = NULL;
293         n->name = optarg;
294         flpp = &import_files;
295         while (*flpp != NULL)
296           flpp = &(*flpp)->next;
297         *flpp = n;
298       }
299       break;
300
301     case OPTION_LOADMAP:
302       config.map_filename = optarg;
303       break;
304
305     case OPTION_MAXDATA:
306       val = strtoul (optarg, &end, 0);
307       if (*end != '\0')
308         einfo (_("%P: warning: ignoring invalid -bmaxdata number %s\n"),
309                optarg);
310       else
311         maxdata = val;
312       break;
313
314     case OPTION_MAXSTACK:
315       val = strtoul (optarg, &end, 0);
316       if (*end != '\0')
317         einfo (_("%P: warning: ignoring invalid -bmaxstack number %s\n"),
318                optarg);
319       else
320         maxstack = val;
321       break;
322
323     case OPTION_MODTYPE:
324       if (*optarg == 'S')
325         {
326           link_info.shared = true;
327           ++optarg;
328         }
329       if (*optarg == '\0' || optarg[1] == '\0')
330         einfo (_("%P: warning: ignoring invalid module type %s\n"), optarg);
331       else
332         modtype = (*optarg << 8) | optarg[1];
333       break;
334
335     case OPTION_NOAUTOIMP:
336       link_info.static_link = true;
337       break;
338
339     case OPTION_NOSTRCMPCT:
340       link_info.traditional_format = true;
341       break;
342
343     case OPTION_PD:
344       /* This sets the page that the .data section is supposed to
345          start on.  The offset within the page should still be the
346          offset within the file, so we need to build an appropriate
347          expression.  */
348       val = strtoul (optarg, &end, 0);
349       if (*end != '\0')
350         einfo (_("%P: warning: ignoring invalid -pD number %s\n"), optarg);
351       else
352         {
353           etree_type *t;
354
355           t = exp_binop ('+',
356                          exp_intop (val),
357                          exp_binop ('&',
358                                     exp_nameop (NAME, "."),
359                                     exp_intop (0xfff)));
360           t = exp_binop ('&',
361                          exp_binop ('+', t, exp_intop (7)),
362                          exp_intop (~ (bfd_vma) 7));
363           lang_section_start (".data", t);
364         }
365       break;
366
367     case OPTION_PT:
368       /* This set the page that the .text section is supposed to start
369          on.  The offset within the page should still be the offset
370          within the file.  */
371       val = strtoul (optarg, &end, 0);
372       if (*end != '\0')
373         einfo (_("%P: warning: ignoring invalid -pT number %s\n"), optarg);
374       else
375         {
376           etree_type *t;
377
378           t = exp_binop ('+',
379                          exp_intop (val),
380                          exp_nameop (SIZEOF_HEADERS, NULL));
381           t = exp_binop ('&',
382                          exp_binop ('+', t, exp_intop (7)),
383                          exp_intop (~ (bfd_vma) 7));
384           lang_section_start (".text", t);
385         }
386       break;
387
388     case OPTION_STRCMPCT:
389       link_info.traditional_format = false;
390       break;
391
392     case OPTION_UNIX:
393       unix_ld = true;
394       break;
395     }
396
397   return 1;
398 }
399
400 /* This is called when an input file can not be recognized as a BFD
401    object or an archive.  If the file starts with #!, we must treat it
402    as an import file.  This is for AIX compatibility.  */
403
404 static boolean
405 gldppcmacos_unrecognized_file (entry)
406      lang_input_statement_type *entry;
407 {
408   FILE *e;
409   boolean ret;
410
411   e = fopen (entry->filename, FOPEN_RT);
412   if (e == NULL)
413     return false;
414
415   ret = false;
416
417   if (getc (e) == '#' && getc (e) == '!')
418     {
419       struct filelist *n;
420       struct filelist **flpp;
421
422       n = (struct filelist *) xmalloc (sizeof (struct filelist));
423       n->next = NULL;
424       n->name = entry->filename;
425       flpp = &import_files;
426       while (*flpp != NULL)
427         flpp = &(*flpp)->next;
428       *flpp = n;
429
430       ret = true;
431       entry->loaded = true;
432     }
433
434   fclose (e);
435
436   return ret;
437 }
438
439 /* This is called after the input files have been opened.  */
440
441 static void
442 gldppcmacos_after_open ()
443 {
444   boolean r;
445   struct set_info *p;
446
447   /* Call ldctor_build_sets, after pretending that this is a
448      relocateable link.  We do this because AIX requires relocation
449      entries for all references to symbols, even in a final
450      executable.  Of course, we only want to do this if we are
451      producing an XCOFF output file.  */
452   r = link_info.relocateable;
453   if (strstr (bfd_get_target (output_bfd), "xcoff") != NULL)
454     link_info.relocateable = true;
455   ldctor_build_sets ();
456   link_info.relocateable = r;
457
458   /* For each set, record the size, so that the XCOFF backend can
459      output the correct csect length.  */
460   for (p = sets; p != (struct set_info *) NULL; p = p->next)
461     {
462       bfd_size_type size;
463
464       /* If the symbol is defined, we may have been invoked from
465          collect, and the sets may already have been built, so we do
466          not do anything.  */
467       if (p->h->type == bfd_link_hash_defined
468           || p->h->type == bfd_link_hash_defweak)
469         continue;
470
471       if (p->reloc != BFD_RELOC_CTOR)
472         {
473           /* Handle this if we need to.  */
474           abort ();
475         }
476
477       size = (p->count + 2) * 4;
478       if (! bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size))
479         einfo (_("%F%P: bfd_xcoff_link_record_set failed: %E\n"));
480     }
481 }
482
483 /* This is called after the sections have been attached to output
484    sections, but before any sizes or addresses have been set.  */
485
486 static void
487 gldppcmacos_before_allocation ()
488 {
489   struct filelist *fl;
490   struct export_symbol_list *el;
491   char *libpath;
492   asection *special_sections[6];
493   int i;
494
495   /* Handle the import and export files, if any.  */
496   for (fl = import_files; fl != NULL; fl = fl->next)
497     gldppcmacos_read_file (fl->name, true);
498   for (el = export_symbols; el != NULL; el = el->next)
499     {
500       struct bfd_link_hash_entry *h;
501
502       h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
503       if (h == NULL)
504         einfo (_("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n"));
505       if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, el->syscall))
506         einfo (_("%P%F: bfd_xcoff_export_symbol failed: %E\n"));
507     }
508
509   /* Track down all relocations called for by the linker script (these
510      are typically constructor/destructor entries created by
511      CONSTRUCTORS) and let the backend know it will need to create
512      .loader relocs for them.  */
513   lang_for_each_statement (gldppcmacos_find_relocs);
514
515   /* We need to build LIBPATH from the -L arguments.  If any -rpath
516      arguments were used, though, we use -rpath instead, as a GNU
517      extension.  */
518   if (command_line.rpath != NULL)
519     libpath = command_line.rpath;
520   else if (search_head == NULL)
521     libpath = (char *) "";
522   else
523     {
524       size_t len;
525       search_dirs_type *search;
526
527       len = strlen (search_head->name);
528       libpath = xmalloc (len + 1);
529       strcpy (libpath, search_head->name);
530       for (search = search_head->next; search != NULL; search = search->next)
531         {
532           size_t nlen;
533
534           nlen = strlen (search->name);
535           libpath = xrealloc (libpath, len + nlen + 2);
536           libpath[len] = ':';
537           strcpy (libpath + len + 1, search->name);
538           len += nlen + 1;
539         }
540     }
541
542   /* Let the XCOFF backend set up the .loader section.  */
543   if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
544                                          entry_symbol, file_align,
545                                          maxstack, maxdata,
546                                          gc && ! unix_ld ? true : false,
547                                          modtype,
548                                          textro ? true : false,
549                                          unix_ld,
550                                          special_sections))
551     einfo (_("%P%F: failed to set dynamic section sizes: %E\n"));
552
553   /* Look through the special sections, and put them in the right
554      place in the link ordering.  This is especially magic.  */
555   for (i = 0; i < 6; i++)
556     {
557       asection *sec;
558       lang_output_section_statement_type *os;
559       lang_statement_union_type **pls;
560       lang_input_section_type *is;
561       const char *oname;
562       boolean start;
563
564       sec = special_sections[i];
565       if (sec == NULL)
566         continue;
567
568       /* Remove this section from the list of the output section.
569          This assumes we know what the script looks like.  */
570       is = NULL;
571       os = lang_output_section_find (sec->output_section->name);
572       if (os == NULL)
573         einfo (_("%P%F: can't find output section %s\n"),
574                sec->output_section->name);
575       for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->header.next)
576         {
577           if ((*pls)->header.type == lang_input_section_enum
578               && (*pls)->input_section.section == sec)
579             {
580               is = (lang_input_section_type *) *pls;
581               *pls = (*pls)->header.next;
582               break;
583             }
584           if ((*pls)->header.type == lang_wild_statement_enum)
585             {
586               lang_statement_union_type **pwls;
587
588               for (pwls = &(*pls)->wild_statement.children.head;
589                    *pwls != NULL;
590                    pwls = &(*pwls)->header.next)
591                 {
592                   if ((*pwls)->header.type == lang_input_section_enum
593                       && (*pwls)->input_section.section == sec)
594                     {
595                       is = (lang_input_section_type *) *pwls;
596                       *pwls = (*pwls)->header.next;
597                       break;
598                     }
599                 }
600               if (is != NULL)
601                 break;
602             }
603         }       
604
605       if (is == NULL)
606         einfo (_("%P%F: can't find %s in output section\n"),
607                bfd_get_section_name (sec->owner, sec));
608
609       /* Now figure out where the section should go.  */
610       switch (i)
611         {
612         default: /* to avoid warnings */
613         case 0:
614           /* _text */
615           oname = ".text";
616           start = true;
617           break;
618         case 1:
619           /* _etext */
620           oname = ".text";
621           start = false;
622           break;
623         case 2:
624           /* _data */
625           oname = ".data";
626           start = true;
627           break;
628         case 3:
629           /* _edata */
630           oname = ".data";
631           start = false;
632           break;
633         case 4:
634         case 5:
635           /* _end and end */
636           oname = ".bss";
637           start = false;
638           break;
639         }
640
641       os = lang_output_section_find (oname);
642
643       if (start)
644         {
645           is->header.next = os->children.head;
646           os->children.head = (lang_statement_union_type *) is;
647         }
648       else
649         {
650           is->header.next = NULL;
651           lang_statement_append (&os->children,
652                                  (lang_statement_union_type *) is,
653                                  &is->header.next);
654         }
655     }
656 }
657
658 /* Read an import or export file.  For an import file, this is called
659    by the before_allocation emulation routine.  For an export file,
660    this is called by the parse_args emulation routine.  */
661
662 static void
663 gldppcmacos_read_file (filename, import)
664      const char *filename;
665      boolean import;
666 {
667   struct obstack *o;
668   FILE *f;
669   int lineno;
670   int c;
671   boolean keep;
672   const char *imppath;
673   const char *impfile;
674   const char *impmember;
675
676   o = (struct obstack *) xmalloc (sizeof (struct obstack));
677   obstack_specify_allocation (o, 0, 0, xmalloc, gldppcmacos_free);
678
679   f = fopen (filename, FOPEN_RT);
680   if (f == NULL)
681     {
682       bfd_set_error (bfd_error_system_call);
683       einfo ("%F%s: %E\n", filename);
684     }
685
686   keep = false;
687
688   imppath = NULL;
689   impfile = NULL;
690   impmember = NULL;
691
692   lineno = 0;
693   while ((c = getc (f)) != EOF)
694     {
695       char *s;
696       char *symname;
697       boolean syscall;
698       bfd_vma address;
699       struct bfd_link_hash_entry *h;
700
701       if (c != '\n')
702         {
703           obstack_1grow (o, c);
704           continue;
705         }
706
707       obstack_1grow (o, '\0');
708       ++lineno;
709
710       s = (char *) obstack_base (o);
711       while (isspace ((unsigned char) *s))
712         ++s;
713       if (*s == '\0'
714           || *s == '*'
715           || (*s == '#' && s[1] == ' ')
716           || (! import && *s == '#' && s[1] == '!'))
717         {
718           obstack_free (o, obstack_base (o));
719           continue;
720         }
721
722       if (*s == '#' && s[1] == '!')
723         {
724           s += 2;
725           while (isspace ((unsigned char) *s))
726             ++s;
727           if (*s == '\0')
728             {
729               imppath = NULL;
730               impfile = NULL;
731               impmember = NULL;
732               obstack_free (o, obstack_base (o));
733             }
734           else if (*s == '(')
735             einfo (_("%F%s%d: #! ([member]) is not supported in import files\n"),
736                    filename, lineno);
737           else
738             {
739               char cs;
740               char *file;
741
742               (void) obstack_finish (o);
743               keep = true;
744               imppath = s;
745               file = NULL;
746               while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
747                 {
748                   if (*s == '/')
749                     file = s + 1;
750                   ++s;
751                 }
752               if (file != NULL)
753                 {
754                   file[-1] = '\0';
755                   impfile = file;
756                   if (imppath == file - 1)
757                     imppath = "/";
758                 }
759               else
760                 {
761                   impfile = imppath;
762                   imppath = "";
763                 }
764               cs = *s;
765               *s = '\0';
766               while (isspace ((unsigned char) cs))
767                 {
768                   ++s;
769                   cs = *s;
770                 }
771               if (cs != '(')
772                 {
773                   impmember = "";
774                   if (cs != '\0')
775                     einfo (_("%s:%d: warning: syntax error in import file\n"),
776                            filename, lineno);
777                 }
778               else
779                 {
780                   ++s;
781                   impmember = s;
782                   while (*s != ')' && *s != '\0')
783                     ++s;
784                   if (*s == ')')
785                     *s = '\0';
786                   else
787                     einfo (_("%s:%d: warning: syntax error in import file\n"),
788                            filename, lineno);
789                 }
790             }
791
792           continue;
793         }
794
795       /* This is a symbol to be imported or exported.  */
796       symname = s;
797       syscall = false;
798       address = (bfd_vma) -1;
799
800       while (! isspace ((unsigned char) *s) && *s != '\0')
801         ++s;
802       if (*s != '\0')
803         {
804           char *se;
805
806           *s++ = '\0';
807
808           while (isspace ((unsigned char) *s))
809             ++s;
810
811           se = s;
812           while (! isspace ((unsigned char) *se) && *se != '\0')
813             ++se;
814           if (*se != '\0')
815             {
816               *se++ = '\0';
817               while (isspace ((unsigned char) *se))
818                 ++se;
819               if (*se != '\0')
820                 einfo (_("%s%d: warning: syntax error in import/export file\n"),
821                        filename, lineno);
822             }
823
824           if (strcasecmp (s, "svc") == 0
825               || strcasecmp (s, "syscall") == 0)
826             syscall = true;
827           else
828             {
829               char *end;
830
831               address = strtoul (s, &end, 0);
832               if (*end != '\0')
833                 einfo (_("%s:%d: warning: syntax error in import/export file\n"),
834                        filename, lineno);
835             }
836         }
837
838       if (! import)
839         {
840           struct export_symbol_list *n;
841
842           ldlang_add_undef (symname);
843           n = ((struct export_symbol_list *)
844                xmalloc (sizeof (struct export_symbol_list)));
845           n->next = export_symbols;
846           n->name = xstrdup (symname);
847           n->syscall = syscall;
848           export_symbols = n;
849         }
850       else
851         {
852           h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
853                                     true);
854           if (h == NULL || h->type == bfd_link_hash_new)
855             {
856               /* We can just ignore attempts to import an unreferenced
857                  symbol.  */
858             }
859           else
860             {
861               if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
862                                              address, imppath, impfile,
863                                              impmember))
864                 einfo (_("%X%s:%d: failed to import symbol %s: %E\n"),
865                        filename, lineno, symname);
866             }
867         }
868
869       obstack_free (o, obstack_base (o));
870     }
871
872   if (obstack_object_size (o) > 0)
873     {
874       einfo (_("%s:%d: warning: ignoring unterminated last line\n"),
875              filename, lineno);
876       obstack_free (o, obstack_base (o));
877     }
878
879   if (! keep)
880     {
881       obstack_free (o, NULL);
882       free (o);
883     }
884 }
885
886 /* This routine saves us from worrying about declaring free.  */
887
888 static void
889 gldppcmacos_free (p)
890      PTR p;
891 {
892   free (p);
893 }
894
895 /* This is called by the before_allocation routine via
896    lang_for_each_statement.  It looks for relocations and assignments
897    to symbols.  */
898
899 static void
900 gldppcmacos_find_relocs (s)
901      lang_statement_union_type *s;
902 {
903   if (s->header.type == lang_reloc_statement_enum)
904     {
905       lang_reloc_statement_type *rs;
906
907       rs = &s->reloc_statement;
908       if (rs->name == NULL)
909         einfo (_("%F%P: only relocations against symbols are permitted\n"));
910       if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
911         einfo (_("%F%P: bfd_xcoff_link_count_reloc failed: %E\n"));
912     }
913
914   if (s->header.type == lang_assignment_statement_enum)
915     gldppcmacos_find_exp_assignment (s->assignment_statement.exp);
916 }
917
918 /* Look through an expression for an assignment statement.  */
919
920 static void
921 gldppcmacos_find_exp_assignment (exp)
922      etree_type *exp;
923 {
924   struct bfd_link_hash_entry *h;
925
926   switch (exp->type.node_class)
927     {
928     case etree_provide:
929       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
930                                 false, false, false);
931       if (h == NULL)
932         break;
933       /* Fall through.  */
934     case etree_assign:
935       if (strcmp (exp->assign.dst, ".") != 0)
936         {
937           if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
938                                                   exp->assign.dst))
939             einfo (_("%P%F: failed to record assignment to %s: %E\n"),
940                    exp->assign.dst);
941         }
942       gldppcmacos_find_exp_assignment (exp->assign.src);
943       break;
944
945     case etree_binary:
946       gldppcmacos_find_exp_assignment (exp->binary.lhs);
947       gldppcmacos_find_exp_assignment (exp->binary.rhs);
948       break;
949
950     case etree_trinary:
951       gldppcmacos_find_exp_assignment (exp->trinary.cond);
952       gldppcmacos_find_exp_assignment (exp->trinary.lhs);
953       gldppcmacos_find_exp_assignment (exp->trinary.rhs);
954       break;
955
956     case etree_unary:
957       gldppcmacos_find_exp_assignment (exp->unary.child);
958       break;
959
960     default:
961       break;
962     }
963 }
964
965 static char *
966 gldppcmacos_get_script(isfile)
967      int *isfile;
968 {                            
969   *isfile = 0;
970
971   if (link_info.relocateable == true && config.build_constructors == true)
972     return
973 "OUTPUT_FORMAT(\"xcoff-powermac\")\n\
974 OUTPUT_ARCH(powerpc)\n\
975 ENTRY(__start)\n\
976 SECTIONS\n\
977 {\n\
978   .pad 0 : { *(.pad) }\n\
979   .text 0 : {\n\
980     *(.text)\n\
981     *(.pr)\n\
982     *(.ro)\n\
983     *(.db)\n\
984     *(.gl)\n\
985     *(.xo)\n\
986     *(.ti)\n\
987     *(.tb)\n\
988   }\n\
989   .data 0 : {\n\
990     *(.data)\n\
991     *(.rw)\n\
992     *(.sv)\n\
993     *(.ua)\n\
994     . = ALIGN(4);\n\
995     CONSTRUCTORS\n\
996     *(.ds)\n\
997     *(.tc0)\n\
998     *(.tc)\n\
999     *(.td)\n\
1000   }\n\
1001   .bss : {\n\
1002     *(.bss)\n\
1003     *(.bs)\n\
1004     *(.uc)\n\
1005     *(COMMON)\n\
1006   }\n\
1007   .loader 0 : {\n\
1008     *(.loader)\n\
1009   }\n\
1010   .debug 0 : {\n\
1011     *(.debug)\n\
1012   }\n\
1013 }\n\n"
1014   ; else if (link_info.relocateable == true) return
1015 "OUTPUT_FORMAT(\"xcoff-powermac\")\n\
1016 OUTPUT_ARCH(powerpc)\n\
1017 ENTRY(__start)\n\
1018 SECTIONS\n\
1019 {\n\
1020   .pad 0 : { *(.pad) }\n\
1021   .text 0 : {\n\
1022     *(.text)\n\
1023     *(.pr)\n\
1024     *(.ro)\n\
1025     *(.db)\n\
1026     *(.gl)\n\
1027     *(.xo)\n\
1028     *(.ti)\n\
1029     *(.tb)\n\
1030   }\n\
1031   .data 0 : {\n\
1032     *(.data)\n\
1033     *(.rw)\n\
1034     *(.sv)\n\
1035     *(.ua)\n\
1036     . = ALIGN(4);\n\
1037     *(.ds)\n\
1038     *(.tc0)\n\
1039     *(.tc)\n\
1040     *(.td)\n\
1041   }\n\
1042   .bss : {\n\
1043     *(.bss)\n\
1044     *(.bs)\n\
1045     *(.uc)\n\
1046     *(COMMON)\n\
1047   }\n\
1048   .loader 0 : {\n\
1049     *(.loader)\n\
1050   }\n\
1051   .debug 0 : {\n\
1052     *(.debug)\n\
1053   }\n\
1054 }\n\n"
1055   ; else if (!config.text_read_only) return
1056 "OUTPUT_FORMAT(\"xcoff-powermac\")\n\
1057 OUTPUT_ARCH(powerpc)\n\
1058  SEARCH_DIR(/usr/local/powerpc-apple-macos/lib);\n\
1059 ENTRY(__start)\n\
1060 SECTIONS\n\
1061 {\n\
1062   .pad 0 : { *(.pad) }\n\
1063   .text   : {\n\
1064     PROVIDE (_text = .);\n\
1065     *(.text)\n\
1066     *(.pr)\n\
1067     *(.ro)\n\
1068     *(.db)\n\
1069     *(.gl)\n\
1070     *(.xo)\n\
1071     *(.ti)\n\
1072     *(.tb)\n\
1073     PROVIDE (_etext = .);\n\
1074   }\n\
1075   .data 0 : {\n\
1076     PROVIDE (_data = .);\n\
1077     *(.data)\n\
1078     *(.rw)\n\
1079     *(.sv)\n\
1080     *(.ua)\n\
1081     . = ALIGN(4);\n\
1082     CONSTRUCTORS\n\
1083     *(.ds)\n\
1084     *(.tc0)\n\
1085     *(.tc)\n\
1086     *(.td)\n\
1087     PROVIDE (_edata = .);\n\
1088   }\n\
1089   .bss : {\n\
1090     *(.bss)\n\
1091     *(.bs)\n\
1092     *(.uc)\n\
1093     *(COMMON)\n\
1094     PROVIDE (_end = .);\n\
1095     PROVIDE (end = .);\n\
1096   }\n\
1097   .loader 0 : {\n\
1098     *(.loader)\n\
1099   }\n\
1100   .debug 0 : {\n\
1101     *(.debug)\n\
1102   }\n\
1103 }\n\n"
1104   ; else if (!config.magic_demand_paged) return
1105 "OUTPUT_FORMAT(\"xcoff-powermac\")\n\
1106 OUTPUT_ARCH(powerpc)\n\
1107  SEARCH_DIR(/usr/local/powerpc-apple-macos/lib);\n\
1108 ENTRY(__start)\n\
1109 SECTIONS\n\
1110 {\n\
1111   .pad 0 : { *(.pad) }\n\
1112   .text   : {\n\
1113     PROVIDE (_text = .);\n\
1114     *(.text)\n\
1115     *(.pr)\n\
1116     *(.ro)\n\
1117     *(.db)\n\
1118     *(.gl)\n\
1119     *(.xo)\n\
1120     *(.ti)\n\
1121     *(.tb)\n\
1122     PROVIDE (_etext = .);\n\
1123   }\n\
1124   .data 0 : {\n\
1125     PROVIDE (_data = .);\n\
1126     *(.data)\n\
1127     *(.rw)\n\
1128     *(.sv)\n\
1129     *(.ua)\n\
1130     . = ALIGN(4);\n\
1131     CONSTRUCTORS\n\
1132     *(.ds)\n\
1133     *(.tc0)\n\
1134     *(.tc)\n\
1135     *(.td)\n\
1136     PROVIDE (_edata = .);\n\
1137   }\n\
1138   .bss : {\n\
1139     *(.bss)\n\
1140     *(.bs)\n\
1141     *(.uc)\n\
1142     *(COMMON)\n\
1143     PROVIDE (_end = .);\n\
1144     PROVIDE (end = .);\n\
1145   }\n\
1146   .loader 0 : {\n\
1147     *(.loader)\n\
1148   }\n\
1149   .debug 0 : {\n\
1150     *(.debug)\n\
1151   }\n\
1152 }\n\n"
1153   ; else return
1154 "OUTPUT_FORMAT(\"xcoff-powermac\")\n\
1155 OUTPUT_ARCH(powerpc)\n\
1156  SEARCH_DIR(/usr/local/powerpc-apple-macos/lib);\n\
1157 ENTRY(__start)\n\
1158 SECTIONS\n\
1159 {\n\
1160   .pad 0 : { *(.pad) }\n\
1161   .text   : {\n\
1162     PROVIDE (_text = .);\n\
1163     *(.text)\n\
1164     *(.pr)\n\
1165     *(.ro)\n\
1166     *(.db)\n\
1167     *(.gl)\n\
1168     *(.xo)\n\
1169     *(.ti)\n\
1170     *(.tb)\n\
1171     PROVIDE (_etext = .);\n\
1172   }\n\
1173   .data 0 : {\n\
1174     PROVIDE (_data = .);\n\
1175     *(.data)\n\
1176     *(.rw)\n\
1177     *(.sv)\n\
1178     *(.ua)\n\
1179     . = ALIGN(4);\n\
1180     CONSTRUCTORS\n\
1181     *(.ds)\n\
1182     *(.tc0)\n\
1183     *(.tc)\n\
1184     *(.td)\n\
1185     PROVIDE (_edata = .);\n\
1186   }\n\
1187   .bss : {\n\
1188     *(.bss)\n\
1189     *(.bs)\n\
1190     *(.uc)\n\
1191     *(COMMON)\n\
1192     PROVIDE (_end = .);\n\
1193     PROVIDE (end = .);\n\
1194   }\n\
1195   .loader 0 : {\n\
1196     *(.loader)\n\
1197   }\n\
1198   .debug 0 : {\n\
1199     *(.debug)\n\
1200   }\n\
1201 }\n\n"
1202 ; }
1203
1204 struct ld_emulation_xfer_struct ld_ppcmacos_emulation = 
1205 {
1206   gldppcmacos_before_parse,
1207   syslib_default,
1208   hll_default,
1209   after_parse_default,
1210   gldppcmacos_after_open,
1211   after_allocation_default,
1212   set_output_arch_default,
1213   ldemul_default_target,
1214   gldppcmacos_before_allocation,
1215   gldppcmacos_get_script,
1216   "ppcmacos",
1217   "xcoff-powermac",
1218   0,    /* finish */
1219   0,    /* create_output_section_statements */
1220   0,    /* open_dynamic_archive */
1221   0,    /* place_orphan */
1222   0,    /* set_symbols */
1223   gldppcmacos_parse_args,
1224   gldppcmacos_unrecognized_file
1225 };