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