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