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