]>
Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | # This shell script emits a C file. -*- C -*- |
2 | # It does some substitutions. | |
86af25fe L |
3 | if [ -z "$MACHINE" ]; then |
4 | OUTPUT_ARCH=${ARCH} | |
5 | else | |
6 | OUTPUT_ARCH=${ARCH}:${MACHINE} | |
7 | fi | |
252b5132 RH |
8 | cat >e${EMULATION_NAME}.c <<EOF |
9 | /* This file is is generated by a shell script. DO NOT EDIT! */ | |
10 | ||
11 | /* AIX emulation code for ${EMULATION_NAME} | |
aef6203b AM |
12 | Copyright 1991, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
13 | 2003, 2004 | |
8529105a | 14 | Free Software Foundation, Inc. |
252b5132 RH |
15 | Written by Steve Chamberlain <[email protected]> |
16 | AIX support by Ian Lance Taylor <[email protected]> | |
3b1b01cf | 17 | AIX 64 bit support by Tom Rix <[email protected]> |
252b5132 RH |
18 | |
19 | This file is part of GLD, the Gnu Linker. | |
20 | ||
21 | This program is free software; you can redistribute it and/or modify | |
22 | it under the terms of the GNU General Public License as published by | |
23 | the Free Software Foundation; either version 2 of the License, or | |
24 | (at your option) any later version. | |
25 | ||
26 | This program is distributed in the hope that it will be useful, | |
27 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
28 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
29 | GNU General Public License for more details. | |
30 | ||
31 | You should have received a copy of the GNU General Public License | |
32 | along with this program; if not, write to the Free Software | |
33 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
34 | ||
35 | #define TARGET_IS_${EMULATION_NAME} | |
36 | ||
37 | #include "bfd.h" | |
38 | #include "sysdep.h" | |
39 | #include "libiberty.h" | |
3882b010 | 40 | #include "safe-ctype.h" |
252b5132 RH |
41 | #include "getopt.h" |
42 | #include "obstack.h" | |
43 | #include "bfdlink.h" | |
44 | ||
252b5132 RH |
45 | #include "ld.h" |
46 | #include "ldmain.h" | |
252b5132 RH |
47 | #include "ldmisc.h" |
48 | #include "ldexp.h" | |
49 | #include "ldlang.h" | |
20713691 AM |
50 | #include "ldfile.h" |
51 | #include "ldemul.h" | |
252b5132 | 52 | #include "ldctor.h" |
df2a7313 | 53 | #include <ldgram.h> |
252b5132 | 54 | |
3b1b01cf TR |
55 | #include "coff/internal.h" |
56 | #include "coff/xcoff.h" | |
f1f0d9ab TR |
57 | #include "libcoff.h" |
58 | #include "libxcoff.h" | |
3b1b01cf | 59 | |
0c7a8e5a AM |
60 | static void gld${EMULATION_NAME}_read_file (const char *, bfd_boolean); |
61 | static void gld${EMULATION_NAME}_free (void *); | |
62 | static void gld${EMULATION_NAME}_find_relocs (lang_statement_union_type *); | |
63 | static void gld${EMULATION_NAME}_find_exp_assignment (etree_type *); | |
64 | ||
252b5132 RH |
65 | |
66 | /* The file alignment required for each section. */ | |
67 | static unsigned long file_align; | |
68 | ||
69 | /* The maximum size the stack is permitted to grow. This is stored in | |
70 | the a.out header. */ | |
71 | static unsigned long maxstack; | |
72 | ||
73 | /* The maximum data size. This is stored in the a.out header. */ | |
74 | static unsigned long maxdata; | |
75 | ||
76 | /* Whether to perform garbage collection. */ | |
77 | static int gc = 1; | |
78 | ||
79 | /* The module type to use. */ | |
80 | static unsigned short modtype = ('1' << 8) | 'L'; | |
81 | ||
82 | /* Whether the .text section must be read-only (i.e., no relocs | |
83 | permitted). */ | |
84 | static int textro; | |
85 | ||
86 | /* Whether to implement Unix like linker semantics. */ | |
87 | static int unix_ld; | |
88 | ||
89 | /* Structure used to hold import file list. */ | |
90 | ||
91 | struct filelist | |
92 | { | |
93 | struct filelist *next; | |
94 | const char *name; | |
95 | }; | |
96 | ||
97 | /* List of import files. */ | |
98 | static struct filelist *import_files; | |
99 | ||
100 | /* List of export symbols read from the export files. */ | |
101 | ||
102 | struct export_symbol_list | |
103 | { | |
104 | struct export_symbol_list *next; | |
105 | const char *name; | |
252b5132 RH |
106 | }; |
107 | ||
108 | static struct export_symbol_list *export_symbols; | |
109 | ||
3b1b01cf TR |
110 | /* Maintains the 32 or 64 bit mode state of import file */ |
111 | static unsigned int symbol_mode = 0x04; | |
112 | ||
742aeb63 TR |
113 | /* Which symbol modes are valid */ |
114 | static unsigned int symbol_mode_mask = 0x0d; | |
115 | ||
116 | /* Whether this is a 64 bit link */ | |
117 | static int is_64bit = 0; | |
118 | ||
119 | /* Which syscalls from import file are valid */ | |
120 | static unsigned int syscall_mask = 0x77; | |
121 | ||
9a4c7f16 TR |
122 | /* fake file for -binitfini support */ |
123 | static lang_input_statement_type *initfini_file; | |
69f284c7 | 124 | |
7fc44b9c | 125 | /* Whether to do run time linking |
ca10c62d | 126 | -brtl enables, -bnortl and -bnortllib disable. */ |
f1f0d9ab | 127 | static int rtld; |
69f284c7 | 128 | |
aa67bccf TR |
129 | /* Explicit command line library path, -blibpath */ |
130 | static char *command_line_blibpath = NULL; | |
131 | ||
252b5132 RH |
132 | /* This routine is called before anything else is done. */ |
133 | ||
134 | static void | |
0c7a8e5a | 135 | gld${EMULATION_NAME}_before_parse (void) |
252b5132 | 136 | { |
5e2f1575 | 137 | ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`); |
f1f0d9ab | 138 | |
b34976b6 | 139 | config.has_shared = TRUE; |
3b1b01cf | 140 | |
9a4c7f16 TR |
141 | /* The link_info.[init|fini]_functions are initialized in ld/lexsup.c. |
142 | Override them here so we can use the link_info.init_function as a | |
7fc44b9c | 143 | state flag that lets the backend know that -binitfini has been done. */ |
9a4c7f16 | 144 | |
3b1b01cf TR |
145 | link_info.init_function = NULL; |
146 | link_info.fini_function = NULL; | |
252b5132 RH |
147 | } |
148 | ||
149 | /* Handle AIX specific options. */ | |
150 | ||
3bcf5557 | 151 | enum |
249172c3 | 152 | { |
3b1b01cf | 153 | OPTION_IGNORE = 300, |
fab80407 AM |
154 | OPTION_AUTOIMP, |
155 | OPTION_ERNOTOK, | |
156 | OPTION_EROK, | |
157 | OPTION_EXPORT, | |
158 | OPTION_IMPORT, | |
159 | OPTION_INITFINI, | |
160 | OPTION_LOADMAP, | |
161 | OPTION_MAXDATA, | |
162 | OPTION_MAXSTACK, | |
163 | OPTION_MODTYPE, | |
164 | OPTION_NOAUTOIMP, | |
165 | OPTION_NOSTRCMPCT, | |
166 | OPTION_PD, | |
167 | OPTION_PT, | |
168 | OPTION_STRCMPCT, | |
742aeb63 TR |
169 | OPTION_UNIX, |
170 | OPTION_32, | |
171 | OPTION_64, | |
aa67bccf TR |
172 | OPTION_LIBPATH, |
173 | OPTION_NOLIBPATH, | |
3b1b01cf TR |
174 | }; |
175 | ||
3bcf5557 | 176 | static void |
0c7a8e5a AM |
177 | gld${EMULATION_NAME}_add_options |
178 | (int ns, char **shortopts, int nl, struct option **longopts, | |
179 | int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED) | |
3bcf5557 AM |
180 | { |
181 | static const char xtra_short[] = "D:H:KT:z"; | |
182 | static const struct option xtra_long[] = { | |
9a4c7f16 TR |
183 | /* -binitfini has special handling in the linker backend. The native linker |
184 | uses the arguemnts to generate a table of init and fini functions for | |
185 | the executable. The important use for this option is to support aix 4.2+ | |
186 | c++ constructors and destructors. This is tied into gcc via collect2.c. | |
7fc44b9c | 187 | |
9a4c7f16 TR |
188 | The function table is accessed by the runtime linker/loader by checking if |
189 | the first symbol in the loader symbol table is __rtinit. The gnu linker | |
190 | generates this symbol and makes it the first loader symbol. */ | |
3b1b01cf | 191 | |
252b5132 RH |
192 | {"basis", no_argument, NULL, OPTION_IGNORE}, |
193 | {"bautoimp", no_argument, NULL, OPTION_AUTOIMP}, | |
194 | {"bcomprld", no_argument, NULL, OPTION_IGNORE}, | |
195 | {"bcrld", no_argument, NULL, OPTION_IGNORE}, | |
196 | {"bcror31", no_argument, NULL, OPTION_IGNORE}, | |
197 | {"bD", required_argument, NULL, OPTION_MAXDATA}, | |
198 | {"bE", required_argument, NULL, OPTION_EXPORT}, | |
199 | {"bernotok", no_argument, NULL, OPTION_ERNOTOK}, | |
200 | {"berok", no_argument, NULL, OPTION_EROK}, | |
201 | {"berrmsg", no_argument, NULL, OPTION_IGNORE}, | |
202 | {"bexport", required_argument, NULL, OPTION_EXPORT}, | |
203 | {"bf", no_argument, NULL, OPTION_ERNOTOK}, | |
204 | {"bgc", no_argument, &gc, 1}, | |
205 | {"bh", required_argument, NULL, OPTION_IGNORE}, | |
206 | {"bhalt", required_argument, NULL, OPTION_IGNORE}, | |
207 | {"bI", required_argument, NULL, OPTION_IMPORT}, | |
208 | {"bimport", required_argument, NULL, OPTION_IMPORT}, | |
3b1b01cf | 209 | {"binitfini", required_argument, NULL, OPTION_INITFINI}, |
252b5132 RH |
210 | {"bl", required_argument, NULL, OPTION_LOADMAP}, |
211 | {"bloadmap", required_argument, NULL, OPTION_LOADMAP}, | |
212 | {"bmaxdata", required_argument, NULL, OPTION_MAXDATA}, | |
213 | {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK}, | |
214 | {"bM", required_argument, NULL, OPTION_MODTYPE}, | |
215 | {"bmodtype", required_argument, NULL, OPTION_MODTYPE}, | |
216 | {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP}, | |
217 | {"bnodelcsect", no_argument, NULL, OPTION_IGNORE}, | |
218 | {"bnoentry", no_argument, NULL, OPTION_IGNORE}, | |
219 | {"bnogc", no_argument, &gc, 0}, | |
220 | {"bnso", no_argument, NULL, OPTION_NOAUTOIMP}, | |
221 | {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT}, | |
222 | {"bnotextro", no_argument, &textro, 0}, | |
223 | {"bnro", no_argument, &textro, 0}, | |
224 | {"bpD", required_argument, NULL, OPTION_PD}, | |
225 | {"bpT", required_argument, NULL, OPTION_PT}, | |
226 | {"bro", no_argument, &textro, 1}, | |
69f284c7 | 227 | {"brtl", no_argument, &rtld, 1}, |
ca10c62d TR |
228 | {"bnortl", no_argument, &rtld, 0}, |
229 | {"bnortllib", no_argument, &rtld, 0}, | |
252b5132 RH |
230 | {"bS", required_argument, NULL, OPTION_MAXSTACK}, |
231 | {"bso", no_argument, NULL, OPTION_AUTOIMP}, | |
232 | {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT}, | |
233 | {"btextro", no_argument, &textro, 1}, | |
742aeb63 TR |
234 | {"b32", no_argument, NULL, OPTION_32}, |
235 | {"b64", no_argument, NULL, OPTION_64}, | |
252b5132 RH |
236 | {"static", no_argument, NULL, OPTION_NOAUTOIMP}, |
237 | {"unix", no_argument, NULL, OPTION_UNIX}, | |
aa67bccf TR |
238 | {"blibpath", required_argument, NULL, OPTION_LIBPATH}, |
239 | {"bnolibpath", required_argument, NULL, OPTION_NOLIBPATH}, | |
252b5132 RH |
240 | {NULL, no_argument, NULL, 0} |
241 | }; | |
242 | ||
243 | /* Options supported by the AIX linker which we do not support: -f, | |
244 | -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps, | |
245 | -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl, | |
246 | -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl, | |
247 | -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink, | |
248 | -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder, | |
249 | -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk, | |
250 | -bx, -bX, -bxref. */ | |
251 | ||
3bcf5557 AM |
252 | *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short)); |
253 | memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short)); | |
0c7a8e5a AM |
254 | *longopts = xrealloc (*longopts, |
255 | nl * sizeof (struct option) + sizeof (xtra_long)); | |
3bcf5557 AM |
256 | memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long)); |
257 | } | |
258 | ||
259 | static bfd_boolean | |
0c7a8e5a | 260 | gld${EMULATION_NAME}_parse_args (int argc, char **argv) |
3bcf5557 AM |
261 | { |
262 | int indx; | |
263 | ||
252b5132 RH |
264 | /* If the current option starts with -b, change the first : to an =. |
265 | The AIX linker uses : to separate the option from the argument; | |
266 | changing it to = lets us treat it as a getopt option. */ | |
267 | indx = optind; | |
fab80407 | 268 | if (indx == 0) |
249172c3 | 269 | indx = 1; |
fab80407 AM |
270 | |
271 | if (indx < argc && strncmp (argv[indx], "-b", 2) == 0) | |
272 | { | |
273 | char *s; | |
274 | ||
275 | for (s = argv[indx]; *s != '\0'; s++) | |
276 | { | |
277 | if (*s == ':') | |
278 | { | |
279 | *s = '='; | |
280 | break; | |
281 | } | |
282 | } | |
252b5132 | 283 | } |
3bcf5557 AM |
284 | return FALSE; |
285 | } | |
3b1b01cf | 286 | |
3bcf5557 | 287 | static bfd_boolean |
0c7a8e5a | 288 | gld${EMULATION_NAME}_handle_option (int optc) |
3bcf5557 AM |
289 | { |
290 | bfd_signed_vma val; | |
291 | const char *end; | |
252b5132 RH |
292 | |
293 | switch (optc) | |
294 | { | |
252b5132 | 295 | default: |
3bcf5557 | 296 | return FALSE; |
252b5132 RH |
297 | |
298 | case 0: | |
299 | /* Long option which just sets a flag. */ | |
300 | break; | |
301 | ||
302 | case 'D': | |
c89e8944 | 303 | val = bfd_scan_vma (optarg, &end, 0); |
252b5132 RH |
304 | if (*end != '\0') |
305 | einfo ("%P: warning: ignoring invalid -D number %s\n", optarg); | |
306 | else if (val != -1) | |
7a5df8d0 | 307 | lang_section_start (".data", exp_intop (val), NULL); |
252b5132 RH |
308 | break; |
309 | ||
310 | case 'H': | |
c89e8944 | 311 | val = bfd_scan_vma (optarg, &end, 0); |
249172c3 | 312 | if (*end != '\0' || (val & (val - 1)) != 0) |
252b5132 RH |
313 | einfo ("%P: warning: ignoring invalid -H number %s\n", optarg); |
314 | else | |
315 | file_align = val; | |
316 | break; | |
317 | ||
318 | case 'K': | |
319 | case 'z': | |
320 | /* FIXME: This should use the page size for the target system. */ | |
321 | file_align = 4096; | |
322 | break; | |
323 | ||
324 | case 'T': | |
325 | /* On AIX this is the same as GNU ld -Ttext. When we see -T | |
7fc44b9c AM |
326 | number, we assume the AIX option is intended. Otherwise, we |
327 | assume the usual GNU ld -T option is intended. We can't just | |
328 | ignore the AIX option, because gcc passes it to the linker. */ | |
c89e8944 | 329 | val = bfd_scan_vma (optarg, &end, 0); |
252b5132 | 330 | if (*end != '\0') |
3bcf5557 | 331 | return FALSE; |
7a5df8d0 | 332 | lang_section_start (".text", exp_intop (val), NULL); |
252b5132 RH |
333 | break; |
334 | ||
335 | case OPTION_IGNORE: | |
336 | break; | |
337 | ||
fab80407 AM |
338 | case OPTION_INITFINI: |
339 | { | |
340 | /* | |
341 | * The aix linker init fini has the format : | |
342 | * | |
343 | * -binitfini:[ Initial][:Termination][:Priority] | |
344 | * | |
345 | * it allows the Termination and Priority to be optional. | |
346 | * | |
347 | * Since we support only one init/fini pair, we ignore the Priority. | |
348 | * | |
349 | * Define the special symbol __rtinit. | |
350 | * | |
351 | * strtok does not correctly handle the case of -binitfini::fini: so | |
352 | * do it by hand | |
353 | */ | |
354 | char *t, *i, *f; | |
355 | ||
356 | i = t = optarg; | |
357 | while (*t && ':' != *t) | |
358 | t++; | |
359 | if (*t) | |
360 | *t++ = 0; | |
361 | ||
362 | if (0 != strlen (i)) | |
249172c3 | 363 | link_info.init_function = i; |
3b1b01cf | 364 | |
fab80407 AM |
365 | f = t; |
366 | while (*t && ':' != *t) | |
367 | t++; | |
368 | *t = 0; | |
3b1b01cf | 369 | |
fab80407 | 370 | if (0 != strlen (f)) |
249172c3 | 371 | link_info.fini_function = f; |
3b1b01cf | 372 | } |
249172c3 | 373 | break; |
7fc44b9c | 374 | |
252b5132 | 375 | case OPTION_AUTOIMP: |
b34976b6 | 376 | link_info.static_link = FALSE; |
252b5132 RH |
377 | break; |
378 | ||
379 | case OPTION_ERNOTOK: | |
b34976b6 | 380 | force_make_executable = FALSE; |
252b5132 RH |
381 | break; |
382 | ||
383 | case OPTION_EROK: | |
b34976b6 | 384 | force_make_executable = TRUE; |
252b5132 RH |
385 | break; |
386 | ||
387 | case OPTION_EXPORT: | |
b34976b6 | 388 | gld${EMULATION_NAME}_read_file (optarg, FALSE); |
252b5132 RH |
389 | break; |
390 | ||
391 | case OPTION_IMPORT: | |
392 | { | |
393 | struct filelist *n; | |
394 | struct filelist **flpp; | |
395 | ||
396 | n = (struct filelist *) xmalloc (sizeof (struct filelist)); | |
397 | n->next = NULL; | |
398 | n->name = optarg; | |
399 | flpp = &import_files; | |
400 | while (*flpp != NULL) | |
401 | flpp = &(*flpp)->next; | |
402 | *flpp = n; | |
403 | } | |
404 | break; | |
405 | ||
406 | case OPTION_LOADMAP: | |
407 | config.map_filename = optarg; | |
408 | break; | |
409 | ||
410 | case OPTION_MAXDATA: | |
c89e8944 | 411 | val = bfd_scan_vma (optarg, &end, 0); |
252b5132 | 412 | if (*end != '\0') |
fab80407 | 413 | einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n", optarg); |
252b5132 RH |
414 | else |
415 | maxdata = val; | |
416 | break; | |
417 | ||
418 | case OPTION_MAXSTACK: | |
c89e8944 | 419 | val = bfd_scan_vma (optarg, &end, 0); |
252b5132 | 420 | if (*end != '\0') |
7fc44b9c | 421 | einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n", |
249172c3 | 422 | optarg); |
252b5132 RH |
423 | else |
424 | maxstack = val; | |
425 | break; | |
426 | ||
427 | case OPTION_MODTYPE: | |
428 | if (*optarg == 'S') | |
429 | { | |
b34976b6 | 430 | link_info.shared = TRUE; |
252b5132 RH |
431 | ++optarg; |
432 | } | |
433 | if (*optarg == '\0' || optarg[1] == '\0') | |
434 | einfo ("%P: warning: ignoring invalid module type %s\n", optarg); | |
435 | else | |
436 | modtype = (*optarg << 8) | optarg[1]; | |
437 | break; | |
438 | ||
439 | case OPTION_NOAUTOIMP: | |
b34976b6 | 440 | link_info.static_link = TRUE; |
252b5132 RH |
441 | break; |
442 | ||
443 | case OPTION_NOSTRCMPCT: | |
b34976b6 | 444 | link_info.traditional_format = TRUE; |
252b5132 RH |
445 | break; |
446 | ||
447 | case OPTION_PD: | |
448 | /* This sets the page that the .data section is supposed to | |
7fc44b9c AM |
449 | start on. The offset within the page should still be the |
450 | offset within the file, so we need to build an appropriate | |
451 | expression. */ | |
c89e8944 | 452 | val = bfd_scan_vma (optarg, &end, 0); |
252b5132 RH |
453 | if (*end != '\0') |
454 | einfo ("%P: warning: ignoring invalid -pD number %s\n", optarg); | |
455 | else | |
456 | { | |
457 | etree_type *t; | |
458 | ||
459 | t = exp_binop ('+', | |
460 | exp_intop (val), | |
461 | exp_binop ('&', | |
462 | exp_nameop (NAME, "."), | |
463 | exp_intop (0xfff))); | |
464 | t = exp_binop ('&', | |
465 | exp_binop ('+', t, exp_intop (31)), | |
249172c3 | 466 | exp_intop (~(bfd_vma) 31)); |
7a5df8d0 | 467 | lang_section_start (".data", t, NULL); |
252b5132 RH |
468 | } |
469 | break; | |
470 | ||
471 | case OPTION_PT: | |
472 | /* This set the page that the .text section is supposed to start | |
7fc44b9c AM |
473 | on. The offset within the page should still be the offset |
474 | within the file. */ | |
c89e8944 | 475 | val = bfd_scan_vma (optarg, &end, 0); |
252b5132 RH |
476 | if (*end != '\0') |
477 | einfo ("%P: warning: ignoring invalid -pT number %s\n", optarg); | |
478 | else | |
479 | { | |
480 | etree_type *t; | |
481 | ||
482 | t = exp_binop ('+', | |
483 | exp_intop (val), | |
484 | exp_nameop (SIZEOF_HEADERS, NULL)); | |
485 | t = exp_binop ('&', | |
486 | exp_binop ('+', t, exp_intop (31)), | |
249172c3 | 487 | exp_intop (~(bfd_vma) 31)); |
7a5df8d0 | 488 | lang_section_start (".text", t, NULL); |
252b5132 RH |
489 | } |
490 | break; | |
491 | ||
492 | case OPTION_STRCMPCT: | |
b34976b6 | 493 | link_info.traditional_format = FALSE; |
252b5132 RH |
494 | break; |
495 | ||
496 | case OPTION_UNIX: | |
b34976b6 | 497 | unix_ld = TRUE; |
252b5132 | 498 | break; |
742aeb63 TR |
499 | |
500 | case OPTION_32: | |
501 | is_64bit = 0; | |
502 | syscall_mask = 0x77; | |
503 | symbol_mode_mask = 0x0d; | |
504 | break; | |
505 | ||
506 | case OPTION_64: | |
507 | is_64bit = 1; | |
508 | syscall_mask = 0xcc; | |
509 | symbol_mode_mask = 0x0e; | |
510 | break; | |
511 | ||
aa67bccf | 512 | case OPTION_LIBPATH: |
cc7e59b2 | 513 | command_line_blibpath = optarg; |
aa67bccf TR |
514 | break; |
515 | ||
516 | case OPTION_NOLIBPATH: | |
cc7e59b2 | 517 | command_line_blibpath = NULL; |
aa67bccf TR |
518 | break; |
519 | ||
252b5132 RH |
520 | } |
521 | ||
3bcf5557 | 522 | return TRUE; |
252b5132 RH |
523 | } |
524 | ||
525 | /* This is called when an input file can not be recognized as a BFD | |
526 | object or an archive. If the file starts with #!, we must treat it | |
527 | as an import file. This is for AIX compatibility. */ | |
528 | ||
b34976b6 | 529 | static bfd_boolean |
0c7a8e5a | 530 | gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry) |
252b5132 RH |
531 | { |
532 | FILE *e; | |
b34976b6 | 533 | bfd_boolean ret; |
252b5132 RH |
534 | |
535 | e = fopen (entry->filename, FOPEN_RT); | |
536 | if (e == NULL) | |
b34976b6 | 537 | return FALSE; |
252b5132 | 538 | |
b34976b6 | 539 | ret = FALSE; |
252b5132 RH |
540 | |
541 | if (getc (e) == '#' && getc (e) == '!') | |
542 | { | |
543 | struct filelist *n; | |
544 | struct filelist **flpp; | |
545 | ||
546 | n = (struct filelist *) xmalloc (sizeof (struct filelist)); | |
547 | n->next = NULL; | |
548 | n->name = entry->filename; | |
549 | flpp = &import_files; | |
550 | while (*flpp != NULL) | |
551 | flpp = &(*flpp)->next; | |
552 | *flpp = n; | |
553 | ||
b34976b6 AM |
554 | ret = TRUE; |
555 | entry->loaded = TRUE; | |
252b5132 RH |
556 | } |
557 | ||
558 | fclose (e); | |
559 | ||
560 | return ret; | |
561 | } | |
562 | ||
563 | /* This is called after the input files have been opened. */ | |
564 | ||
565 | static void | |
0c7a8e5a | 566 | gld${EMULATION_NAME}_after_open (void) |
252b5132 | 567 | { |
b34976b6 | 568 | bfd_boolean r; |
252b5132 RH |
569 | struct set_info *p; |
570 | ||
571 | /* Call ldctor_build_sets, after pretending that this is a | |
1049f94e | 572 | relocatable link. We do this because AIX requires relocation |
252b5132 RH |
573 | entries for all references to symbols, even in a final |
574 | executable. Of course, we only want to do this if we are | |
575 | producing an XCOFF output file. */ | |
1049f94e | 576 | r = link_info.relocatable; |
252b5132 | 577 | if (strstr (bfd_get_target (output_bfd), "xcoff") != NULL) |
1049f94e | 578 | link_info.relocatable = TRUE; |
252b5132 | 579 | ldctor_build_sets (); |
1049f94e | 580 | link_info.relocatable = r; |
252b5132 RH |
581 | |
582 | /* For each set, record the size, so that the XCOFF backend can | |
583 | output the correct csect length. */ | |
584 | for (p = sets; p != (struct set_info *) NULL; p = p->next) | |
585 | { | |
586 | bfd_size_type size; | |
587 | ||
588 | /* If the symbol is defined, we may have been invoked from | |
7fc44b9c AM |
589 | collect, and the sets may already have been built, so we do |
590 | not do anything. */ | |
252b5132 RH |
591 | if (p->h->type == bfd_link_hash_defined |
592 | || p->h->type == bfd_link_hash_defweak) | |
593 | continue; | |
594 | ||
595 | if (p->reloc != BFD_RELOC_CTOR) | |
596 | { | |
597 | /* Handle this if we need to. */ | |
598 | abort (); | |
599 | } | |
600 | ||
601 | size = (p->count + 2) * 4; | |
249172c3 | 602 | if (!bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size)) |
252b5132 RH |
603 | einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n"); |
604 | } | |
605 | } | |
606 | ||
607 | /* This is called after the sections have been attached to output | |
608 | sections, but before any sizes or addresses have been set. */ | |
609 | ||
610 | static void | |
0c7a8e5a | 611 | gld${EMULATION_NAME}_before_allocation (void) |
252b5132 RH |
612 | { |
613 | struct filelist *fl; | |
614 | struct export_symbol_list *el; | |
615 | char *libpath; | |
3b1b01cf | 616 | asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS]; |
252b5132 RH |
617 | int i; |
618 | ||
619 | /* Handle the import and export files, if any. */ | |
620 | for (fl = import_files; fl != NULL; fl = fl->next) | |
b34976b6 | 621 | gld${EMULATION_NAME}_read_file (fl->name, TRUE); |
fab80407 AM |
622 | for (el = export_symbols; el != NULL; el = el->next) |
623 | { | |
624 | struct bfd_link_hash_entry *h; | |
625 | ||
b34976b6 | 626 | h = bfd_link_hash_lookup (link_info.hash, el->name, FALSE, FALSE, FALSE); |
fab80407 AM |
627 | if (h == NULL) |
628 | einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n"); | |
249172c3 | 629 | if (!bfd_xcoff_export_symbol (output_bfd, &link_info, h)) |
fab80407 AM |
630 | einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n"); |
631 | } | |
252b5132 RH |
632 | |
633 | /* Track down all relocations called for by the linker script (these | |
634 | are typically constructor/destructor entries created by | |
635 | CONSTRUCTORS) and let the backend know it will need to create | |
636 | .loader relocs for them. */ | |
637 | lang_for_each_statement (gld${EMULATION_NAME}_find_relocs); | |
638 | ||
aa67bccf | 639 | /* Precedence of LIBPATH |
7fc44b9c AM |
640 | -blibpath: native support always first |
641 | -rpath: gnu extension | |
642 | -L build from command line -L's */ | |
aa67bccf TR |
643 | if (command_line_blibpath != NULL) |
644 | libpath = command_line_blibpath; | |
645 | else if (command_line.rpath != NULL) | |
252b5132 RH |
646 | libpath = command_line.rpath; |
647 | else if (search_head == NULL) | |
648 | libpath = (char *) ""; | |
649 | else | |
650 | { | |
651 | size_t len; | |
652 | search_dirs_type *search; | |
653 | ||
654 | len = strlen (search_head->name); | |
655 | libpath = xmalloc (len + 1); | |
656 | strcpy (libpath, search_head->name); | |
657 | for (search = search_head->next; search != NULL; search = search->next) | |
658 | { | |
659 | size_t nlen; | |
660 | ||
661 | nlen = strlen (search->name); | |
662 | libpath = xrealloc (libpath, len + nlen + 2); | |
663 | libpath[len] = ':'; | |
664 | strcpy (libpath + len + 1, search->name); | |
665 | len += nlen + 1; | |
666 | } | |
667 | } | |
668 | ||
669 | /* Let the XCOFF backend set up the .loader section. */ | |
7fc44b9c | 670 | if (!bfd_xcoff_size_dynamic_sections |
e3e942e9 | 671 | (output_bfd, &link_info, libpath, entry_symbol.name, file_align, |
b34976b6 AM |
672 | maxstack, maxdata, gc && !unix_ld ? TRUE : FALSE, |
673 | modtype, textro ? TRUE : FALSE, unix_ld, special_sections, | |
674 | rtld ? TRUE : FALSE)) | |
252b5132 RH |
675 | einfo ("%P%F: failed to set dynamic section sizes: %E\n"); |
676 | ||
677 | /* Look through the special sections, and put them in the right | |
678 | place in the link ordering. This is especially magic. */ | |
fab80407 AM |
679 | for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++) |
680 | { | |
681 | asection *sec; | |
682 | lang_output_section_statement_type *os; | |
683 | lang_statement_union_type **pls; | |
684 | lang_input_section_type *is; | |
685 | const char *oname; | |
b34976b6 | 686 | bfd_boolean start; |
fab80407 AM |
687 | |
688 | sec = special_sections[i]; | |
689 | if (sec == NULL) | |
690 | continue; | |
3b1b01cf | 691 | |
fab80407 | 692 | /* Remove this section from the list of the output section. |
7fc44b9c | 693 | This assumes we know what the script looks like. */ |
fab80407 AM |
694 | is = NULL; |
695 | os = lang_output_section_find (sec->output_section->name); | |
7fc44b9c | 696 | if (os == NULL) |
fab80407 AM |
697 | einfo ("%P%F: can't find output section %s\n", |
698 | sec->output_section->name); | |
fab80407 AM |
699 | |
700 | for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->header.next) | |
701 | { | |
7fc44b9c | 702 | if ((*pls)->header.type == lang_input_section_enum |
249172c3 | 703 | && (*pls)->input_section.section == sec) |
fab80407 | 704 | { |
249172c3 | 705 | is = (lang_input_section_type *) * pls; |
fab80407 | 706 | *pls = (*pls)->header.next; |
252b5132 | 707 | break; |
fab80407 AM |
708 | } |
709 | ||
710 | if ((*pls)->header.type == lang_wild_statement_enum) | |
711 | { | |
712 | lang_statement_union_type **pwls; | |
713 | ||
714 | for (pwls = &(*pls)->wild_statement.children.head; | |
249172c3 | 715 | *pwls != NULL; pwls = &(*pwls)->header.next) |
fab80407 AM |
716 | { |
717 | ||
7fc44b9c | 718 | if ((*pwls)->header.type == lang_input_section_enum |
249172c3 | 719 | && (*pwls)->input_section.section == sec) |
fab80407 | 720 | { |
249172c3 | 721 | is = (lang_input_section_type *) * pwls; |
fab80407 AM |
722 | *pwls = (*pwls)->header.next; |
723 | break; | |
724 | } | |
725 | } | |
726 | ||
727 | if (is != NULL) | |
728 | break; | |
729 | } | |
730 | } | |
731 | ||
732 | if (is == NULL) | |
733 | { | |
734 | einfo ("%P%F: can't find %s in output section\n", | |
735 | bfd_get_section_name (sec->owner, sec)); | |
3b1b01cf | 736 | } |
fab80407 AM |
737 | |
738 | /* Now figure out where the section should go. */ | |
739 | switch (i) | |
740 | { | |
741 | ||
249172c3 | 742 | default: /* to avoid warnings */ |
fab80407 AM |
743 | case XCOFF_SPECIAL_SECTION_TEXT: |
744 | /* _text */ | |
745 | oname = ".text"; | |
b34976b6 | 746 | start = TRUE; |
3b1b01cf | 747 | break; |
252b5132 | 748 | |
fab80407 AM |
749 | case XCOFF_SPECIAL_SECTION_ETEXT: |
750 | /* _etext */ | |
751 | oname = ".text"; | |
b34976b6 | 752 | start = FALSE; |
fab80407 | 753 | break; |
252b5132 | 754 | |
fab80407 AM |
755 | case XCOFF_SPECIAL_SECTION_DATA: |
756 | /* _data */ | |
757 | oname = ".data"; | |
b34976b6 | 758 | start = TRUE; |
fab80407 | 759 | break; |
252b5132 | 760 | |
fab80407 AM |
761 | case XCOFF_SPECIAL_SECTION_EDATA: |
762 | /* _edata */ | |
763 | oname = ".data"; | |
b34976b6 | 764 | start = FALSE; |
fab80407 AM |
765 | break; |
766 | ||
767 | case XCOFF_SPECIAL_SECTION_END: | |
768 | case XCOFF_SPECIAL_SECTION_END2: | |
769 | /* _end and end */ | |
770 | oname = ".bss"; | |
b34976b6 | 771 | start = FALSE; |
fab80407 AM |
772 | break; |
773 | } | |
252b5132 | 774 | |
fab80407 | 775 | os = lang_output_section_find (oname); |
252b5132 | 776 | |
fab80407 AM |
777 | if (start) |
778 | { | |
779 | is->header.next = os->children.head; | |
780 | os->children.head = (lang_statement_union_type *) is; | |
781 | } | |
782 | else | |
783 | { | |
784 | is->header.next = NULL; | |
785 | lang_statement_append (&os->children, | |
786 | (lang_statement_union_type *) is, | |
787 | &is->header.next); | |
788 | } | |
3b1b01cf | 789 | } |
3b1b01cf TR |
790 | } |
791 | ||
742aeb63 | 792 | static char * |
0c7a8e5a | 793 | gld${EMULATION_NAME}_choose_target (int argc, char **argv) |
742aeb63 TR |
794 | { |
795 | int i, j, jmax; | |
796 | static char *from_outside; | |
797 | static char *from_inside; | |
7fc44b9c | 798 | static char *argv_to_target[][2] = { |
249172c3 | 799 | {NULL, "${OUTPUT_FORMAT}"}, |
eb1e0e80 NC |
800 | {"-b32", "${OUTPUT_FORMAT_32BIT}"}, |
801 | {"-b64", "${OUTPUT_FORMAT_64BIT}"}, | |
249172c3 | 802 | }; |
742aeb63 TR |
803 | |
804 | jmax = 3; | |
805 | ||
806 | from_outside = getenv (TARGET_ENVIRON); | |
249172c3 | 807 | if (from_outside != (char *) NULL) |
742aeb63 TR |
808 | return from_outside; |
809 | ||
810 | /* Set to default. */ | |
811 | from_inside = argv_to_target[0][1]; | |
812 | for (i = 1; i < argc; i++) | |
813 | { | |
7fc44b9c | 814 | for (j = 1; j < jmax; j++) |
742aeb63 TR |
815 | { |
816 | if (0 == strcmp (argv[i], argv_to_target[j][0])) | |
817 | from_inside = argv_to_target[j][1]; | |
818 | } | |
819 | } | |
249172c3 | 820 | |
742aeb63 TR |
821 | return from_inside; |
822 | } | |
823 | ||
7fc44b9c | 824 | /* Returns |
249172c3 TR |
825 | 1 : state changed |
826 | 0 : no change */ | |
7fc44b9c | 827 | static int |
0c7a8e5a | 828 | change_symbol_mode (char *input) |
3b1b01cf | 829 | { |
3b1b01cf | 830 | char *symbol_mode_string[] = { |
249172c3 TR |
831 | "# 32", /* 0x01 */ |
832 | "# 64", /* 0x02 */ | |
833 | "# no32", /* 0x04 */ | |
834 | "# no64", /* 0x08 */ | |
3b1b01cf TR |
835 | NULL, |
836 | }; | |
249172c3 | 837 | |
3b1b01cf TR |
838 | unsigned int bit; |
839 | char *string; | |
840 | ||
249172c3 | 841 | for (bit = 0;; bit++) |
fab80407 AM |
842 | { |
843 | string = symbol_mode_string[bit]; | |
249172c3 | 844 | if (string == NULL) |
fab80407 AM |
845 | return 0; |
846 | ||
847 | if (0 == strcmp (input, string)) | |
848 | { | |
849 | symbol_mode = (1 << bit); | |
850 | return 1; | |
851 | } | |
3b1b01cf | 852 | } |
3b1b01cf TR |
853 | /* should not be here */ |
854 | return 0; | |
855 | } | |
856 | ||
7fc44b9c | 857 | /* Returns |
249172c3 TR |
858 | 1 : yes |
859 | 0 : ignore | |
860 | -1 : error, try something else */ | |
7fc44b9c | 861 | static int |
0c7a8e5a | 862 | is_syscall (char *input, unsigned int *flag) |
3b1b01cf | 863 | { |
3b1b01cf TR |
864 | unsigned int bit; |
865 | char *string; | |
7fc44b9c | 866 | |
1fdf0249 TR |
867 | struct sc { |
868 | char *syscall_string; | |
869 | unsigned int flag; | |
870 | } s [] = { | |
871 | { "svc" /* 0x01 */, XCOFF_SYSCALL32 }, | |
872 | { "svc32" /* 0x02 */, XCOFF_SYSCALL32 }, | |
873 | { "svc3264" /* 0x04 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 }, | |
874 | { "svc64" /* 0x08 */, XCOFF_SYSCALL64 }, | |
875 | { "syscall" /* 0x10 */, XCOFF_SYSCALL32 }, | |
876 | { "syscall32" /* 0x20 */, XCOFF_SYSCALL32 }, | |
877 | { "syscall3264" /* 0x40 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 }, | |
878 | { "syscall64" /* 0x80 */, XCOFF_SYSCALL64 }, | |
879 | { NULL, 0 }, | |
3b1b01cf TR |
880 | }; |
881 | ||
1fdf0249 | 882 | *flag = 0; |
3b1b01cf | 883 | |
7fc44b9c | 884 | for (bit = 0;; bit++) |
249172c3 TR |
885 | { |
886 | string = s[bit].syscall_string; | |
7fc44b9c | 887 | if (string == NULL) |
249172c3 | 888 | return -1; |
fab80407 | 889 | |
7fc44b9c | 890 | if (0 == strcmp (input, string)) |
249172c3 | 891 | { |
7fc44b9c | 892 | if (1 << bit & syscall_mask) |
249172c3 TR |
893 | { |
894 | *flag = s[bit].flag; | |
895 | return 1; | |
7fc44b9c AM |
896 | } |
897 | else | |
249172c3 TR |
898 | { |
899 | return 0; | |
900 | } | |
901 | } | |
252b5132 | 902 | } |
3b1b01cf TR |
903 | /* should not be here */ |
904 | return -1; | |
252b5132 RH |
905 | } |
906 | ||
907 | /* Read an import or export file. For an import file, this is called | |
908 | by the before_allocation emulation routine. For an export file, | |
3bcf5557 | 909 | this is called by the handle_option emulation routine. */ |
252b5132 RH |
910 | |
911 | static void | |
0c7a8e5a | 912 | gld${EMULATION_NAME}_read_file (const char *filename, bfd_boolean import) |
252b5132 RH |
913 | { |
914 | struct obstack *o; | |
915 | FILE *f; | |
916 | int lineno; | |
917 | int c; | |
b34976b6 | 918 | bfd_boolean keep; |
252b5132 RH |
919 | const char *imppath; |
920 | const char *impfile; | |
921 | const char *impmember; | |
922 | ||
923 | o = (struct obstack *) xmalloc (sizeof (struct obstack)); | |
924 | obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free); | |
925 | ||
926 | f = fopen (filename, FOPEN_RT); | |
927 | if (f == NULL) | |
928 | { | |
929 | bfd_set_error (bfd_error_system_call); | |
930 | einfo ("%F%s: %E\n", filename); | |
931 | } | |
932 | ||
b34976b6 | 933 | keep = FALSE; |
252b5132 RH |
934 | |
935 | imppath = NULL; | |
936 | impfile = NULL; | |
937 | impmember = NULL; | |
938 | ||
939 | lineno = 0; | |
3b1b01cf | 940 | |
69f284c7 TR |
941 | /* Default to 32 and 64 bit mode |
942 | symbols at top of /lib/syscalls.exp do not have a mode modifier and they | |
943 | are not repeated, assume 64 bit routines also want to use them. | |
944 | See the routine change_symbol_mode for more information. */ | |
945 | ||
3b1b01cf TR |
946 | symbol_mode = 0x04; |
947 | ||
252b5132 RH |
948 | while ((c = getc (f)) != EOF) |
949 | { | |
950 | char *s; | |
951 | char *symname; | |
1fdf0249 | 952 | unsigned int syscall_flag = 0; |
252b5132 RH |
953 | bfd_vma address; |
954 | struct bfd_link_hash_entry *h; | |
955 | ||
956 | if (c != '\n') | |
957 | { | |
958 | obstack_1grow (o, c); | |
959 | continue; | |
960 | } | |
961 | ||
962 | obstack_1grow (o, '\0'); | |
963 | ++lineno; | |
964 | ||
965 | s = (char *) obstack_base (o); | |
3882b010 | 966 | while (ISSPACE (*s)) |
252b5132 | 967 | ++s; |
fab80407 AM |
968 | if (*s == '\0' |
969 | || *s == '*' | |
970 | || change_symbol_mode (s) | |
971 | || (*s == '#' && s[1] == ' ') | |
249172c3 | 972 | || (!import && *s == '#' && s[1] == '!')) |
252b5132 RH |
973 | { |
974 | obstack_free (o, obstack_base (o)); | |
975 | continue; | |
976 | } | |
977 | ||
978 | if (*s == '#' && s[1] == '!') | |
979 | { | |
980 | s += 2; | |
3882b010 | 981 | while (ISSPACE (*s)) |
252b5132 RH |
982 | ++s; |
983 | if (*s == '\0') | |
984 | { | |
985 | imppath = NULL; | |
986 | impfile = NULL; | |
987 | impmember = NULL; | |
988 | obstack_free (o, obstack_base (o)); | |
989 | } | |
990 | else if (*s == '(') | |
991 | einfo ("%F%s%d: #! ([member]) is not supported in import files\n", | |
992 | filename, lineno); | |
993 | else | |
994 | { | |
995 | char cs; | |
996 | char *file; | |
997 | ||
998 | (void) obstack_finish (o); | |
b34976b6 | 999 | keep = TRUE; |
252b5132 RH |
1000 | imppath = s; |
1001 | file = NULL; | |
249172c3 | 1002 | while (!ISSPACE (*s) && *s != '(' && *s != '\0') |
252b5132 RH |
1003 | { |
1004 | if (*s == '/') | |
1005 | file = s + 1; | |
1006 | ++s; | |
1007 | } | |
1008 | if (file != NULL) | |
1009 | { | |
1010 | file[-1] = '\0'; | |
1011 | impfile = file; | |
1012 | if (imppath == file - 1) | |
1013 | imppath = "/"; | |
1014 | } | |
1015 | else | |
1016 | { | |
1017 | impfile = imppath; | |
1018 | imppath = ""; | |
1019 | } | |
1020 | cs = *s; | |
1021 | *s = '\0'; | |
3882b010 | 1022 | while (ISSPACE (cs)) |
252b5132 RH |
1023 | { |
1024 | ++s; | |
1025 | cs = *s; | |
1026 | } | |
1027 | if (cs != '(') | |
1028 | { | |
1029 | impmember = ""; | |
1030 | if (cs != '\0') | |
1031 | einfo ("%s:%d: warning: syntax error in import file\n", | |
1032 | filename, lineno); | |
1033 | } | |
1034 | else | |
1035 | { | |
1036 | ++s; | |
1037 | impmember = s; | |
1038 | while (*s != ')' && *s != '\0') | |
1039 | ++s; | |
1040 | if (*s == ')') | |
1041 | *s = '\0'; | |
1042 | else | |
1043 | einfo ("%s:%d: warning: syntax error in import file\n", | |
1044 | filename, lineno); | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | continue; | |
1049 | } | |
1050 | ||
742aeb63 | 1051 | if (symbol_mode & symbol_mode_mask) |
fab80407 AM |
1052 | { |
1053 | /* This is a symbol to be imported or exported. */ | |
1054 | symname = s; | |
1fdf0249 | 1055 | syscall_flag = 0; |
fab80407 AM |
1056 | address = (bfd_vma) -1; |
1057 | ||
249172c3 | 1058 | while (!ISSPACE (*s) && *s != '\0') |
fab80407 AM |
1059 | ++s; |
1060 | if (*s != '\0') | |
1061 | { | |
1062 | char *se; | |
3b1b01cf | 1063 | |
fab80407 | 1064 | *s++ = '\0'; |
3b1b01cf | 1065 | |
3882b010 | 1066 | while (ISSPACE (*s)) |
fab80407 AM |
1067 | ++s; |
1068 | ||
1069 | se = s; | |
249172c3 | 1070 | while (!ISSPACE (*se) && *se != '\0') |
fab80407 AM |
1071 | ++se; |
1072 | if (*se != '\0') | |
1073 | { | |
1074 | *se++ = '\0'; | |
3882b010 | 1075 | while (ISSPACE (*se)) |
fab80407 AM |
1076 | ++se; |
1077 | if (*se != '\0') | |
1078 | einfo ("%s%d: warning: syntax error in import/export file\n", | |
1079 | filename, lineno); | |
1080 | } | |
1081 | ||
1082 | if (s != se) | |
1083 | { | |
1084 | int status; | |
c89e8944 | 1085 | const char *end; |
fab80407 | 1086 | |
249172c3 | 1087 | status = is_syscall (s, &syscall_flag); |
7fc44b9c AM |
1088 | |
1089 | if (0 > status) | |
249172c3 TR |
1090 | { |
1091 | /* not a system call, check for address */ | |
c89e8944 | 1092 | address = bfd_scan_vma (s, &end, 0); |
249172c3 TR |
1093 | if (*end != '\0') |
1094 | { | |
1095 | einfo ("%s:%d: warning: syntax error in import/export file\n", | |
1096 | filename, lineno); | |
7fc44b9c | 1097 | |
249172c3 TR |
1098 | } |
1099 | } | |
3b1b01cf | 1100 | } |
252b5132 | 1101 | } |
3b1b01cf | 1102 | |
249172c3 | 1103 | if (!import) |
fab80407 AM |
1104 | { |
1105 | struct export_symbol_list *n; | |
1106 | ||
1107 | ldlang_add_undef (symname); | |
1108 | n = ((struct export_symbol_list *) | |
1109 | xmalloc (sizeof (struct export_symbol_list))); | |
1110 | n->next = export_symbols; | |
1111 | n->name = xstrdup (symname); | |
fab80407 AM |
1112 | export_symbols = n; |
1113 | } | |
1114 | else | |
1115 | { | |
b34976b6 AM |
1116 | h = bfd_link_hash_lookup (link_info.hash, symname, FALSE, FALSE, |
1117 | TRUE); | |
fab80407 AM |
1118 | if (h == NULL || h->type == bfd_link_hash_new) |
1119 | { | |
1120 | /* We can just ignore attempts to import an unreferenced | |
1121 | symbol. */ | |
1122 | } | |
1123 | else | |
1124 | { | |
249172c3 TR |
1125 | if (!bfd_xcoff_import_symbol (output_bfd, &link_info, h, |
1126 | address, imppath, impfile, | |
1127 | impmember, syscall_flag)) | |
fab80407 AM |
1128 | einfo ("%X%s:%d: failed to import symbol %s: %E\n", |
1129 | filename, lineno, symname); | |
1130 | } | |
1131 | } | |
1132 | } | |
252b5132 RH |
1133 | obstack_free (o, obstack_base (o)); |
1134 | } | |
fab80407 | 1135 | |
252b5132 RH |
1136 | if (obstack_object_size (o) > 0) |
1137 | { | |
fab80407 AM |
1138 | einfo ("%s:%d: warning: ignoring unterminated last line\n", |
1139 | filename, lineno); | |
1140 | obstack_free (o, obstack_base (o)); | |
1141 | } | |
252b5132 | 1142 | |
249172c3 | 1143 | if (!keep) |
fab80407 AM |
1144 | { |
1145 | obstack_free (o, NULL); | |
1146 | free (o); | |
252b5132 | 1147 | } |
fab80407 | 1148 | } |
252b5132 RH |
1149 | |
1150 | /* This routine saves us from worrying about declaring free. */ | |
1151 | ||
1152 | static void | |
0c7a8e5a | 1153 | gld${EMULATION_NAME}_free (void *p) |
252b5132 RH |
1154 | { |
1155 | free (p); | |
1156 | } | |
1157 | ||
1158 | /* This is called by the before_allocation routine via | |
1159 | lang_for_each_statement. It looks for relocations and assignments | |
1160 | to symbols. */ | |
1161 | ||
1162 | static void | |
0c7a8e5a | 1163 | gld${EMULATION_NAME}_find_relocs (lang_statement_union_type *s) |
252b5132 RH |
1164 | { |
1165 | if (s->header.type == lang_reloc_statement_enum) | |
1166 | { | |
1167 | lang_reloc_statement_type *rs; | |
1168 | ||
1169 | rs = &s->reloc_statement; | |
1170 | if (rs->name == NULL) | |
1171 | einfo ("%F%P: only relocations against symbols are permitted\n"); | |
249172c3 | 1172 | if (!bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name)) |
252b5132 RH |
1173 | einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n"); |
1174 | } | |
1175 | ||
1176 | if (s->header.type == lang_assignment_statement_enum) | |
1177 | gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp); | |
1178 | } | |
1179 | ||
1180 | /* Look through an expression for an assignment statement. */ | |
1181 | ||
1182 | static void | |
0c7a8e5a | 1183 | gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp) |
252b5132 RH |
1184 | { |
1185 | struct bfd_link_hash_entry *h; | |
1186 | ||
1187 | switch (exp->type.node_class) | |
1188 | { | |
1189 | case etree_provide: | |
1190 | h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst, | |
b34976b6 | 1191 | FALSE, FALSE, FALSE); |
252b5132 RH |
1192 | if (h == NULL) |
1193 | break; | |
1194 | /* Fall through. */ | |
1195 | case etree_assign: | |
1196 | if (strcmp (exp->assign.dst, ".") != 0) | |
1197 | { | |
249172c3 TR |
1198 | if (!bfd_xcoff_record_link_assignment (output_bfd, &link_info, |
1199 | exp->assign.dst)) | |
252b5132 RH |
1200 | einfo ("%P%F: failed to record assignment to %s: %E\n", |
1201 | exp->assign.dst); | |
1202 | } | |
1203 | gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src); | |
1204 | break; | |
1205 | ||
1206 | case etree_binary: | |
1207 | gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs); | |
1208 | gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs); | |
1209 | break; | |
1210 | ||
1211 | case etree_trinary: | |
1212 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond); | |
1213 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs); | |
1214 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs); | |
1215 | break; | |
1216 | ||
1217 | case etree_unary: | |
1218 | gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child); | |
1219 | break; | |
1220 | ||
1221 | default: | |
1222 | break; | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | static char * | |
0c7a8e5a | 1227 | gld${EMULATION_NAME}_get_script (int *isfile) |
252b5132 RH |
1228 | EOF |
1229 | ||
1230 | if test -n "$COMPILE_IN" | |
1231 | then | |
1232 | # Scripts compiled in. | |
1233 | ||
1234 | # sed commands to quote an ld script as a C string. | |
5f642101 | 1235 | sc="-f ${srcdir}/emultempl/ostring.sed" |
252b5132 RH |
1236 | |
1237 | cat >>e${EMULATION_NAME}.c <<EOF | |
fab80407 | 1238 | { |
252b5132 RH |
1239 | *isfile = 0; |
1240 | ||
1049f94e | 1241 | if (link_info.relocatable && config.build_constructors) |
252b5132 RH |
1242 | return |
1243 | EOF | |
b34976b6 | 1244 | sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c |
1049f94e | 1245 | echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c |
b34976b6 AM |
1246 | sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c |
1247 | echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c | |
1248 | sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c | |
1249 | echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c | |
1250 | sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c | |
1251 | echo ' ; else return' >> e${EMULATION_NAME}.c | |
1252 | sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c | |
1253 | echo '; }' >> e${EMULATION_NAME}.c | |
252b5132 RH |
1254 | |
1255 | else | |
1256 | # Scripts read from the filesystem. | |
1257 | ||
1258 | cat >>e${EMULATION_NAME}.c <<EOF | |
fab80407 | 1259 | { |
252b5132 RH |
1260 | *isfile = 1; |
1261 | ||
1049f94e | 1262 | if (link_info.relocatable && config.build_constructors) |
252b5132 | 1263 | return "ldscripts/${EMULATION_NAME}.xu"; |
1049f94e | 1264 | else if (link_info.relocatable) |
252b5132 RH |
1265 | return "ldscripts/${EMULATION_NAME}.xr"; |
1266 | else if (!config.text_read_only) | |
1267 | return "ldscripts/${EMULATION_NAME}.xbn"; | |
1268 | else if (!config.magic_demand_paged) | |
1269 | return "ldscripts/${EMULATION_NAME}.xn"; | |
1270 | else | |
1271 | return "ldscripts/${EMULATION_NAME}.x"; | |
1272 | } | |
1273 | EOF | |
1274 | ||
1275 | fi | |
1276 | ||
1277 | cat >>e${EMULATION_NAME}.c <<EOF | |
1278 | ||
7fc44b9c | 1279 | static void |
0c7a8e5a | 1280 | gld${EMULATION_NAME}_create_output_section_statements (void) |
9a4c7f16 TR |
1281 | { |
1282 | /* __rtinit */ | |
7fc44b9c AM |
1283 | if ((bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour) |
1284 | && (link_info.init_function != NULL | |
69f284c7 | 1285 | || link_info.fini_function != NULL |
b34976b6 | 1286 | || rtld)) |
9a4c7f16 | 1287 | { |
9a4c7f16 TR |
1288 | initfini_file = lang_add_input_file ("initfini", |
1289 | lang_input_file_is_file_enum, | |
1290 | NULL); | |
7fc44b9c | 1291 | |
9a4c7f16 TR |
1292 | initfini_file->the_bfd = bfd_create ("initfini", output_bfd); |
1293 | if (initfini_file->the_bfd == NULL | |
1294 | || ! bfd_set_arch_mach (initfini_file->the_bfd, | |
1295 | bfd_get_arch (output_bfd), | |
1296 | bfd_get_mach (output_bfd))) | |
1297 | { | |
1298 | einfo ("%X%P: can not create BFD %E\n"); | |
1299 | return; | |
1300 | } | |
7fc44b9c | 1301 | |
9a4c7f16 | 1302 | /* Call backend to fill in the rest */ |
b34976b6 AM |
1303 | if (! bfd_xcoff_link_generate_rtinit (initfini_file->the_bfd, |
1304 | link_info.init_function, | |
1305 | link_info.fini_function, | |
1306 | rtld)) | |
9a4c7f16 TR |
1307 | { |
1308 | einfo ("%X%P: can not create BFD %E\n"); | |
1309 | return; | |
1310 | } | |
69f284c7 TR |
1311 | |
1312 | /* __rtld defined in /lib/librtl.a */ | |
b34976b6 | 1313 | if (rtld) |
69f284c7 | 1314 | lang_add_input_file ("rtl", lang_input_file_is_l_enum, NULL); |
9a4c7f16 TR |
1315 | } |
1316 | } | |
1317 | ||
f1f0d9ab | 1318 | static void |
0c7a8e5a | 1319 | gld${EMULATION_NAME}_set_output_arch (void) |
f1f0d9ab TR |
1320 | { |
1321 | bfd_set_arch_mach (output_bfd, | |
1322 | bfd_xcoff_architecture (output_bfd), | |
1323 | bfd_xcoff_machine (output_bfd)); | |
1324 | ||
1325 | ldfile_output_architecture = bfd_get_arch (output_bfd); | |
1326 | ldfile_output_machine = bfd_get_mach (output_bfd); | |
1327 | ldfile_output_machine_name = bfd_printable_name (output_bfd); | |
1328 | } | |
1329 | ||
249172c3 | 1330 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = { |
252b5132 RH |
1331 | gld${EMULATION_NAME}_before_parse, |
1332 | syslib_default, | |
1333 | hll_default, | |
1334 | after_parse_default, | |
1335 | gld${EMULATION_NAME}_after_open, | |
1336 | after_allocation_default, | |
f1f0d9ab TR |
1337 | gld${EMULATION_NAME}_set_output_arch, |
1338 | gld${EMULATION_NAME}_choose_target, | |
252b5132 RH |
1339 | gld${EMULATION_NAME}_before_allocation, |
1340 | gld${EMULATION_NAME}_get_script, | |
1341 | "${EMULATION_NAME}", | |
1342 | "${OUTPUT_FORMAT}", | |
249172c3 | 1343 | 0, /* finish */ |
9a4c7f16 | 1344 | gld${EMULATION_NAME}_create_output_section_statements, |
249172c3 TR |
1345 | 0, /* open_dynamic_archive */ |
1346 | 0, /* place_orphan */ | |
1347 | 0, /* set_symbols */ | |
252b5132 | 1348 | gld${EMULATION_NAME}_parse_args, |
3bcf5557 AM |
1349 | gld${EMULATION_NAME}_add_options, |
1350 | gld${EMULATION_NAME}_handle_option, | |
40d109bf | 1351 | gld${EMULATION_NAME}_unrecognized_file, |
249172c3 TR |
1352 | NULL, /* list_options */ |
1353 | NULL, /* recognized_file */ | |
1354 | NULL, /* find potential_libraries */ | |
fac1652d | 1355 | NULL /* new_vers_pattern */ |
252b5132 RH |
1356 | }; |
1357 | EOF |