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