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