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