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