* ldmain.c (main): Flush stdout before and stderr after printing
[binutils-gdb.git] / ld / emultempl / pep.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
4 OUTPUT_ARCH=${ARCH}
5 else
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
7 fi
8 rm -f e${EMULATION_NAME}.c
9 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
10 fragment <<EOF
11 /* Copyright 2006, 2007, 2008, 2009, 2010, 2011
12 Free Software Foundation, Inc.
13 Written by Kai Tietz, OneVision Software GmbH&CoKg.
14
15 This file is part of the GNU Binutils.
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3 of the License, or
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
31
32
33 /* For WINDOWS_XP64 and higher */
34 /* Based on pe.em, but modified for 64 bit support. */
35
36 #define TARGET_IS_${EMULATION_NAME}
37
38 #define COFF_IMAGE_WITH_PE
39 #define COFF_WITH_PE
40 #define COFF_WITH_pex64
41
42 #include "sysdep.h"
43 #include "bfd.h"
44 #include "bfdlink.h"
45 #include "getopt.h"
46 #include "libiberty.h"
47 #include "ld.h"
48 #include "ldmain.h"
49 #include "ldexp.h"
50 #include "ldlang.h"
51 #include "ldfile.h"
52 #include "ldemul.h"
53 #include <ldgram.h>
54 #include "ldlex.h"
55 #include "ldmisc.h"
56 #include "ldctor.h"
57 #include "coff/internal.h"
58
59 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
60 header in generic PE code. */
61 #include "coff/x86_64.h"
62 #include "coff/pe.h"
63
64 /* FIXME: This is a BFD internal header file, and we should not be
65 using it here. */
66 #include "../bfd/libcoff.h"
67
68 #undef AOUTSZ
69 #define AOUTSZ PEPAOUTSZ
70 #define PEAOUTHDR PEPAOUTHDR
71
72 #include "deffile.h"
73 #include "pep-dll.h"
74 #include "safe-ctype.h"
75
76 /* Permit the emulation parameters to override the default section
77 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
78 it seem that include/coff/internal.h should not define
79 PE_DEF_SECTION_ALIGNMENT. */
80 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
81 #undef PE_DEF_SECTION_ALIGNMENT
82 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
83 #endif
84
85 #ifdef TARGET_IS_i386pep
86 #define DLL_SUPPORT
87 #endif
88
89 #if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
90 #define PE_DEF_SUBSYSTEM 3
91 #else
92 #undef NT_EXE_IMAGE_BASE
93 #define NT_EXE_IMAGE_BASE 0x00010000
94 #undef PE_DEF_SECTION_ALIGNMENT
95 #define PE_DEF_SUBSYSTEM 2
96 #undef PE_DEF_FILE_ALIGNMENT
97 #define PE_DEF_FILE_ALIGNMENT 0x00000200
98 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
99 #endif
100
101 static struct internal_extra_pe_aouthdr pep;
102 static int dll;
103 static int pep_subsystem = ${SUBSYSTEM};
104 static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
105 static int support_old_code = 0;
106 static lang_assignment_statement_type *image_base_statement = 0;
107 static unsigned short pe_dll_characteristics = 0;
108
109 #ifdef DLL_SUPPORT
110 static int pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default). */
111 static char * pep_out_def_filename = NULL;
112 static char * pep_implib_filename = NULL;
113 static int pep_enable_auto_image_base = 0;
114 static char * pep_dll_search_prefix = NULL;
115 #endif
116
117 extern const char *output_filename;
118
119 static int is_underscoring (void)
120 {
121 int u = 0;
122 if (pep_leading_underscore != -1)
123 return pep_leading_underscore;
124 if (!bfd_get_target_info ("${OUTPUT_FORMAT}", NULL, NULL, &u, NULL))
125 bfd_get_target_info ("${RELOCATEABLE_OUTPUT_FORMAT}", NULL, NULL, &u, NULL);
126
127 if (u == -1)
128 abort ();
129 pep_leading_underscore = (u != 0 ? 1 : 0);
130 return pep_leading_underscore;
131 }
132
133
134 static void
135 gld_${EMULATION_NAME}_before_parse (void)
136 {
137 is_underscoring ();
138 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
139 output_filename = "${EXECUTABLE_NAME:-a.exe}";
140 #ifdef DLL_SUPPORT
141 config.dynamic_link = TRUE;
142 config.has_shared = 1;
143 link_info.pei386_auto_import = 1;
144 link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2. */
145 #endif
146 }
147 \f
148 /* PE format extra command line options. */
149
150 /* Used for setting flags in the PE header. */
151 enum options
152 {
153 OPTION_BASE_FILE = 300 + 1,
154 OPTION_DLL,
155 OPTION_FILE_ALIGNMENT,
156 OPTION_IMAGE_BASE,
157 OPTION_MAJOR_IMAGE_VERSION,
158 OPTION_MAJOR_OS_VERSION,
159 OPTION_MAJOR_SUBSYSTEM_VERSION,
160 OPTION_MINOR_IMAGE_VERSION,
161 OPTION_MINOR_OS_VERSION,
162 OPTION_MINOR_SUBSYSTEM_VERSION,
163 OPTION_SECTION_ALIGNMENT,
164 OPTION_STACK,
165 OPTION_SUBSYSTEM,
166 OPTION_HEAP,
167 OPTION_SUPPORT_OLD_CODE,
168 OPTION_OUT_DEF,
169 OPTION_EXPORT_ALL,
170 OPTION_EXCLUDE_SYMBOLS,
171 OPTION_EXCLUDE_ALL_SYMBOLS,
172 OPTION_KILL_ATS,
173 OPTION_STDCALL_ALIASES,
174 OPTION_ENABLE_STDCALL_FIXUP,
175 OPTION_DISABLE_STDCALL_FIXUP,
176 OPTION_IMPLIB_FILENAME,
177 OPTION_WARN_DUPLICATE_EXPORTS,
178 OPTION_IMP_COMPAT,
179 OPTION_ENABLE_AUTO_IMAGE_BASE,
180 OPTION_DISABLE_AUTO_IMAGE_BASE,
181 OPTION_DLL_SEARCH_PREFIX,
182 OPTION_NO_DEFAULT_EXCLUDES,
183 OPTION_DLL_ENABLE_AUTO_IMPORT,
184 OPTION_DLL_DISABLE_AUTO_IMPORT,
185 OPTION_ENABLE_EXTRA_PE_DEBUG,
186 OPTION_EXCLUDE_LIBS,
187 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
188 OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
189 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
190 OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
191 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
192 OPTION_NO_LEADING_UNDERSCORE,
193 OPTION_LEADING_UNDERSCORE,
194 OPTION_ENABLE_LONG_SECTION_NAMES,
195 OPTION_DISABLE_LONG_SECTION_NAMES,
196 OPTION_DYNAMIC_BASE,
197 OPTION_FORCE_INTEGRITY,
198 OPTION_NX_COMPAT,
199 OPTION_NO_ISOLATION,
200 OPTION_NO_SEH,
201 OPTION_NO_BIND,
202 OPTION_WDM_DRIVER,
203 OPTION_TERMINAL_SERVER_AWARE
204 };
205
206 static void
207 gld${EMULATION_NAME}_add_options
208 (int ns ATTRIBUTE_UNUSED,
209 char **shortopts ATTRIBUTE_UNUSED,
210 int nl,
211 struct option **longopts,
212 int nrl ATTRIBUTE_UNUSED,
213 struct option **really_longopts ATTRIBUTE_UNUSED)
214 {
215 static const struct option xtra_long[] =
216 {
217 /* PE options */
218 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
219 {"dll", no_argument, NULL, OPTION_DLL},
220 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
221 {"heap", required_argument, NULL, OPTION_HEAP},
222 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
223 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
224 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
225 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
226 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
227 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
228 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
229 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
230 {"stack", required_argument, NULL, OPTION_STACK},
231 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
232 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
233 {"use-nul-prefixed-import-tables", no_argument, NULL,
234 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
235 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
236 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
237 #ifdef DLL_SUPPORT
238 /* getopt allows abbreviations, so we do this to stop it
239 from treating -o as an abbreviation for this option. */
240 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
241 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
242 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
243 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
244 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
245 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
246 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
247 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
248 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
249 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
250 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
251 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
252 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
253 /* getopt() allows abbreviations, so we do this to stop it from
254 treating -c as an abbreviation for these --compat-implib. */
255 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
256 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
257 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
258 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
259 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
260 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
261 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
262 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
263 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
264 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
265 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
266 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
267 #endif
268 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
269 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
270 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
271 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
272 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
273 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
274 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
275 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
276 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
277 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
278 {NULL, no_argument, NULL, 0}
279 };
280
281 *longopts
282 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
283 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
284 }
285
286 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
287 parameters which may be input from the command line. */
288
289 typedef struct
290 {
291 void *ptr;
292 int size;
293 bfd_vma value;
294 char *symbol;
295 int inited;
296 /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
297 C visible symbols can be prefixed by underscore dependent on target's
298 settings. */
299 bfd_boolean is_c_symbol;
300 } definfo;
301
302 #define GET_INIT_SYMBOL_NAME(IDX) \
303 (init[(IDX)].symbol \
304 + ((init[(IDX)].is_c_symbol == FALSE || (is_underscoring () == 1)) ? 0 : 1))
305
306 /* Decorates the C visible symbol by underscore, if target requires. */
307 #define U(CSTR) \
308 ((is_underscoring () == 0) ? CSTR : "_" CSTR)
309
310 /* Get size of constant string for a possible underscore prefixed
311 C visible symbol. */
312 #define U_SIZE(CSTR) \
313 (sizeof (CSTR) + (is_underscoring () == 0 ? 0 : 1))
314
315 #define D(field,symbol,def,usc) {&pep.field,sizeof(pep.field), def, symbol,0, usc}
316
317 static definfo init[] =
318 {
319 /* imagebase must be first */
320 #define IMAGEBASEOFF 0
321 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, FALSE),
322 #define DLLOFF 1
323 {&dll, sizeof(dll), 0, "__dll__", 0, FALSE},
324 #define MSIMAGEBASEOFF 2
325 D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, TRUE),
326 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, FALSE),
327 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, FALSE),
328 D(MajorOperatingSystemVersion,"__major_os_version__", 4, FALSE),
329 D(MinorOperatingSystemVersion,"__minor_os_version__", 0, FALSE),
330 D(MajorImageVersion,"__major_image_version__", 0, FALSE),
331 D(MinorImageVersion,"__minor_image_version__", 0, FALSE),
332 D(MajorSubsystemVersion,"__major_subsystem_version__", 5, FALSE),
333 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2, FALSE),
334 D(Subsystem,"__subsystem__", ${SUBSYSTEM}, FALSE),
335 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, FALSE),
336 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, FALSE),
337 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, FALSE),
338 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, FALSE),
339 D(LoaderFlags,"__loader_flags__", 0x0, FALSE),
340 D(DllCharacteristics, "__dll_characteristics__", 0x0, FALSE),
341 { NULL, 0, 0, NULL, 0, FALSE}
342 };
343
344
345 static void
346 gld_${EMULATION_NAME}_list_options (FILE *file)
347 {
348 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
349 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
350 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
351 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
352 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
353 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
354 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
355 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
356 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
357 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
358 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
359 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
360 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
361 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
362 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
363 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
364 #ifdef DLL_SUPPORT
365 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
366 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
367 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
368 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
369 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
370 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
371 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
372 fprintf (file, _(" Exclude objects, archive members from auto\n"));
373 fprintf (file, _(" export, place into import library instead.\n"));
374 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
375 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
376 fprintf (file, _(" --out-implib <file> Generate import library\n"));
377 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
378 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
379 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
380 create __imp_<SYMBOL> as well.\n"));
381 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
382 unless user specifies one\n"));
383 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
384 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
385 an importlib, use <string><basename>.dll\n\
386 in preference to lib<basename>.dll \n"));
387 fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\
388 __imp_sym for DATA references\n"));
389 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
390 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
391 adding pseudo-relocations resolved at\n\
392 runtime.\n"));
393 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
394 auto-imported DATA.\n"));
395 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
396 or linking to DLLs (esp. auto-import)\n"));
397 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
398 executable image files\n"));
399 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
400 in object files\n"));
401 fprintf (file, _(" --dynamicbase Image base address may be relocated using\n\
402 address space layout randomization (ASLR)\n"));
403 fprintf (file, _(" --forceinteg Code integrity checks are enforced\n"));
404 fprintf (file, _(" --nxcompat Image is compatible with data execution prevention\n"));
405 fprintf (file, _(" --no-isolation Image understands isolation but do not isolate the image\n"));
406 fprintf (file, _(" --no-seh Image does not use SEH. No SE handler may\n\
407 be called in this image\n"));
408 fprintf (file, _(" --no-bind Do not bind this image\n"));
409 fprintf (file, _(" --wdmdriver Driver uses the WDM model\n"));
410 fprintf (file, _(" --tsaware Image is Terminal Server aware\n"));
411 #endif
412 }
413
414
415 static void
416 set_pep_name (char *name, bfd_vma val)
417 {
418 int i;
419 is_underscoring ();
420 /* Find the name and set it. */
421 for (i = 0; init[i].ptr; i++)
422 {
423 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
424 {
425 init[i].value = val;
426 init[i].inited = 1;
427 if (strcmp (name,"__image_base__") == 0)
428 set_pep_name (U ("__ImageBase"), val);
429 return;
430 }
431 }
432 abort ();
433 }
434
435 static void
436 set_entry_point (void)
437 {
438 const char *entry;
439 const char *initial_symbol_char;
440 int i;
441
442 static const struct
443 {
444 const int value;
445 const char *entry;
446 }
447 v[] =
448 {
449 { 1, "NtProcessStartup" },
450 { 2, "WinMainCRTStartup" },
451 { 3, "mainCRTStartup" },
452 { 7, "__PosixProcessStartup" },
453 { 9, "WinMainCRTStartup" },
454 {14, "mainCRTStartup" },
455 { 0, NULL }
456 };
457
458 /* Entry point name for arbitrary subsystem numbers. */
459 static const char default_entry[] = "mainCRTStartup";
460
461 if (link_info.shared || dll)
462 {
463 entry = "DllMainCRTStartup";
464 }
465 else
466 {
467 for (i = 0; v[i].entry; i++)
468 if (v[i].value == pep_subsystem)
469 break;
470
471 /* If no match, use the default. */
472 if (v[i].entry != NULL)
473 entry = v[i].entry;
474 else
475 entry = default_entry;
476 }
477
478 /* Now we check target's default for getting proper symbol_char. */
479 initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
480
481 if (*initial_symbol_char != '\0')
482 {
483 char *alc_entry;
484
485 /* lang_default_entry expects its argument to be permanently
486 allocated, so we don't free this string. */
487 alc_entry = xmalloc (strlen (initial_symbol_char)
488 + strlen (entry)
489 + 1);
490 strcpy (alc_entry, initial_symbol_char);
491 strcat (alc_entry, entry);
492 entry = alc_entry;
493 }
494
495 lang_default_entry (entry);
496 }
497
498 static void
499 set_pep_subsystem (void)
500 {
501 const char *sver;
502 char *end;
503 int len;
504 int i;
505 unsigned long temp_subsystem;
506 static const struct
507 {
508 const char *name;
509 const int value;
510 }
511 v[] =
512 {
513 { "native", 1 },
514 { "windows", 2 },
515 { "console", 3 },
516 { "posix", 7 },
517 { "wince", 9 },
518 { "xbox", 14 },
519 { NULL, 0 }
520 };
521
522 /* Check for the presence of a version number. */
523 sver = strchr (optarg, ':');
524 if (sver == NULL)
525 len = strlen (optarg);
526 else
527 {
528 len = sver - optarg;
529 set_pep_name ("__major_subsystem_version__",
530 strtoul (sver + 1, &end, 0));
531 if (*end == '.')
532 set_pep_name ("__minor_subsystem_version__",
533 strtoul (end + 1, &end, 0));
534 if (*end != '\0')
535 einfo (_("%P: warning: bad version number in -subsystem option\n"));
536 }
537
538 /* Check for numeric subsystem. */
539 temp_subsystem = strtoul (optarg, & end, 0);
540 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
541 {
542 /* Search list for a numeric match to use its entry point. */
543 for (i = 0; v[i].name; i++)
544 if (v[i].value == (int) temp_subsystem)
545 break;
546
547 /* Use this subsystem. */
548 pep_subsystem = (int) temp_subsystem;
549 }
550 else
551 {
552 /* Search for subsystem by name. */
553 for (i = 0; v[i].name; i++)
554 if (strncmp (optarg, v[i].name, len) == 0
555 && v[i].name[len] == '\0')
556 break;
557
558 if (v[i].name == NULL)
559 {
560 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
561 return;
562 }
563
564 pep_subsystem = v[i].value;
565 }
566
567 set_pep_name ("__subsystem__", pep_subsystem);
568
569 return;
570 }
571
572
573 static void
574 set_pep_value (char *name)
575 {
576 char *end;
577
578 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
579
580 if (end == optarg)
581 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
582
583 optarg = end;
584 }
585
586
587 static void
588 set_pep_stack_heap (char *resname, char *comname)
589 {
590 set_pep_value (resname);
591
592 if (*optarg == ',')
593 {
594 optarg++;
595 set_pep_value (comname);
596 }
597 else if (*optarg)
598 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
599 }
600
601
602 static bfd_boolean
603 gld${EMULATION_NAME}_handle_option (int optc)
604 {
605 is_underscoring ();
606 switch (optc)
607 {
608 default:
609 return FALSE;
610
611 case OPTION_BASE_FILE:
612 link_info.base_file = fopen (optarg, FOPEN_WB);
613 if (link_info.base_file == NULL)
614 einfo (_("%F%P: cannot open base file %s\n"), optarg);
615 break;
616
617 /* PE options. */
618 case OPTION_HEAP:
619 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
620 break;
621 case OPTION_STACK:
622 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
623 break;
624 case OPTION_SUBSYSTEM:
625 set_pep_subsystem ();
626 break;
627 case OPTION_MAJOR_OS_VERSION:
628 set_pep_value ("__major_os_version__");
629 break;
630 case OPTION_MINOR_OS_VERSION:
631 set_pep_value ("__minor_os_version__");
632 break;
633 case OPTION_MAJOR_SUBSYSTEM_VERSION:
634 set_pep_value ("__major_subsystem_version__");
635 break;
636 case OPTION_MINOR_SUBSYSTEM_VERSION:
637 set_pep_value ("__minor_subsystem_version__");
638 break;
639 case OPTION_MAJOR_IMAGE_VERSION:
640 set_pep_value ("__major_image_version__");
641 break;
642 case OPTION_MINOR_IMAGE_VERSION:
643 set_pep_value ("__minor_image_version__");
644 break;
645 case OPTION_FILE_ALIGNMENT:
646 set_pep_value ("__file_alignment__");
647 break;
648 case OPTION_SECTION_ALIGNMENT:
649 set_pep_value ("__section_alignment__");
650 break;
651 case OPTION_DLL:
652 set_pep_name ("__dll__", 1);
653 break;
654 case OPTION_IMAGE_BASE:
655 set_pep_value ("__image_base__");
656 break;
657 case OPTION_SUPPORT_OLD_CODE:
658 support_old_code = 1;
659 break;
660 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
661 pep_use_nul_prefixed_import_tables = TRUE;
662 break;
663 case OPTION_NO_LEADING_UNDERSCORE:
664 pep_leading_underscore = 0;
665 break;
666 case OPTION_LEADING_UNDERSCORE:
667 pep_leading_underscore = 1;
668 break;
669 #ifdef DLL_SUPPORT
670 case OPTION_OUT_DEF:
671 pep_out_def_filename = xstrdup (optarg);
672 break;
673 case OPTION_EXPORT_ALL:
674 pep_dll_export_everything = 1;
675 break;
676 case OPTION_EXCLUDE_SYMBOLS:
677 pep_dll_add_excludes (optarg, EXCLUDESYMS);
678 break;
679 case OPTION_EXCLUDE_ALL_SYMBOLS:
680 pep_dll_exclude_all_symbols = 1;
681 break;
682 case OPTION_EXCLUDE_LIBS:
683 pep_dll_add_excludes (optarg, EXCLUDELIBS);
684 break;
685 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
686 pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
687 break;
688 case OPTION_KILL_ATS:
689 pep_dll_kill_ats = 1;
690 break;
691 case OPTION_STDCALL_ALIASES:
692 pep_dll_stdcall_aliases = 1;
693 break;
694 case OPTION_ENABLE_STDCALL_FIXUP:
695 pep_enable_stdcall_fixup = 1;
696 break;
697 case OPTION_DISABLE_STDCALL_FIXUP:
698 pep_enable_stdcall_fixup = 0;
699 break;
700 case OPTION_IMPLIB_FILENAME:
701 pep_implib_filename = xstrdup (optarg);
702 break;
703 case OPTION_WARN_DUPLICATE_EXPORTS:
704 pep_dll_warn_dup_exports = 1;
705 break;
706 case OPTION_IMP_COMPAT:
707 pep_dll_compat_implib = 1;
708 break;
709 case OPTION_ENABLE_AUTO_IMAGE_BASE:
710 pep_enable_auto_image_base = 1;
711 break;
712 case OPTION_DISABLE_AUTO_IMAGE_BASE:
713 pep_enable_auto_image_base = 0;
714 break;
715 case OPTION_DLL_SEARCH_PREFIX:
716 pep_dll_search_prefix = xstrdup (optarg);
717 break;
718 case OPTION_NO_DEFAULT_EXCLUDES:
719 pep_dll_do_default_excludes = 0;
720 break;
721 case OPTION_DLL_ENABLE_AUTO_IMPORT:
722 link_info.pei386_auto_import = 1;
723 break;
724 case OPTION_DLL_DISABLE_AUTO_IMPORT:
725 link_info.pei386_auto_import = 0;
726 break;
727 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
728 link_info.pei386_runtime_pseudo_reloc = 2;
729 break;
730 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
731 link_info.pei386_runtime_pseudo_reloc = 0;
732 break;
733 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
734 link_info.pei386_runtime_pseudo_reloc = 2;
735 break;
736 case OPTION_ENABLE_EXTRA_PE_DEBUG:
737 pep_dll_extra_pe_debug = 1;
738 break;
739 #endif
740 case OPTION_ENABLE_LONG_SECTION_NAMES:
741 pep_use_coff_long_section_names = 1;
742 break;
743 case OPTION_DISABLE_LONG_SECTION_NAMES:
744 pep_use_coff_long_section_names = 0;
745 break;
746 /* Get DLLCharacteristics bits */
747 case OPTION_DYNAMIC_BASE:
748 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
749 break;
750 case OPTION_FORCE_INTEGRITY:
751 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
752 break;
753 case OPTION_NX_COMPAT:
754 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
755 break;
756 case OPTION_NO_ISOLATION:
757 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
758 break;
759 case OPTION_NO_SEH:
760 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
761 break;
762 case OPTION_NO_BIND:
763 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
764 break;
765 case OPTION_WDM_DRIVER:
766 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
767 break;
768 case OPTION_TERMINAL_SERVER_AWARE:
769 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
770 break;
771 }
772
773 /* Set DLLCharacteristics bits */
774 set_pep_name ("__dll_characteristics__", pe_dll_characteristics);
775
776 return TRUE;
777 }
778 \f
779
780 #ifdef DLL_SUPPORT
781 static unsigned long
782 strhash (const char *str)
783 {
784 const unsigned char *s;
785 unsigned long hash;
786 unsigned int c;
787 unsigned int len;
788
789 hash = 0;
790 len = 0;
791 s = (const unsigned char *) str;
792 while ((c = *s++) != '\0')
793 {
794 hash += c + (c << 17);
795 hash ^= hash >> 2;
796 ++len;
797 }
798 hash += len + (len << 17);
799 hash ^= hash >> 2;
800
801 return hash;
802 }
803
804 /* Use the output file to create a image base for relocatable DLLs. */
805
806 static bfd_vma
807 compute_dll_image_base (const char *ofile)
808 {
809 bfd_vma hash = (bfd_vma) strhash (ofile);
810 return 0x61300000 + ((hash << 16) & 0x0FFC0000);
811 }
812 #endif
813
814 /* Assign values to the special symbols before the linker script is
815 read. */
816
817 static void
818 gld_${EMULATION_NAME}_set_symbols (void)
819 {
820 /* Run through and invent symbols for all the
821 names and insert the defaults. */
822 int j;
823
824 is_underscoring ();
825
826 if (!init[IMAGEBASEOFF].inited)
827 {
828 if (link_info.relocatable)
829 init[IMAGEBASEOFF].value = 0;
830 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
831 {
832 #ifdef DLL_SUPPORT
833 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base
834 ? compute_dll_image_base (output_filename)
835 : NT_DLL_IMAGE_BASE);
836 #else
837 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
838 #endif
839 }
840 else
841 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
842 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
843 }
844
845 /* Don't do any symbol assignments if this is a relocatable link. */
846 if (link_info.relocatable)
847 return;
848
849 /* Glue the assignments into the abs section. */
850 push_stat_ptr (&abs_output_section->children);
851
852 for (j = 0; init[j].ptr; j++)
853 {
854 bfd_vma val = init[j].value;
855 lang_assignment_statement_type *rv;
856
857 rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
858 exp_intop (val)));
859 if (init[j].size == sizeof (short))
860 *(short *) init[j].ptr = (short) val;
861 else if (init[j].size == sizeof (int))
862 *(int *) init[j].ptr = (int) val;
863 else if (init[j].size == sizeof (long))
864 *(long *) init[j].ptr = (long) val;
865 /* This might be a long long or other special type. */
866 else if (init[j].size == sizeof (bfd_vma))
867 *(bfd_vma *) init[j].ptr = val;
868 else abort ();
869 if (j == IMAGEBASEOFF)
870 image_base_statement = rv;
871 }
872 /* Restore the pointer. */
873 pop_stat_ptr ();
874
875 if (pep.FileAlignment > pep.SectionAlignment)
876 {
877 einfo (_("%P: warning, file alignment > section alignment.\n"));
878 }
879 }
880
881 /* This is called after the linker script and the command line options
882 have been read. */
883
884 static void
885 gld_${EMULATION_NAME}_after_parse (void)
886 {
887 /* PR ld/6744: Warn the user if they have used an ELF-only
888 option hoping it will work on PE+. */
889 if (link_info.export_dynamic)
890 einfo (_("%P: warning: --export-dynamic is not supported for PE+ "
891 "targets, did you mean --export-all-symbols?\n"));
892
893 set_entry_point ();
894
895 after_parse_default ();
896 }
897
898 /* pep-dll.c directly accesses pep_data_import_dll,
899 so it must be defined outside of #ifdef DLL_SUPPORT.
900 Note - this variable is deliberately not initialised.
901 This allows it to be treated as a common varaible, and only
902 exist in one incarnation in a multiple target enabled linker. */
903 char * pep_data_import_dll;
904
905 #ifdef DLL_SUPPORT
906 static struct bfd_link_hash_entry *pep_undef_found_sym;
907
908 static bfd_boolean
909 pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
910 {
911 int sl;
912 char *string = inf;
913 const char *hs = h->root.string;
914
915 sl = strlen (string);
916 if (h->type == bfd_link_hash_defined
917 && ((*hs == '@' && *string == '_'
918 && strncmp (hs + 1, string + 1, sl - 1) == 0)
919 || strncmp (hs, string, sl) == 0)
920 && h->root.string[sl] == '@')
921 {
922 pep_undef_found_sym = h;
923 return FALSE;
924 }
925 return TRUE;
926 }
927
928 static void
929 pep_fixup_stdcalls (void)
930 {
931 static int gave_warning_message = 0;
932 struct bfd_link_hash_entry *undef, *sym;
933
934 if (pep_dll_extra_pe_debug)
935 printf ("%s\n", __FUNCTION__);
936
937 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
938 if (undef->type == bfd_link_hash_undefined)
939 {
940 char* at = strchr (undef->root.string, '@');
941 int lead_at = (*undef->root.string == '@');
942 if (lead_at)
943 at = strchr (undef->root.string + 1, '@');
944 if (at || lead_at)
945 {
946 /* The symbol is a stdcall symbol, so let's look for a
947 cdecl symbol with the same name and resolve to that. */
948 char *cname = xstrdup (undef->root.string);
949
950 if (lead_at)
951 *cname = '_';
952 at = strchr (cname, '@');
953 if (at)
954 *at = 0;
955 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
956
957 if (sym && sym->type == bfd_link_hash_defined)
958 {
959 undef->type = bfd_link_hash_defined;
960 undef->u.def.value = sym->u.def.value;
961 undef->u.def.section = sym->u.def.section;
962
963 if (pep_enable_stdcall_fixup == -1)
964 {
965 einfo (_("Warning: resolving %s by linking to %s\n"),
966 undef->root.string, cname);
967 if (! gave_warning_message)
968 {
969 gave_warning_message = 1;
970 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
971 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
972 }
973 }
974 }
975 }
976 else
977 {
978 /* The symbol is a cdecl symbol, so we look for stdcall
979 symbols - which means scanning the whole symbol table. */
980 pep_undef_found_sym = 0;
981 bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
982 (char *) undef->root.string);
983 sym = pep_undef_found_sym;
984 if (sym)
985 {
986 undef->type = bfd_link_hash_defined;
987 undef->u.def.value = sym->u.def.value;
988 undef->u.def.section = sym->u.def.section;
989
990 if (pep_enable_stdcall_fixup == -1)
991 {
992 einfo (_("Warning: resolving %s by linking to %s\n"),
993 undef->root.string, sym->root.string);
994 if (! gave_warning_message)
995 {
996 gave_warning_message = 1;
997 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
998 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
999 }
1000 }
1001 }
1002 }
1003 }
1004 }
1005
1006 static int
1007 make_import_fixup (arelent *rel, asection *s)
1008 {
1009 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
1010 char addend[8];
1011 bfd_vma _addend = 0;
1012 int suc = 0;
1013
1014 if (pep_dll_extra_pe_debug)
1015 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
1016 (unsigned long) rel->address, (long) rel->addend);
1017
1018 memset (addend, 0, sizeof (addend));
1019 switch ((rel->howto->bitsize))
1020 {
1021 case 8:
1022 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
1023 if (suc && rel->howto->pc_relative)
1024 _addend = (bfd_vma) ((bfd_signed_vma) ((char) bfd_get_8 (s->owner, addend)));
1025 else if (suc)
1026 _addend = ((bfd_vma) bfd_get_8 (s->owner, addend)) & 0xff;
1027 break;
1028 case 16:
1029 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
1030 if (suc && rel->howto->pc_relative)
1031 _addend = (bfd_vma) ((bfd_signed_vma) ((short) bfd_get_16 (s->owner, addend)));
1032 else if (suc)
1033 _addend = ((bfd_vma) bfd_get_16 (s->owner, addend)) & 0xffff;
1034 break;
1035 case 32:
1036 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
1037 if (suc && rel->howto->pc_relative)
1038 _addend = (bfd_vma) ((bfd_signed_vma) ((int) bfd_get_32 (s->owner, addend)));
1039 else if (suc)
1040 _addend = ((bfd_vma) bfd_get_32 (s->owner, addend)) & 0xffffffff;
1041 break;
1042 case 64:
1043 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
1044 if (suc)
1045 _addend = ((bfd_vma) bfd_get_64 (s->owner, addend));
1046 break;
1047 }
1048 if (! suc)
1049 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
1050 s->owner, s, rel->address);
1051
1052 if (pep_dll_extra_pe_debug)
1053 {
1054 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx", (long) _addend, (long) rel->addend, (long) rel->address);
1055 if (rel->howto->pc_relative) printf (" pcrel");
1056 printf (" %d bit rel.\n",(int) rel->howto->bitsize);
1057 }
1058 pep_create_import_fixup (rel, s, _addend);
1059
1060 return 1;
1061 }
1062
1063 static void
1064 pep_find_data_imports (void)
1065 {
1066 struct bfd_link_hash_entry *undef, *sym;
1067
1068 if (link_info.pei386_auto_import == 0)
1069 return;
1070
1071 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1072 {
1073 if (undef->type == bfd_link_hash_undefined)
1074 {
1075 /* C++ symbols are *long*. */
1076 char buf[4096];
1077
1078 if (pep_dll_extra_pe_debug)
1079 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
1080
1081 sprintf (buf, "__imp_%s", undef->root.string);
1082
1083 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
1084
1085 if (sym && sym->type == bfd_link_hash_defined)
1086 {
1087 bfd *b = sym->u.def.section->owner;
1088 asymbol **symbols;
1089 int nsyms, i;
1090
1091 if (!bfd_generic_link_read_symbols (b))
1092 {
1093 einfo (_("%B%F: could not read symbols: %E\n"), b);
1094 return;
1095 }
1096
1097 symbols = bfd_get_outsymbols (b);
1098 nsyms = bfd_get_symcount (b);
1099
1100 for (i = 0; i < nsyms; i++)
1101 {
1102 if (! CONST_STRNEQ (symbols[i]->name, U ("_head_")))
1103 continue;
1104
1105 if (pep_dll_extra_pe_debug)
1106 printf ("->%s\n", symbols[i]->name);
1107
1108 pep_data_import_dll = (char*) (symbols[i]->name +
1109 U_SIZE ("_head_") - 1);
1110 break;
1111 }
1112
1113 pep_walk_relocs_of_symbol (&link_info, undef->root.string,
1114 make_import_fixup);
1115
1116 /* Let's differentiate it somehow from defined. */
1117 undef->type = bfd_link_hash_defweak;
1118 /* We replace original name with __imp_ prefixed, this
1119 1) may trash memory 2) leads to duplicate symbol generation.
1120 Still, IMHO it's better than having name poluted. */
1121 undef->root.string = sym->root.string;
1122 undef->u.def.value = sym->u.def.value;
1123 undef->u.def.section = sym->u.def.section;
1124 }
1125 }
1126 }
1127 }
1128
1129 static bfd_boolean
1130 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1131 {
1132 if (pep_dll_extra_pe_debug)
1133 printf ("+%s\n", h->string);
1134
1135 return TRUE;
1136 }
1137 #endif /* DLL_SUPPORT */
1138
1139 static void
1140 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1141 {
1142 int *found = (int *) obj;
1143 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1144 *found = 1;
1145 }
1146
1147 static void
1148 gld_${EMULATION_NAME}_after_open (void)
1149 {
1150 after_open_default ();
1151
1152 is_underscoring ();
1153 #ifdef DLL_SUPPORT
1154 if (pep_dll_extra_pe_debug)
1155 {
1156 bfd *a;
1157 struct bfd_link_hash_entry *sym;
1158
1159 printf ("%s()\n", __FUNCTION__);
1160
1161 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1162 printf ("-%s\n", sym->root.string);
1163 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1164
1165 for (a = link_info.input_bfds; a; a = a->link_next)
1166 printf ("*%s\n",a->filename);
1167 }
1168 #endif
1169
1170 /* Pass the wacky PE command line options into the output bfd.
1171 FIXME: This should be done via a function, rather than by
1172 including an internal BFD header. */
1173
1174 if (coff_data (link_info.output_bfd) == NULL
1175 || coff_data (link_info.output_bfd)->pe == 0)
1176 einfo (_("%F%P: cannot perform PE operations on non PE output file '%B'.\n"),
1177 link_info.output_bfd);
1178
1179 pe_data (link_info.output_bfd)->pe_opthdr = pep;
1180 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1181 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1182
1183 /* At this point we must decide whether to use long section names
1184 in the output or not. If the user hasn't explicitly specified
1185 on the command line, we leave it to the default for the format
1186 (object files yes, image files no), except if there is debug
1187 information present; GDB relies on the long section names to
1188 find it, so enable it in that case. */
1189 if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1190 {
1191 /* Iterate over all sections of all input BFDs, checking
1192 for any that begin 'debug_' and are long names. */
1193 LANG_FOR_EACH_INPUT_STATEMENT (is)
1194 {
1195 int found_debug = 0;
1196 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1197 if (found_debug)
1198 {
1199 pep_use_coff_long_section_names = 1;
1200 break;
1201 }
1202 }
1203 }
1204
1205 pep_output_file_set_long_section_names (link_info.output_bfd);
1206
1207 #ifdef DLL_SUPPORT
1208 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1209 pep_fixup_stdcalls ();
1210
1211 pep_process_import_defs (link_info.output_bfd, &link_info);
1212
1213 pep_find_data_imports ();
1214
1215 /* As possibly new symbols are added by imports, we rerun
1216 stdcall/fastcall fixup here. */
1217 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1218 pep_fixup_stdcalls ();
1219
1220 #ifndef TARGET_IS_i386pep
1221 if (link_info.shared)
1222 #else
1223 if (!link_info.relocatable)
1224 #endif
1225 pep_dll_build_sections (link_info.output_bfd, &link_info);
1226
1227 #ifndef TARGET_IS_i386pep
1228 else
1229 pep_exe_build_sections (link_info.output_bfd, &link_info);
1230 #endif
1231 #endif /* DLL_SUPPORT */
1232
1233 {
1234 /* This next chunk of code tries to detect the case where you have
1235 two import libraries for the same DLL (specifically,
1236 symbolically linking libm.a and libc.a in cygwin to
1237 libcygwin.a). In those cases, it's possible for function
1238 thunks from the second implib to be used but without the
1239 head/tail objects, causing an improper import table. We detect
1240 those cases and rename the "other" import libraries to match
1241 the one the head/tail come from, so that the linker will sort
1242 things nicely and produce a valid import table. */
1243
1244 LANG_FOR_EACH_INPUT_STATEMENT (is)
1245 {
1246 if (is->the_bfd->my_archive)
1247 {
1248 int idata2 = 0, reloc_count=0, is_imp = 0;
1249 asection *sec;
1250
1251 /* See if this is an import library thunk. */
1252 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1253 {
1254 if (strcmp (sec->name, ".idata\$2") == 0)
1255 idata2 = 1;
1256 if (CONST_STRNEQ (sec->name, ".idata\$"))
1257 is_imp = 1;
1258 reloc_count += sec->reloc_count;
1259 }
1260
1261 if (is_imp && !idata2 && reloc_count)
1262 {
1263 /* It is, look for the reference to head and see if it's
1264 from our own library. */
1265 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1266 {
1267 int i;
1268 long relsize;
1269 asymbol **symbols;
1270 arelent **relocs;
1271 int nrelocs;
1272
1273 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1274 if (relsize < 1)
1275 break;
1276
1277 if (!bfd_generic_link_read_symbols (is->the_bfd))
1278 {
1279 einfo (_("%B%F: could not read symbols: %E\n"),
1280 is->the_bfd);
1281 return;
1282 }
1283 symbols = bfd_get_outsymbols (is->the_bfd);
1284
1285 relocs = xmalloc ((size_t) relsize);
1286 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1287 relocs, symbols);
1288 if (nrelocs < 0)
1289 {
1290 free (relocs);
1291 einfo ("%X%P: unable to process relocs: %E\n");
1292 return;
1293 }
1294
1295 for (i = 0; i < nrelocs; i++)
1296 {
1297 struct bfd_symbol *s;
1298 struct bfd_link_hash_entry * blhe;
1299 char *other_bfd_filename;
1300 char *n;
1301
1302 s = (relocs[i]->sym_ptr_ptr)[0];
1303
1304 if (s->flags & BSF_LOCAL)
1305 continue;
1306
1307 /* Thunk section with reloc to another bfd. */
1308 blhe = bfd_link_hash_lookup (link_info.hash,
1309 s->name,
1310 FALSE, FALSE, TRUE);
1311
1312 if (blhe == NULL
1313 || blhe->type != bfd_link_hash_defined)
1314 continue;
1315
1316 other_bfd_filename
1317 = blhe->u.def.section->owner->my_archive
1318 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1319 : bfd_get_filename (blhe->u.def.section->owner);
1320
1321 if (strcmp (bfd_get_filename (is->the_bfd->my_archive),
1322 other_bfd_filename) == 0)
1323 continue;
1324
1325 /* Rename this implib to match the other one. */
1326 n = xmalloc (strlen (other_bfd_filename) + 1);
1327 strcpy (n, other_bfd_filename);
1328 is->the_bfd->my_archive->filename = n;
1329 }
1330
1331 free (relocs);
1332 /* Note - we do not free the symbols,
1333 they are now cached in the BFD. */
1334 }
1335 }
1336 }
1337 }
1338 }
1339
1340 {
1341 int is_ms_arch = 0;
1342 bfd *cur_arch = 0;
1343 lang_input_statement_type *is2;
1344 lang_input_statement_type *is3;
1345
1346 /* Careful - this is a shell script. Watch those dollar signs! */
1347 /* Microsoft import libraries have every member named the same,
1348 and not in the right order for us to link them correctly. We
1349 must detect these and rename the members so that they'll link
1350 correctly. There are three types of objects: the head, the
1351 thunks, and the sentinel(s). The head is easy; it's the one
1352 with idata2. We assume that the sentinels won't have relocs,
1353 and the thunks will. It's easier than checking the symbol
1354 table for external references. */
1355 LANG_FOR_EACH_INPUT_STATEMENT (is)
1356 {
1357 if (is->the_bfd->my_archive)
1358 {
1359 char *pnt;
1360 bfd *arch = is->the_bfd->my_archive;
1361
1362 if (cur_arch != arch)
1363 {
1364 cur_arch = arch;
1365 is_ms_arch = 1;
1366
1367 for (is3 = is;
1368 is3 && is3->the_bfd->my_archive == arch;
1369 is3 = (lang_input_statement_type *) is3->next)
1370 {
1371 /* A MS dynamic import library can also contain static
1372 members, so look for the first element with a .dll
1373 extension, and use that for the remainder of the
1374 comparisons. */
1375 pnt = strrchr (is3->the_bfd->filename, '.');
1376 if (pnt != NULL && strcmp (pnt, ".dll") == 0)
1377 break;
1378 }
1379
1380 if (is3 == NULL)
1381 is_ms_arch = 0;
1382 else
1383 {
1384 /* OK, found one. Now look to see if the remaining
1385 (dynamic import) members use the same name. */
1386 for (is2 = is;
1387 is2 && is2->the_bfd->my_archive == arch;
1388 is2 = (lang_input_statement_type *) is2->next)
1389 {
1390 /* Skip static members, ie anything with a .obj
1391 extension. */
1392 pnt = strrchr (is2->the_bfd->filename, '.');
1393 if (pnt != NULL && strcmp (pnt, ".obj") == 0)
1394 continue;
1395
1396 if (strcmp (is3->the_bfd->filename,
1397 is2->the_bfd->filename))
1398 {
1399 is_ms_arch = 0;
1400 break;
1401 }
1402 }
1403 }
1404 }
1405
1406 /* This fragment might have come from an .obj file in a Microsoft
1407 import, and not an actual import record. If this is the case,
1408 then leave the filename alone. */
1409 pnt = strrchr (is->the_bfd->filename, '.');
1410
1411 if (is_ms_arch && (strcmp (pnt, ".dll") == 0))
1412 {
1413 int idata2 = 0, reloc_count=0;
1414 asection *sec;
1415 char *new_name, seq;
1416
1417 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1418 {
1419 if (strcmp (sec->name, ".idata\$2") == 0)
1420 idata2 = 1;
1421 reloc_count += sec->reloc_count;
1422 }
1423
1424 if (idata2) /* .idata2 is the TOC */
1425 seq = 'a';
1426 else if (reloc_count > 0) /* thunks */
1427 seq = 'b';
1428 else /* sentinel */
1429 seq = 'c';
1430
1431 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1432 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1433 is->the_bfd->filename = new_name;
1434
1435 new_name = xmalloc (strlen (is->filename) + 3);
1436 sprintf (new_name, "%s.%c", is->filename, seq);
1437 is->filename = new_name;
1438 }
1439 }
1440 }
1441 }
1442 }
1443 \f
1444 static void
1445 gld_${EMULATION_NAME}_before_allocation (void)
1446 {
1447 is_underscoring ();
1448 before_allocation_default ();
1449 }
1450 \f
1451 #ifdef DLL_SUPPORT
1452 /* This is called when an input file isn't recognized as a BFD. We
1453 check here for .DEF files and pull them in automatically. */
1454
1455 static int
1456 saw_option (char *option)
1457 {
1458 int i;
1459
1460 is_underscoring ();
1461
1462 for (i = 0; init[i].ptr; i++)
1463 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1464 return init[i].inited;
1465 return 0;
1466 }
1467 #endif /* DLL_SUPPORT */
1468
1469 static bfd_boolean
1470 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1471 {
1472 #ifdef DLL_SUPPORT
1473 const char *ext = entry->filename + strlen (entry->filename) - 4;
1474
1475 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
1476 {
1477 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1478
1479 if (pep_def_file)
1480 {
1481 int i, buflen=0, len;
1482 char *buf;
1483
1484 for (i = 0; i < pep_def_file->num_exports; i++)
1485 {
1486 len = strlen (pep_def_file->exports[i].internal_name);
1487 if (buflen < len + 2)
1488 buflen = len + 2;
1489 }
1490
1491 buf = xmalloc (buflen);
1492
1493 for (i = 0; i < pep_def_file->num_exports; i++)
1494 {
1495 struct bfd_link_hash_entry *h;
1496
1497 sprintf (buf, "%s%s", U (""),
1498 pep_def_file->exports[i].internal_name);
1499
1500 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1501 if (h == (struct bfd_link_hash_entry *) NULL)
1502 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1503 if (h->type == bfd_link_hash_new)
1504 {
1505 h->type = bfd_link_hash_undefined;
1506 h->u.undef.abfd = NULL;
1507 bfd_link_add_undef (link_info.hash, h);
1508 }
1509 }
1510 free (buf);
1511
1512 /* def_file_print (stdout, pep_def_file); */
1513 if (pep_def_file->is_dll == 1)
1514 link_info.shared = 1;
1515
1516 if (pep_def_file->base_address != (bfd_vma)(-1))
1517 {
1518 pep.ImageBase
1519 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1520 = init[IMAGEBASEOFF].value
1521 = pep_def_file->base_address;
1522 init[IMAGEBASEOFF].inited = 1;
1523 if (image_base_statement)
1524 image_base_statement->exp = exp_assign ("__image_base__",
1525 exp_intop (pep.ImageBase));
1526 }
1527
1528 if (pep_def_file->stack_reserve != -1
1529 && ! saw_option ("__size_of_stack_reserve__"))
1530 {
1531 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1532 if (pep_def_file->stack_commit != -1)
1533 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1534 }
1535 if (pep_def_file->heap_reserve != -1
1536 && ! saw_option ("__size_of_heap_reserve__"))
1537 {
1538 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1539 if (pep_def_file->heap_commit != -1)
1540 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1541 }
1542 return TRUE;
1543 }
1544 }
1545 #endif
1546 return FALSE;
1547 }
1548
1549 static bfd_boolean
1550 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1551 {
1552 is_underscoring ();
1553 #ifdef DLL_SUPPORT
1554 #ifdef TARGET_IS_i386pep
1555 pep_dll_id_target ("pei-x86-64");
1556 #endif
1557 if (pep_bfd_is_dll (entry->the_bfd))
1558 return pep_implied_import_dll (entry->filename);
1559 #endif
1560 return FALSE;
1561 }
1562
1563 static void
1564 gld_${EMULATION_NAME}_finish (void)
1565 {
1566 is_underscoring ();
1567 finish_default ();
1568
1569 #ifdef DLL_SUPPORT
1570 if (link_info.shared
1571 || (!link_info.relocatable && pep_def_file->num_exports != 0))
1572 {
1573 pep_dll_fill_sections (link_info.output_bfd, &link_info);
1574 if (pep_implib_filename)
1575 pep_dll_generate_implib (pep_def_file, pep_implib_filename, &link_info);
1576 }
1577
1578 if (pep_out_def_filename)
1579 pep_dll_generate_def_file (pep_out_def_filename);
1580 #endif /* DLL_SUPPORT */
1581
1582 /* I don't know where .idata gets set as code, but it shouldn't be. */
1583 {
1584 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
1585
1586 if (asec)
1587 {
1588 asec->flags &= ~SEC_CODE;
1589 asec->flags |= SEC_DATA;
1590 }
1591 }
1592 }
1593
1594 \f
1595 /* Place an orphan section.
1596
1597 We use this to put sections in a reasonable place in the file, and
1598 to ensure that they are aligned as required.
1599
1600 We handle grouped sections here as well. A section named .foo\$nn
1601 goes into the output section .foo. All grouped sections are sorted
1602 by name.
1603
1604 Grouped sections for the default sections are handled by the
1605 default linker script using wildcards, and are sorted by
1606 sort_sections. */
1607
1608 static lang_output_section_statement_type *
1609 gld_${EMULATION_NAME}_place_orphan (asection *s,
1610 const char *secname,
1611 int constraint)
1612 {
1613 const char *orig_secname = secname;
1614 char *dollar = NULL;
1615 lang_output_section_statement_type *os;
1616 lang_statement_list_type add_child;
1617 lang_output_section_statement_type *match_by_name = NULL;
1618 lang_statement_union_type **pl;
1619
1620 /* Look through the script to see where to place this section. */
1621 if (!link_info.relocatable
1622 && (dollar = strchr (secname, '\$')) != NULL)
1623 {
1624 size_t len = dollar - secname;
1625 char *newname = xmalloc (len + 1);
1626 memcpy (newname, secname, len);
1627 newname[len] = '\0';
1628 secname = newname;
1629 }
1630
1631 lang_list_init (&add_child);
1632
1633 os = NULL;
1634 if (constraint == 0)
1635 for (os = lang_output_section_find (secname);
1636 os != NULL;
1637 os = next_matching_output_section_statement (os, 0))
1638 {
1639 /* If we don't match an existing output section, tell
1640 lang_insert_orphan to create a new output section. */
1641 constraint = SPECIAL;
1642
1643 if (os->bfd_section != NULL
1644 && (os->bfd_section->flags == 0
1645 || ((s->flags ^ os->bfd_section->flags)
1646 & (SEC_LOAD | SEC_ALLOC)) == 0))
1647 {
1648 /* We already have an output section statement with this
1649 name, and its bfd section has compatible flags.
1650 If the section already exists but does not have any flags set,
1651 then it has been created by the linker, probably as a result of
1652 a --section-start command line switch. */
1653 lang_add_section (&add_child, s, os);
1654 break;
1655 }
1656
1657 /* Save unused output sections in case we can match them
1658 against orphans later. */
1659 if (os->bfd_section == NULL)
1660 match_by_name = os;
1661 }
1662
1663 /* If we didn't match an active output section, see if we matched an
1664 unused one and use that. */
1665 if (os == NULL && match_by_name)
1666 {
1667 lang_add_section (&match_by_name->children, s, match_by_name);
1668 return match_by_name;
1669 }
1670
1671 if (os == NULL)
1672 {
1673 static struct orphan_save hold[] =
1674 {
1675 { ".text",
1676 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1677 0, 0, 0, 0 },
1678 { ".idata",
1679 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1680 0, 0, 0, 0 },
1681 { ".rdata",
1682 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1683 0, 0, 0, 0 },
1684 { ".data",
1685 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1686 0, 0, 0, 0 },
1687 { ".bss",
1688 SEC_ALLOC,
1689 0, 0, 0, 0 }
1690 };
1691 enum orphan_save_index
1692 {
1693 orphan_text = 0,
1694 orphan_idata,
1695 orphan_rodata,
1696 orphan_data,
1697 orphan_bss
1698 };
1699 static int orphan_init_done = 0;
1700 struct orphan_save *place;
1701 lang_output_section_statement_type *after;
1702 etree_type *address;
1703
1704 if (!orphan_init_done)
1705 {
1706 struct orphan_save *ho;
1707 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1708 if (ho->name != NULL)
1709 {
1710 ho->os = lang_output_section_find (ho->name);
1711 if (ho->os != NULL && ho->os->flags == 0)
1712 ho->os->flags = ho->flags;
1713 }
1714 orphan_init_done = 1;
1715 }
1716
1717 /* Try to put the new output section in a reasonable place based
1718 on the section name and section flags. */
1719
1720 place = NULL;
1721 if ((s->flags & SEC_ALLOC) == 0)
1722 ;
1723 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1724 place = &hold[orphan_bss];
1725 else if ((s->flags & SEC_READONLY) == 0)
1726 place = &hold[orphan_data];
1727 else if ((s->flags & SEC_CODE) == 0)
1728 {
1729 place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
1730 : &hold[orphan_rodata]);
1731 }
1732 else
1733 place = &hold[orphan_text];
1734
1735 after = NULL;
1736 if (place != NULL)
1737 {
1738 if (place->os == NULL)
1739 place->os = lang_output_section_find (place->name);
1740 after = place->os;
1741 if (after == NULL)
1742 after = lang_output_section_find_by_flags (s, &place->os, NULL);
1743 if (after == NULL)
1744 /* *ABS* is always the first output section statement. */
1745 after = (&lang_output_section_statement.head
1746 ->output_section_statement);
1747 }
1748
1749 /* All sections in an executable must be aligned to a page boundary. */
1750 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
1751 os = lang_insert_orphan (s, secname, constraint, after, place, address,
1752 &add_child);
1753 }
1754
1755 /* If the section name has a '\$', sort it with the other '\$'
1756 sections. */
1757 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
1758 {
1759 lang_input_section_type *ls;
1760 const char *lname;
1761
1762 if ((*pl)->header.type != lang_input_section_enum)
1763 continue;
1764
1765 ls = &(*pl)->input_section;
1766
1767 lname = bfd_get_section_name (ls->section->owner, ls->section);
1768 if (strchr (lname, '\$') != NULL
1769 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
1770 break;
1771 }
1772
1773 if (add_child.head != NULL)
1774 {
1775 *add_child.tail = *pl;
1776 *pl = add_child.head;
1777 }
1778
1779 return os;
1780 }
1781
1782 static bfd_boolean
1783 gld_${EMULATION_NAME}_open_dynamic_archive
1784 (const char *arch ATTRIBUTE_UNUSED,
1785 search_dirs_type *search,
1786 lang_input_statement_type *entry)
1787 {
1788 static const struct
1789 {
1790 const char * format;
1791 bfd_boolean use_prefix;
1792 }
1793 libname_fmt [] =
1794 {
1795 /* Preferred explicit import library for dll's. */
1796 { "lib%s.dll.a", FALSE },
1797 /* Alternate explicit import library for dll's. */
1798 { "%s.dll.a", FALSE },
1799 /* "libfoo.a" could be either an import lib or a static lib.
1800 For backwards compatibility, libfoo.a needs to precede
1801 libfoo.dll and foo.dll in the search. */
1802 { "lib%s.a", FALSE },
1803 /* The 'native' spelling of an import lib name is "foo.lib". */
1804 { "%s.lib", FALSE },
1805 #ifdef DLL_SUPPORT
1806 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
1807 { "%s%s.dll", TRUE },
1808 #endif
1809 /* Try "libfoo.dll" (default preferred dll name). */
1810 { "lib%s.dll", FALSE },
1811 /* Finally try 'native' dll name "foo.dll". */
1812 { "%s.dll", FALSE },
1813 /* Note: If adding more formats to this table, make sure to check to
1814 see if their length is longer than libname_fmt[0].format, and if
1815 so, update the call to xmalloc() below. */
1816 { NULL, FALSE }
1817 };
1818 static unsigned int format_max_len = 0;
1819 const char * filename;
1820 char * full_string;
1821 char * base_string;
1822 unsigned int i;
1823
1824
1825 if (! entry->maybe_archive)
1826 return FALSE;
1827
1828 filename = entry->filename;
1829
1830 if (format_max_len == 0)
1831 /* We need to allow space in the memory that we are going to allocate
1832 for the characters in the format string. Since the format array is
1833 static we only need to calculate this information once. In theory
1834 this value could also be computed statically, but this introduces
1835 the possibility for a discrepancy and hence a possible memory
1836 corruption. The lengths we compute here will be too long because
1837 they will include any formating characters (%s) in the strings, but
1838 this will not matter. */
1839 for (i = 0; libname_fmt[i].format; i++)
1840 if (format_max_len < strlen (libname_fmt[i].format))
1841 format_max_len = strlen (libname_fmt[i].format);
1842
1843 full_string = xmalloc (strlen (search->name)
1844 + strlen (filename)
1845 + format_max_len
1846 #ifdef DLL_SUPPORT
1847 + (pep_dll_search_prefix
1848 ? strlen (pep_dll_search_prefix) : 0)
1849 #endif
1850 /* Allow for the terminating NUL and for the path
1851 separator character that is inserted between
1852 search->name and the start of the format string. */
1853 + 2);
1854
1855 sprintf (full_string, "%s/", search->name);
1856 base_string = full_string + strlen (full_string);
1857
1858 for (i = 0; libname_fmt[i].format; i++)
1859 {
1860 #ifdef DLL_SUPPORT
1861 if (libname_fmt[i].use_prefix)
1862 {
1863 if (!pep_dll_search_prefix)
1864 continue;
1865 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
1866 }
1867 else
1868 #endif
1869 sprintf (base_string, libname_fmt[i].format, filename);
1870
1871 if (ldfile_try_open_bfd (full_string, entry))
1872 break;
1873 }
1874
1875 if (!libname_fmt[i].format)
1876 {
1877 free (full_string);
1878 return FALSE;
1879 }
1880
1881 entry->filename = full_string;
1882
1883 return TRUE;
1884 }
1885
1886 static int
1887 gld_${EMULATION_NAME}_find_potential_libraries
1888 (char *name, lang_input_statement_type *entry)
1889 {
1890 return ldfile_open_file_search (name, entry, "", ".lib");
1891 }
1892 \f
1893 static char *
1894 gld_${EMULATION_NAME}_get_script (int *isfile)
1895 EOF
1896 # Scripts compiled in.
1897 # sed commands to quote an ld script as a C string.
1898 sc="-f stringify.sed"
1899
1900 fragment <<EOF
1901 {
1902 *isfile = 0;
1903
1904 if (link_info.relocatable && config.build_constructors)
1905 return
1906 EOF
1907 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1908 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
1909 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1910 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1911 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1912 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1913 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1914 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
1915 echo ' ; else if (link_info.pei386_auto_import == 1 && link_info.pei386_runtime_pseudo_reloc != 2) return' >> e${EMULATION_NAME}.c
1916 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
1917 fi
1918 echo ' ; else return' >> e${EMULATION_NAME}.c
1919 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1920 echo '; }' >> e${EMULATION_NAME}.c
1921
1922 fragment <<EOF
1923
1924
1925 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1926 {
1927 gld_${EMULATION_NAME}_before_parse,
1928 syslib_default,
1929 hll_default,
1930 gld_${EMULATION_NAME}_after_parse,
1931 gld_${EMULATION_NAME}_after_open,
1932 after_allocation_default,
1933 set_output_arch_default,
1934 ldemul_default_target,
1935 gld_${EMULATION_NAME}_before_allocation,
1936 gld_${EMULATION_NAME}_get_script,
1937 "${EMULATION_NAME}",
1938 "${OUTPUT_FORMAT}",
1939 gld_${EMULATION_NAME}_finish,
1940 NULL, /* Create output section statements. */
1941 gld_${EMULATION_NAME}_open_dynamic_archive,
1942 gld_${EMULATION_NAME}_place_orphan,
1943 gld_${EMULATION_NAME}_set_symbols,
1944 NULL, /* parse_args */
1945 gld${EMULATION_NAME}_add_options,
1946 gld${EMULATION_NAME}_handle_option,
1947 gld_${EMULATION_NAME}_unrecognized_file,
1948 gld_${EMULATION_NAME}_list_options,
1949 gld_${EMULATION_NAME}_recognized_file,
1950 gld_${EMULATION_NAME}_find_potential_libraries,
1951 NULL /* new_vers_pattern. */
1952 };
1953 EOF