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
7 OUTPUT_ARCH=${ARCH}:${MACHINE}
9 rm -f e${EMULATION_NAME}.c
10 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
11 cat >>e${EMULATION_NAME}.c <<EOF
12 /* This file is part of GLD, the Gnu Linker.
13 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
14 2005 Free Software Foundation, Inc.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
31 /* The original file generated returned different default scripts depending
32 on whether certain switches were set, but these switches pertain to the
33 Linux system and that particular version of coff. In the NT case, we
34 only determine if the subsystem is console or windows in order to select
35 the correct entry point by default. */
37 #define TARGET_IS_${EMULATION_NAME}
39 /* Do this before including bfd.h, so we prototype the right functions. */
40 #ifdef TARGET_IS_arm_epoc_pe
41 #define bfd_arm_pe_allocate_interworking_sections \
42 bfd_arm_epoc_pe_allocate_interworking_sections
43 #define bfd_arm_pe_get_bfd_for_interworking \
44 bfd_arm_epoc_pe_get_bfd_for_interworking
45 #define bfd_arm_pe_process_before_allocation \
46 bfd_arm_epoc_pe_process_before_allocation
53 #include "libiberty.h"
64 #include "coff/internal.h"
66 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
67 header in generic PE code. */
68 #include "coff/i386.h"
71 /* FIXME: This is a BFD internal header file, and we should not be
73 #include "../bfd/libcoff.h"
77 #include "safe-ctype.h"
79 /* Permit the emulation parameters to override the default section
80 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
81 it seem that include/coff/internal.h should not define
82 PE_DEF_SECTION_ALIGNMENT. */
83 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
84 #undef PE_DEF_SECTION_ALIGNMENT
85 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
88 #if defined(TARGET_IS_i386pe)
91 #if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe) || defined(TARGET_IS_armpe)
95 #if defined(TARGET_IS_i386pe) || ! defined(DLL_SUPPORT)
96 #define PE_DEF_SUBSYSTEM 3
98 #undef NT_EXE_IMAGE_BASE
99 #undef PE_DEF_SECTION_ALIGNMENT
100 #undef PE_DEF_FILE_ALIGNMENT
101 #define NT_EXE_IMAGE_BASE 0x00010000
102 #ifdef TARGET_IS_armpe
103 #define PE_DEF_SECTION_ALIGNMENT 0x00001000
104 #define PE_DEF_SUBSYSTEM 9
106 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
107 #define PE_DEF_SUBSYSTEM 2
109 #define PE_DEF_FILE_ALIGNMENT 0x00000200
113 static struct internal_extra_pe_aouthdr pe;
115 static flagword real_flags = 0;
116 static int support_old_code = 0;
117 static char * thumb_entry_symbol = NULL;
118 static lang_assignment_statement_type *image_base_statement = 0;
121 static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable. */
122 static char *pe_out_def_filename = NULL;
123 static char *pe_implib_filename = NULL;
124 static int pe_enable_auto_image_base = 0;
125 static char *pe_dll_search_prefix = NULL;
128 extern const char *output_filename;
131 gld_${EMULATION_NAME}_before_parse (void)
133 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
134 output_filename = "${EXECUTABLE_NAME:-a.exe}";
136 config.dynamic_link = TRUE;
137 config.has_shared = 1;
138 link_info.pei386_auto_import = -1;
139 link_info.pei386_runtime_pseudo_reloc = -1;
141 #if (PE_DEF_SUBSYSTEM == 9) || (PE_DEF_SUBSYSTEM == 2)
142 #if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
143 lang_default_entry ("WinMainCRTStartup");
145 lang_default_entry ("_WinMainCRTStartup");
148 lang_default_entry ("${ENTRY}");
153 /* PE format extra command line options. */
155 /* Used for setting flags in the PE header. */
156 #define OPTION_BASE_FILE (300 + 1)
157 #define OPTION_DLL (OPTION_BASE_FILE + 1)
158 #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
159 #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
160 #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
161 #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
162 #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
163 #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
164 #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
165 #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
166 #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
167 #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
168 #define OPTION_SUBSYSTEM (OPTION_STACK + 1)
169 #define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
170 #define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
171 #define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1)
172 #define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1)
173 #define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1)
174 #define OPTION_KILL_ATS (OPTION_EXCLUDE_SYMBOLS + 1)
175 #define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1)
176 #define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1)
177 #define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1)
178 #define OPTION_IMPLIB_FILENAME (OPTION_DISABLE_STDCALL_FIXUP + 1)
179 #define OPTION_THUMB_ENTRY (OPTION_IMPLIB_FILENAME + 1)
180 #define OPTION_WARN_DUPLICATE_EXPORTS (OPTION_THUMB_ENTRY + 1)
181 #define OPTION_IMP_COMPAT (OPTION_WARN_DUPLICATE_EXPORTS + 1)
182 #define OPTION_ENABLE_AUTO_IMAGE_BASE (OPTION_IMP_COMPAT + 1)
183 #define OPTION_DISABLE_AUTO_IMAGE_BASE (OPTION_ENABLE_AUTO_IMAGE_BASE + 1)
184 #define OPTION_DLL_SEARCH_PREFIX (OPTION_DISABLE_AUTO_IMAGE_BASE + 1)
185 #define OPTION_NO_DEFAULT_EXCLUDES (OPTION_DLL_SEARCH_PREFIX + 1)
186 #define OPTION_DLL_ENABLE_AUTO_IMPORT (OPTION_NO_DEFAULT_EXCLUDES + 1)
187 #define OPTION_DLL_DISABLE_AUTO_IMPORT (OPTION_DLL_ENABLE_AUTO_IMPORT + 1)
188 #define OPTION_ENABLE_EXTRA_PE_DEBUG (OPTION_DLL_DISABLE_AUTO_IMPORT + 1)
189 #define OPTION_EXCLUDE_LIBS (OPTION_ENABLE_EXTRA_PE_DEBUG + 1)
190 #define OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC \
191 (OPTION_EXCLUDE_LIBS + 1)
192 #define OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC \
193 (OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC + 1)
194 #define OPTION_LARGE_ADDRESS_AWARE \
195 (OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC + 1)
198 gld${EMULATION_NAME}_add_options
199 (int ns ATTRIBUTE_UNUSED, char **shortopts ATTRIBUTE_UNUSED, int nl,
200 struct option **longopts, int nrl ATTRIBUTE_UNUSED,
201 struct option **really_longopts ATTRIBUTE_UNUSED)
203 static const struct option xtra_long[] = {
205 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
206 {"dll", no_argument, NULL, OPTION_DLL},
207 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
208 {"heap", required_argument, NULL, OPTION_HEAP},
209 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
210 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
211 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
212 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
213 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
214 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
215 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
216 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
217 {"stack", required_argument, NULL, OPTION_STACK},
218 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
219 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
220 {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
222 /* getopt allows abbreviations, so we do this to stop it from treating -o
223 as an abbreviation for this option */
224 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
225 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
226 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
227 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
228 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
229 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
230 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
231 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
232 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
233 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
234 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
235 /* getopt() allows abbreviations, so we do this to stop it from
236 treating -c as an abbreviation for these --compat-implib. */
237 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
238 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
239 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
240 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
241 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
242 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
243 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
244 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
245 {"enable-extra-pe-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
246 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
247 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
249 {"large-address-aware", no_argument, NULL, OPTION_LARGE_ADDRESS_AWARE},
250 {NULL, no_argument, NULL, 0}
253 *longopts = (struct option *)
254 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
255 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
258 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
259 parameters which may be input from the command line. */
270 #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
272 static definfo init[] =
274 /* imagebase must be first */
275 #define IMAGEBASEOFF 0
276 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
278 {&dll, sizeof(dll), 0, "__dll__", 0},
279 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
280 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
281 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
282 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
283 D(MajorImageVersion,"__major_image_version__", 1),
284 D(MinorImageVersion,"__minor_image_version__", 0),
285 #ifdef TARGET_IS_armpe
286 D(MajorSubsystemVersion,"__major_subsystem_version__", 2),
288 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
290 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
291 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
292 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000),
293 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
294 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
295 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
296 D(LoaderFlags,"__loader_flags__", 0x0),
297 { NULL, 0, 0, NULL, 0 }
302 gld_${EMULATION_NAME}_list_options (FILE *file)
304 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
305 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
306 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
307 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
308 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
309 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
310 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
311 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
312 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
313 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
314 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
315 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
316 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
317 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
318 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
319 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
321 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
322 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
323 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
324 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
325 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
326 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
327 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
328 fprintf (file, _(" --out-implib <file> Generate import library\n"));
329 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
330 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
331 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
332 create __imp_<SYMBOL> as well.\n"));
333 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
334 unless user specifies one\n"));
335 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
336 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
337 an importlib, use <string><basename>.dll\n\
338 in preference to lib<basename>.dll \n"));
339 fprintf (file, _(" --enable-auto-import Do sophistcated linking of _sym to\n\
340 __imp_sym for DATA references\n"));
341 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
342 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
343 adding pseudo-relocations resolved at\n\
345 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
346 auto-imported DATA.\n"));
347 fprintf (file, _(" --enable-extra-pe-debug Enable verbose debug output when building\n\
348 or linking to DLLs (esp. auto-import)\n"));
350 fprintf (file, _(" --large-address-aware Executable supports virtual addresses\n\
351 greater than 2 gigabytes\n"));
356 set_pe_name (char *name, long val)
360 /* Find the name and set it. */
361 for (i = 0; init[i].ptr; i++)
363 if (strcmp (name, init[i].symbol) == 0)
375 set_pe_subsystem (void)
379 const char *initial_symbol_char;
384 unsigned long temp_subsystem;
393 { "native", 1, "NtProcessStartup" },
394 { "windows", 2, "WinMainCRTStartup" },
395 { "console", 3, "mainCRTStartup" },
396 { "posix", 7, "__PosixProcessStartup"},
397 { "wince", 9, "_WinMainCRTStartup" },
398 { "xbox", 14, "mainCRTStartup" },
401 /* Entry point name for arbitrary subsystem numbers. */
402 static const char default_entry[] = "mainCRTStartup";
404 /* Check for the presence of a version number. */
405 sver = strchr (optarg, ':');
407 len = strlen (optarg);
411 set_pe_name ("__major_subsystem_version__",
412 strtoul (sver + 1, &end, 0));
414 set_pe_name ("__minor_subsystem_version__",
415 strtoul (end + 1, &end, 0));
417 einfo (_("%P: warning: bad version number in -subsystem option\n"));
420 /* Check for numeric subsystem. */
421 temp_subsystem = strtoul (optarg, & end, 0);
422 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
424 /* Search list for a numeric match to use its entry point. */
425 for (i = 0; v[i].name; i++)
426 if (v[i].value == (int) temp_subsystem)
429 /* If no match, use the default. */
430 if (v[i].name != NULL)
433 entry = default_entry;
435 /* Use this subsystem. */
436 subsystem = (int) temp_subsystem;
440 /* Search for subsystem by name. */
441 for (i = 0; v[i].name; i++)
442 if (strncmp (optarg, v[i].name, len) == 0
443 && v[i].name[len] == '\0')
446 if (v[i].name == NULL)
448 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
453 subsystem = v[i].value;
456 set_pe_name ("__subsystem__", subsystem);
458 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
459 if (*initial_symbol_char != '\0')
463 /* lang_default_entry expects its argument to be permanently
464 allocated, so we don't free this string. */
465 alc_entry = xmalloc (strlen (initial_symbol_char)
468 strcpy (alc_entry, initial_symbol_char);
469 strcat (alc_entry, entry);
473 lang_default_entry (entry);
480 set_pe_value (char *name)
484 set_pe_name (name, strtoul (optarg, &end, 0));
487 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
494 set_pe_stack_heap (char *resname, char *comname)
496 set_pe_value (resname);
501 set_pe_value (comname);
504 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
509 gld${EMULATION_NAME}_handle_option (int optc)
516 case OPTION_BASE_FILE:
517 link_info.base_file = fopen (optarg, FOPEN_WB);
518 if (link_info.base_file == NULL)
520 /* xgettext:c-format */
521 fprintf (stderr, _("%s: Can't open base file %s\n"),
522 program_name, optarg);
529 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
532 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
534 case OPTION_SUBSYSTEM:
537 case OPTION_MAJOR_OS_VERSION:
538 set_pe_value ("__major_os_version__");
540 case OPTION_MINOR_OS_VERSION:
541 set_pe_value ("__minor_os_version__");
543 case OPTION_MAJOR_SUBSYSTEM_VERSION:
544 set_pe_value ("__major_subsystem_version__");
546 case OPTION_MINOR_SUBSYSTEM_VERSION:
547 set_pe_value ("__minor_subsystem_version__");
549 case OPTION_MAJOR_IMAGE_VERSION:
550 set_pe_value ("__major_image_version__");
552 case OPTION_MINOR_IMAGE_VERSION:
553 set_pe_value ("__minor_image_version__");
555 case OPTION_FILE_ALIGNMENT:
556 set_pe_value ("__file_alignment__");
558 case OPTION_SECTION_ALIGNMENT:
559 set_pe_value ("__section_alignment__");
562 set_pe_name ("__dll__", 1);
564 case OPTION_IMAGE_BASE:
565 set_pe_value ("__image_base__");
567 case OPTION_SUPPORT_OLD_CODE:
568 support_old_code = 1;
570 case OPTION_THUMB_ENTRY:
571 thumb_entry_symbol = optarg;
575 pe_out_def_filename = xstrdup (optarg);
577 case OPTION_EXPORT_ALL:
578 pe_dll_export_everything = 1;
580 case OPTION_EXCLUDE_SYMBOLS:
581 pe_dll_add_excludes (optarg, 0);
583 case OPTION_EXCLUDE_LIBS:
584 pe_dll_add_excludes (optarg, 1);
586 case OPTION_KILL_ATS:
589 case OPTION_STDCALL_ALIASES:
590 pe_dll_stdcall_aliases = 1;
592 case OPTION_ENABLE_STDCALL_FIXUP:
593 pe_enable_stdcall_fixup = 1;
595 case OPTION_DISABLE_STDCALL_FIXUP:
596 pe_enable_stdcall_fixup = 0;
598 case OPTION_IMPLIB_FILENAME:
599 pe_implib_filename = xstrdup (optarg);
601 case OPTION_WARN_DUPLICATE_EXPORTS:
602 pe_dll_warn_dup_exports = 1;
604 case OPTION_IMP_COMPAT:
605 pe_dll_compat_implib = 1;
607 case OPTION_ENABLE_AUTO_IMAGE_BASE:
608 pe_enable_auto_image_base = 1;
610 case OPTION_DISABLE_AUTO_IMAGE_BASE:
611 pe_enable_auto_image_base = 0;
613 case OPTION_DLL_SEARCH_PREFIX:
614 pe_dll_search_prefix = xstrdup (optarg);
616 case OPTION_NO_DEFAULT_EXCLUDES:
617 pe_dll_do_default_excludes = 0;
619 case OPTION_DLL_ENABLE_AUTO_IMPORT:
620 link_info.pei386_auto_import = 1;
622 case OPTION_DLL_DISABLE_AUTO_IMPORT:
623 link_info.pei386_auto_import = 0;
625 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
626 link_info.pei386_runtime_pseudo_reloc = 1;
628 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
629 link_info.pei386_runtime_pseudo_reloc = 0;
631 case OPTION_ENABLE_EXTRA_PE_DEBUG:
632 pe_dll_extra_pe_debug = 1;
635 case OPTION_LARGE_ADDRESS_AWARE:
636 real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
645 strhash (const char *str)
647 const unsigned char *s;
654 s = (const unsigned char *) str;
655 while ((c = *s++) != '\0')
657 hash += c + (c << 17);
661 hash += len + (len << 17);
667 /* Use the output file to create a image base for relocatable DLLs. */
670 compute_dll_image_base (const char *ofile)
672 unsigned long hash = strhash (ofile);
673 return 0x61300000 + ((hash << 16) & 0x0FFC0000);
677 /* Assign values to the special symbols before the linker script is
681 gld_${EMULATION_NAME}_set_symbols (void)
683 /* Run through and invent symbols for all the
684 names and insert the defaults. */
686 lang_statement_list_type *save;
688 if (!init[IMAGEBASEOFF].inited)
690 if (link_info.relocatable)
691 init[IMAGEBASEOFF].value = 0;
692 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
694 init[IMAGEBASEOFF].value = (pe_enable_auto_image_base) ?
695 compute_dll_image_base (output_filename) : NT_DLL_IMAGE_BASE;
697 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
700 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
703 /* Don't do any symbol assignments if this is a relocatable link. */
704 if (link_info.relocatable)
707 /* Glue the assignments into the abs section. */
710 stat_ptr = &(abs_output_section->children);
712 for (j = 0; init[j].ptr; j++)
714 long val = init[j].value;
715 lang_assignment_statement_type *rv;
716 rv = lang_add_assignment (exp_assop ('=', init[j].symbol,
718 if (init[j].size == sizeof (short))
719 *(short *) init[j].ptr = val;
720 else if (init[j].size == sizeof (int))
721 *(int *) init[j].ptr = val;
722 else if (init[j].size == sizeof (long))
723 *(long *) init[j].ptr = val;
724 /* This might be a long long or other special type. */
725 else if (init[j].size == sizeof (bfd_vma))
726 *(bfd_vma *) init[j].ptr = val;
728 if (j == IMAGEBASEOFF)
729 image_base_statement = rv;
731 /* Restore the pointer. */
734 if (pe.FileAlignment >
737 einfo (_("%P: warning, file alignment > section alignment.\n"));
741 /* This is called after the linker script and the command line options
745 gld_${EMULATION_NAME}_after_parse (void)
747 /* The Windows libraries are designed for the linker to treat the
748 entry point as an undefined symbol. Otherwise, the .obj that
749 defines mainCRTStartup is brought in because it is the first
750 encountered in libc.lib and it has other symbols in it which will
751 be pulled in by the link process. To avoid this, we act as
752 though the user specified -u with the entry point symbol.
754 This function is called after the linker script and command line
755 options have been read, so at this point we know the right entry
756 point. This function is called before the input files are
757 opened, so registering the symbol as undefined will make a
760 if (! link_info.relocatable && entry_symbol.name != NULL)
761 ldlang_add_undef (entry_symbol.name);
764 /* pe-dll.c directly accesses pe_data_import_dll,
765 so it must be defined outside of #ifdef DLL_SUPPORT.
766 Note - this variable is deliberately not initialised.
767 This allows it to be treated as a common varaible, and only
768 exist in one incarnation in a multiple target enabled linker. */
769 char * pe_data_import_dll;
772 static struct bfd_link_hash_entry *pe_undef_found_sym;
775 pe_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
780 sl = strlen (string);
781 if (h->type == bfd_link_hash_defined
782 && strncmp (h->root.string, string, sl) == 0
783 && h->root.string[sl] == '@')
785 pe_undef_found_sym = h;
792 pe_fixup_stdcalls (void)
794 static int gave_warning_message = 0;
795 struct bfd_link_hash_entry *undef, *sym;
797 if (pe_dll_extra_pe_debug)
798 printf ("%s\n", __FUNCTION__);
800 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
801 if (undef->type == bfd_link_hash_undefined)
803 char* at = strchr (undef->root.string, '@');
804 int lead_at = (*undef->root.string == '@');
805 /* For now, don't try to fixup fastcall symbols. */
809 /* The symbol is a stdcall symbol, so let's look for a
810 cdecl symbol with the same name and resolve to that. */
811 char *cname = xstrdup (undef->root.string /* + lead_at */);
812 at = strchr (cname, '@');
814 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
816 if (sym && sym->type == bfd_link_hash_defined)
818 undef->type = bfd_link_hash_defined;
819 undef->u.def.value = sym->u.def.value;
820 undef->u.def.section = sym->u.def.section;
822 if (pe_enable_stdcall_fixup == -1)
824 einfo (_("Warning: resolving %s by linking to %s\n"),
825 undef->root.string, cname);
826 if (! gave_warning_message)
828 gave_warning_message = 1;
829 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
830 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
837 /* The symbol is a cdecl symbol, so we look for stdcall
838 symbols - which means scanning the whole symbol table. */
839 pe_undef_found_sym = 0;
840 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
841 (char *) undef->root.string);
842 sym = pe_undef_found_sym;
845 undef->type = bfd_link_hash_defined;
846 undef->u.def.value = sym->u.def.value;
847 undef->u.def.section = sym->u.def.section;
849 if (pe_enable_stdcall_fixup == -1)
851 einfo (_("Warning: resolving %s by linking to %s\n"),
852 undef->root.string, sym->root.string);
853 if (! gave_warning_message)
855 gave_warning_message = 1;
856 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
857 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
866 make_import_fixup (arelent *rel, asection *s)
868 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
871 if (pe_dll_extra_pe_debug)
872 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
873 (long) rel->address, (long) rel->addend);
875 if (! bfd_get_section_contents (s->owner, s, addend, rel->address, sizeof (addend)))
876 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
877 s->owner, s, rel->address);
879 pe_create_import_fixup (rel, s, bfd_get_32 (s->owner, addend));
885 pe_find_data_imports (void)
887 struct bfd_link_hash_entry *undef, *sym;
889 if (link_info.pei386_auto_import == 0)
892 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
894 if (undef->type == bfd_link_hash_undefined)
896 /* C++ symbols are *long*. */
899 if (pe_dll_extra_pe_debug)
900 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
902 sprintf (buf, "__imp_%s", undef->root.string);
904 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
906 if (sym && sym->type == bfd_link_hash_defined)
908 bfd *b = sym->u.def.section->owner;
910 int nsyms, symsize, i;
912 if (link_info.pei386_auto_import == -1)
913 info_msg (_("Info: resolving %s by linking to %s (auto-import)\n"),
914 undef->root.string, buf);
916 symsize = bfd_get_symtab_upper_bound (b);
917 symbols = (asymbol **) xmalloc (symsize);
918 nsyms = bfd_canonicalize_symtab (b, symbols);
920 for (i = 0; i < nsyms; i++)
922 if (memcmp (symbols[i]->name, "__head_",
923 sizeof ("__head_") - 1))
926 if (pe_dll_extra_pe_debug)
927 printf ("->%s\n", symbols[i]->name);
929 pe_data_import_dll = (char*) (symbols[i]->name +
930 sizeof ("__head_") - 1);
934 pe_walk_relocs_of_symbol (&link_info, undef->root.string,
937 /* Let's differentiate it somehow from defined. */
938 undef->type = bfd_link_hash_defweak;
939 /* We replace original name with __imp_ prefixed, this
940 1) may trash memory 2) leads to duplicate symbol generation.
941 Still, IMHO it's better than having name poluted. */
942 undef->root.string = sym->root.string;
943 undef->u.def.value = sym->u.def.value;
944 undef->u.def.section = sym->u.def.section;
951 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
953 if (pe_dll_extra_pe_debug)
954 printf ("+%s\n", h->string);
958 #endif /* DLL_SUPPORT */
962 gld_${EMULATION_NAME}_after_open (void)
965 if (pe_dll_extra_pe_debug)
968 struct bfd_link_hash_entry *sym;
970 printf ("%s()\n", __FUNCTION__);
972 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
973 printf ("-%s\n", sym->root.string);
974 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
976 for (a = link_info.input_bfds; a; a = a->link_next)
977 printf ("*%s\n",a->filename);
981 /* Pass the wacky PE command line options into the output bfd.
982 FIXME: This should be done via a function, rather than by
983 including an internal BFD header. */
985 if (coff_data (output_bfd) == NULL || coff_data (output_bfd)->pe == 0)
986 einfo (_("%F%P: PE operations on non PE file.\n"));
988 pe_data (output_bfd)->pe_opthdr = pe;
989 pe_data (output_bfd)->dll = init[DLLOFF].value;
990 pe_data (output_bfd)->real_flags |= real_flags;
993 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
994 pe_fixup_stdcalls ();
996 pe_process_import_defs (output_bfd, & link_info);
998 pe_find_data_imports ();
1000 #if ! (defined (TARGET_IS_i386pe) || defined (TARGET_IS_armpe))
1001 if (link_info.shared)
1003 if (!link_info.relocatable)
1005 pe_dll_build_sections (output_bfd, &link_info);
1007 #ifndef TARGET_IS_i386pe
1008 #ifndef TARGET_IS_armpe
1010 pe_exe_build_sections (output_bfd, &link_info);
1015 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
1016 if (strstr (bfd_get_target (output_bfd), "arm") == NULL)
1018 /* The arm backend needs special fields in the output hash structure.
1019 These will only be created if the output format is an arm format,
1020 hence we do not support linking and changing output formats at the
1021 same time. Use a link followed by objcopy to change output formats. */
1022 einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n");
1026 /* Find a BFD that can hold the interworking stubs. */
1027 LANG_FOR_EACH_INPUT_STATEMENT (is)
1029 if (bfd_arm_pe_get_bfd_for_interworking (is->the_bfd, & link_info))
1036 /* This next chunk of code tries to detect the case where you have
1037 two import libraries for the same DLL (specifically,
1038 symbolically linking libm.a and libc.a in cygwin to
1039 libcygwin.a). In those cases, it's possible for function
1040 thunks from the second implib to be used but without the
1041 head/tail objects, causing an improper import table. We detect
1042 those cases and rename the "other" import libraries to match
1043 the one the head/tail come from, so that the linker will sort
1044 things nicely and produce a valid import table. */
1046 LANG_FOR_EACH_INPUT_STATEMENT (is)
1048 if (is->the_bfd->my_archive)
1050 int idata2 = 0, reloc_count=0, is_imp = 0;
1053 /* See if this is an import library thunk. */
1054 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1056 if (strcmp (sec->name, ".idata\$2") == 0)
1058 if (strncmp (sec->name, ".idata\$", 7) == 0)
1060 reloc_count += sec->reloc_count;
1063 if (is_imp && !idata2 && reloc_count)
1065 /* It is, look for the reference to head and see if it's
1066 from our own library. */
1067 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1076 symsize = bfd_get_symtab_upper_bound (is->the_bfd);
1079 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1083 symbols = (asymbol **) xmalloc (symsize);
1084 symsize = bfd_canonicalize_symtab (is->the_bfd, symbols);
1087 einfo ("%X%P: unable to process symbols: %E");
1091 relocs = (arelent **) xmalloc ((size_t) relsize);
1092 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1097 einfo ("%X%P: unable to process relocs: %E");
1101 for (i = 0; i < nrelocs; i++)
1103 struct bfd_symbol *s;
1104 struct bfd_link_hash_entry * blhe;
1105 char *other_bfd_filename;
1108 s = (relocs[i]->sym_ptr_ptr)[0];
1110 if (s->flags & BSF_LOCAL)
1113 /* Thunk section with reloc to another bfd. */
1114 blhe = bfd_link_hash_lookup (link_info.hash,
1116 FALSE, FALSE, TRUE);
1119 || blhe->type != bfd_link_hash_defined)
1123 = blhe->u.def.section->owner->my_archive
1124 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1125 : bfd_get_filename (blhe->u.def.section->owner);
1127 if (strcmp (bfd_get_filename (is->the_bfd->my_archive),
1128 other_bfd_filename) == 0)
1131 /* Rename this implib to match the other one. */
1132 n = xmalloc (strlen (other_bfd_filename) + 1);
1133 strcpy (n, other_bfd_filename);
1134 is->the_bfd->my_archive->filename = n;
1138 /* Note - we do not free the symbols,
1139 they are now cached in the BFD. */
1149 lang_input_statement_type *is2;
1150 lang_input_statement_type *is3;
1152 /* Careful - this is a shell script. Watch those dollar signs! */
1153 /* Microsoft import libraries have every member named the same,
1154 and not in the right order for us to link them correctly. We
1155 must detect these and rename the members so that they'll link
1156 correctly. There are three types of objects: the head, the
1157 thunks, and the sentinel(s). The head is easy; it's the one
1158 with idata2. We assume that the sentinels won't have relocs,
1159 and the thunks will. It's easier than checking the symbol
1160 table for external references. */
1161 LANG_FOR_EACH_INPUT_STATEMENT (is)
1163 if (is->the_bfd->my_archive)
1166 bfd *arch = is->the_bfd->my_archive;
1168 if (cur_arch != arch)
1174 is3 && is3->the_bfd->my_archive == arch;
1175 is3 = (lang_input_statement_type *) is3->next)
1177 /* A MS dynamic import library can also contain static
1178 members, so look for the first element with a .dll
1179 extension, and use that for the remainder of the
1181 pnt = strrchr (is3->the_bfd->filename, '.');
1182 if (pnt != NULL && strcmp (pnt, ".dll") == 0)
1190 /* OK, found one. Now look to see if the remaining
1191 (dynamic import) members use the same name. */
1193 is2 && is2->the_bfd->my_archive == arch;
1194 is2 = (lang_input_statement_type *) is2->next)
1196 /* Skip static members, ie anything with a .obj
1198 pnt = strrchr (is2->the_bfd->filename, '.');
1199 if (pnt != NULL && strcmp (pnt, ".obj") == 0)
1202 if (strcmp (is3->the_bfd->filename,
1203 is2->the_bfd->filename))
1212 /* This fragment might have come from an .obj file in a Microsoft
1213 import, and not an actual import record. If this is the case,
1214 then leave the filename alone. */
1215 pnt = strrchr (is->the_bfd->filename, '.');
1217 if (is_ms_arch && (strcmp (pnt, ".dll") == 0))
1219 int idata2 = 0, reloc_count=0;
1221 char *new_name, seq;
1223 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1225 if (strcmp (sec->name, ".idata\$2") == 0)
1227 reloc_count += sec->reloc_count;
1230 if (idata2) /* .idata2 is the TOC */
1232 else if (reloc_count > 0) /* thunks */
1237 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1238 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1239 is->the_bfd->filename = new_name;
1241 new_name = xmalloc (strlen (is->filename) + 3);
1242 sprintf (new_name, "%s.%c", is->filename, seq);
1243 is->filename = new_name;
1251 gld_${EMULATION_NAME}_before_allocation (void)
1253 #ifdef TARGET_IS_ppcpe
1254 /* Here we rummage through the found bfds to collect toc information. */
1256 LANG_FOR_EACH_INPUT_STATEMENT (is)
1258 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
1260 /* xgettext:c-format */
1261 einfo (_("Errors encountered processing file %s\n"), is->filename);
1266 /* We have seen it all. Allocate it, and carry on. */
1267 ppc_allocate_toc_section (&link_info);
1268 #endif /* TARGET_IS_ppcpe */
1270 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
1271 /* FIXME: we should be able to set the size of the interworking stub
1274 Here we rummage through the found bfds to collect glue
1275 information. FIXME: should this be based on a command line
1276 option? krk@cygnus.com. */
1278 LANG_FOR_EACH_INPUT_STATEMENT (is)
1280 if (! bfd_arm_pe_process_before_allocation
1281 (is->the_bfd, & link_info, support_old_code))
1283 /* xgettext:c-format */
1284 einfo (_("Errors encountered processing file %s for interworking"),
1290 /* We have seen it all. Allocate it, and carry on. */
1291 bfd_arm_pe_allocate_interworking_sections (& link_info);
1292 #endif /* TARGET_IS_armpe */
1294 before_allocation_default ();
1298 /* This is called when an input file isn't recognized as a BFD. We
1299 check here for .DEF files and pull them in automatically. */
1302 saw_option (char *option)
1306 for (i = 0; init[i].ptr; i++)
1307 if (strcmp (init[i].symbol, option) == 0)
1308 return init[i].inited;
1311 #endif /* DLL_SUPPORT */
1314 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1317 const char *ext = entry->filename + strlen (entry->filename) - 4;
1319 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
1321 pe_def_file = def_file_parse (entry->filename, pe_def_file);
1325 int i, buflen=0, len;
1328 for (i = 0; i < pe_def_file->num_exports; i++)
1330 len = strlen (pe_def_file->exports[i].internal_name);
1331 if (buflen < len + 2)
1335 buf = (char *) xmalloc (buflen);
1337 for (i = 0; i < pe_def_file->num_exports; i++)
1339 struct bfd_link_hash_entry *h;
1341 sprintf (buf, "_%s", pe_def_file->exports[i].internal_name);
1343 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1344 if (h == (struct bfd_link_hash_entry *) NULL)
1345 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1346 if (h->type == bfd_link_hash_new)
1348 h->type = bfd_link_hash_undefined;
1349 h->u.undef.abfd = NULL;
1350 bfd_link_add_undef (link_info.hash, h);
1355 /* def_file_print (stdout, pe_def_file); */
1356 if (pe_def_file->is_dll == 1)
1357 link_info.shared = 1;
1359 if (pe_def_file->base_address != (bfd_vma)(-1))
1362 pe_data (output_bfd)->pe_opthdr.ImageBase =
1363 init[IMAGEBASEOFF].value = pe_def_file->base_address;
1364 init[IMAGEBASEOFF].inited = 1;
1365 if (image_base_statement)
1366 image_base_statement->exp =
1367 exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
1370 if (pe_def_file->stack_reserve != -1
1371 && ! saw_option ("__size_of_stack_reserve__"))
1373 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
1374 if (pe_def_file->stack_commit != -1)
1375 pe.SizeOfStackCommit = pe_def_file->stack_commit;
1377 if (pe_def_file->heap_reserve != -1
1378 && ! saw_option ("__size_of_heap_reserve__"))
1380 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
1381 if (pe_def_file->heap_commit != -1)
1382 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
1392 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1395 #ifdef TARGET_IS_i386pe
1396 pe_dll_id_target ("pei-i386");
1398 #ifdef TARGET_IS_shpe
1399 pe_dll_id_target ("pei-shl");
1401 #ifdef TARGET_IS_mipspe
1402 pe_dll_id_target ("pei-mips");
1404 #ifdef TARGET_IS_armpe
1405 pe_dll_id_target ("pei-arm-little");
1407 if (bfd_get_format (entry->the_bfd) == bfd_object)
1409 char fbuf[LD_PATHMAX + 1];
1412 if (REALPATH (entry->filename, fbuf) == NULL)
1413 strncpy (fbuf, entry->filename, sizeof (fbuf));
1415 ext = fbuf + strlen (fbuf) - 4;
1417 if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0)
1418 return pe_implied_import_dll (fbuf);
1425 gld_${EMULATION_NAME}_finish (void)
1427 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
1428 struct bfd_link_hash_entry * h;
1430 if (thumb_entry_symbol != NULL)
1432 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol,
1433 FALSE, FALSE, TRUE);
1435 if (h != (struct bfd_link_hash_entry *) NULL
1436 && (h->type == bfd_link_hash_defined
1437 || h->type == bfd_link_hash_defweak)
1438 && h->u.def.section->output_section != NULL)
1440 static char buffer[32];
1443 /* Special procesing is required for a Thumb entry symbol. The
1444 bottom bit of its address must be set. */
1445 val = (h->u.def.value
1446 + bfd_get_section_vma (output_bfd,
1447 h->u.def.section->output_section)
1448 + h->u.def.section->output_offset);
1452 /* Now convert this value into a string and store it in entry_symbol
1453 where the lang_finish() function will pick it up. */
1457 sprintf_vma (buffer + 2, val);
1459 if (entry_symbol.name != NULL && entry_from_cmdline)
1460 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
1461 thumb_entry_symbol, entry_symbol.name);
1462 entry_symbol.name = buffer;
1465 einfo (_("%P: warning: connot find thumb start symbol %s\n"), thumb_entry_symbol);
1467 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) */
1472 if (link_info.shared
1473 #if !defined(TARGET_IS_shpe) && !defined(TARGET_IS_mipspe)
1474 || (!link_info.relocatable && pe_def_file->num_exports != 0)
1478 pe_dll_fill_sections (output_bfd, &link_info);
1479 if (pe_implib_filename)
1480 pe_dll_generate_implib (pe_def_file, pe_implib_filename);
1482 #if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe)
1483 /* ARM doesn't need relocs. */
1486 pe_exe_fill_sections (output_bfd, &link_info);
1490 if (pe_out_def_filename)
1491 pe_dll_generate_def_file (pe_out_def_filename);
1492 #endif /* DLL_SUPPORT */
1494 /* I don't know where .idata gets set as code, but it shouldn't be. */
1496 asection *asec = bfd_get_section_by_name (output_bfd, ".idata");
1500 asec->flags &= ~SEC_CODE;
1501 asec->flags |= SEC_DATA;
1507 /* Place an orphan section.
1509 We use this to put sections in a reasonable place in the file, and
1510 to ensure that they are aligned as required.
1512 We handle grouped sections here as well. A section named .foo$nn
1513 goes into the output section .foo. All grouped sections are sorted
1516 Grouped sections for the default sections are handled by the
1517 default linker script using wildcards, and are sorted by
1521 gld_${EMULATION_NAME}_place_orphan (asection *s)
1523 const char *secname;
1524 const char *orig_secname;
1525 char *dollar = NULL;
1526 lang_output_section_statement_type *os;
1527 lang_statement_list_type add_child;
1529 secname = bfd_get_section_name (s->owner, s);
1531 /* Look through the script to see where to place this section. */
1532 orig_secname = secname;
1533 if (!link_info.relocatable
1534 && (dollar = strchr (secname, '$')) != NULL)
1536 size_t len = dollar - orig_secname;
1537 char *newname = xmalloc (len + 1);
1538 memcpy (newname, orig_secname, len);
1539 newname[len] = '\0';
1543 os = lang_output_section_find (secname);
1545 lang_list_init (&add_child);
1548 && (os->bfd_section == NULL
1549 || os->bfd_section->flags == 0
1550 || ((s->flags ^ os->bfd_section->flags)
1551 & (SEC_LOAD | SEC_ALLOC)) == 0))
1553 /* We already have an output section statement with this
1554 name, and its bfd section, if any, has compatible flags.
1555 If the section already exists but does not have any flags set,
1556 then it has been created by the linker, probably as a result of
1557 a --section-start command line switch. */
1558 lang_add_section (&add_child, s, os);
1562 static struct orphan_save hold[] =
1565 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1568 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1571 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1577 enum orphan_save_index
1584 static int orphan_init_done = 0;
1585 struct orphan_save *place;
1586 lang_output_section_statement_type *after;
1587 etree_type *address;
1589 if (!orphan_init_done)
1591 struct orphan_save *ho;
1592 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1593 if (ho->name != NULL)
1595 ho->os = lang_output_section_find (ho->name);
1596 if (ho->os != NULL && ho->os->flags == 0)
1597 ho->os->flags = ho->flags;
1599 orphan_init_done = 1;
1602 /* Try to put the new output section in a reasonable place based
1603 on the section name and section flags. */
1606 if ((s->flags & SEC_ALLOC) == 0)
1608 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1609 place = &hold[orphan_bss];
1610 else if ((s->flags & SEC_READONLY) == 0)
1611 place = &hold[orphan_data];
1612 else if ((s->flags & SEC_CODE) == 0)
1613 place = &hold[orphan_rodata];
1615 place = &hold[orphan_text];
1620 if (place->os == NULL)
1621 place->os = lang_output_section_find (place->name);
1624 after = lang_output_section_find_by_flags (s, &place->os, NULL);
1626 /* *ABS* is always the first output section statement. */
1627 after = (&lang_output_section_statement.head
1628 ->output_section_statement);
1631 /* Choose a unique name for the section. This will be needed if the
1632 same section name appears in the input file with different
1633 loadable or allocatable characteristics. */
1634 if (bfd_get_section_by_name (output_bfd, secname) != NULL)
1636 static int count = 1;
1637 secname = bfd_get_unique_section_name (output_bfd, secname, &count);
1638 if (secname == NULL)
1639 einfo ("%F%P: place_orphan failed: %E\n");
1642 /* All sections in an executable must be aligned to a page boundary. */
1643 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
1644 os = lang_insert_orphan (s, secname, after, place, address, &add_child);
1648 lang_statement_union_type **pl = &os->children.head;
1652 bfd_boolean found_dollar;
1654 /* The section name has a '$'. Sort it with the other '$'
1656 found_dollar = FALSE;
1657 for ( ; *pl != NULL; pl = &(*pl)->header.next)
1659 lang_input_section_type *ls;
1662 if ((*pl)->header.type != lang_input_section_enum)
1665 ls = &(*pl)->input_section;
1667 lname = bfd_get_section_name (ls->section->owner, ls->section);
1668 if (strchr (lname, '$') == NULL)
1675 found_dollar = TRUE;
1676 if (strcmp (orig_secname, lname) < 0)
1682 if (add_child.head != NULL)
1684 add_child.head->header.next = *pl;
1685 *pl = add_child.head;
1693 gld_${EMULATION_NAME}_open_dynamic_archive
1694 (const char *arch ATTRIBUTE_UNUSED, search_dirs_type *search,
1695 lang_input_statement_type *entry)
1697 const char * filename;
1700 if (! entry->is_archive)
1703 filename = entry->filename;
1705 string = (char *) xmalloc (strlen (search->name)
1707 + sizeof "/lib.a.dll"
1709 + (pe_dll_search_prefix ? strlen (pe_dll_search_prefix) : 0)
1713 /* Try "libfoo.dll.a" first (preferred explicit import library for dll's. */
1714 sprintf (string, "%s/lib%s.dll.a", search->name, filename);
1716 if (! ldfile_try_open_bfd (string, entry))
1718 /* Try "foo.dll.a" next (alternate explicit import library for dll's. */
1719 sprintf (string, "%s/%s.dll.a", search->name, filename);
1720 if (! ldfile_try_open_bfd (string, entry))
1722 /* Try libfoo.a next. Normally, this would be interpreted as a static
1723 library, but it *could* be an import library. For backwards compatibility,
1724 libfoo.a needs to ==precede== libfoo.dll and foo.dll in the search,
1725 or sometimes errors occur when building legacy packages.
1727 Putting libfoo.a here means that in a failure case (i.e. the library
1728 -lfoo is not found) we will search for libfoo.a twice before
1729 giving up -- once here, and once when searching for a "static" lib.
1730 for a "static" lib. */
1731 /* Try "libfoo.a" (import lib, or static lib, but must
1732 take precedence over dll's). */
1733 sprintf (string, "%s/lib%s.a", search->name, filename);
1734 if (! ldfile_try_open_bfd (string, entry))
1737 if (pe_dll_search_prefix)
1739 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
1740 sprintf (string, "%s/%s%s.dll", search->name, pe_dll_search_prefix, filename);
1741 if (! ldfile_try_open_bfd (string, entry))
1743 /* Try "libfoo.dll" (default preferred dll name). */
1744 sprintf (string, "%s/lib%s.dll", search->name, filename);
1745 if (! ldfile_try_open_bfd (string, entry))
1747 /* Finally, try "foo.dll" (alternate dll name). */
1748 sprintf (string, "%s/%s.dll", search->name, filename);
1749 if (! ldfile_try_open_bfd (string, entry))
1757 else /* pe_dll_search_prefix not specified. */
1760 /* Try "libfoo.dll" (preferred dll name). */
1761 sprintf (string, "%s/lib%s.dll", search->name, filename);
1762 if (! ldfile_try_open_bfd (string, entry))
1764 /* Finally, try "foo.dll" (alternate dll name). */
1765 sprintf (string, "%s/%s.dll", search->name, filename);
1766 if (! ldfile_try_open_bfd (string, entry))
1777 entry->filename = string;
1783 gld_${EMULATION_NAME}_find_potential_libraries
1784 (char *name, lang_input_statement_type *entry)
1786 return ldfile_open_file_search (name, entry, "", ".lib");
1790 gld_${EMULATION_NAME}_get_script (int *isfile)
1792 # Scripts compiled in.
1793 # sed commands to quote an ld script as a C string.
1794 sc="-f stringify.sed"
1796 cat >>e${EMULATION_NAME}.c <<EOF
1800 if (link_info.relocatable && config.build_constructors)
1803 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1804 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
1805 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1806 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1807 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1808 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1809 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1810 echo ' ; else return' >> e${EMULATION_NAME}.c
1811 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1812 echo '; }' >> e${EMULATION_NAME}.c
1814 cat >>e${EMULATION_NAME}.c <<EOF
1817 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1819 gld_${EMULATION_NAME}_before_parse,
1822 gld_${EMULATION_NAME}_after_parse,
1823 gld_${EMULATION_NAME}_after_open,
1824 after_allocation_default,
1825 set_output_arch_default,
1826 ldemul_default_target,
1827 gld_${EMULATION_NAME}_before_allocation,
1828 gld_${EMULATION_NAME}_get_script,
1829 "${EMULATION_NAME}",
1831 gld_${EMULATION_NAME}_finish,
1832 NULL, /* Create output section statements. */
1833 gld_${EMULATION_NAME}_open_dynamic_archive,
1834 gld_${EMULATION_NAME}_place_orphan,
1835 gld_${EMULATION_NAME}_set_symbols,
1836 NULL, /* parse_args */
1837 gld${EMULATION_NAME}_add_options,
1838 gld${EMULATION_NAME}_handle_option,
1839 gld_${EMULATION_NAME}_unrecognized_file,
1840 gld_${EMULATION_NAME}_list_options,
1841 gld_${EMULATION_NAME}_recognized_file,
1842 gld_${EMULATION_NAME}_find_potential_libraries,
1843 NULL /* new_vers_pattern. */