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