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