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