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