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