gdb/gdbserver/
[deliverable/binutils-gdb.git] / ld / emultempl / pe.em
CommitLineData
252b5132
RH
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3fc90ddb 3if [ -z "$MACHINE" ]; then
86af25fe
L
4 OUTPUT_ARCH=${ARCH}
5else
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
7fi
361d94e7
ILT
8rm -f e${EMULATION_NAME}.c
9(echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
92b93329 10fragment <<EOF
f96b4a7b 11/* Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
bde18da4 12 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
252b5132 13
f96b4a7b
NC
14 This file is part of the GNU Binutils.
15
2fa9fc65
NC
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
f96b4a7b 18 the Free Software Foundation; either version 3 of the License, or
2fa9fc65 19 (at your option) any later version.
252b5132 20
2fa9fc65
NC
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.
252b5132 25
2fa9fc65
NC
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
f96b4a7b
NC
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
29 MA 02110-1301, USA. */
30
252b5132
RH
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
3fc90ddb
AJ
37 the correct entry point by default. */
38
db09f25b
AM
39#define TARGET_IS_${EMULATION_NAME}
40
41/* Do this before including bfd.h, so we prototype the right functions. */
db09f25b 42
7148cc28
NC
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
92b93329 53
252b5132 54#include "sysdep.h"
3db64b00 55#include "bfd.h"
252b5132
RH
56#include "bfdlink.h"
57#include "getopt.h"
58#include "libiberty.h"
59#include "ld.h"
60#include "ldmain.h"
252b5132
RH
61#include "ldexp.h"
62#include "ldlang.h"
b71e2778 63#include "ldfile.h"
252b5132 64#include "ldemul.h"
df2a7313 65#include <ldgram.h>
252b5132
RH
66#include "ldlex.h"
67#include "ldmisc.h"
68#include "ldctor.h"
252b5132 69#include "coff/internal.h"
71add731 70
26d2d8a2
BF
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
71add731
ILT
76/* FIXME: This is a BFD internal header file, and we should not be
77 using it here. */
252b5132 78#include "../bfd/libcoff.h"
71add731 79
252b5132 80#include "deffile.h"
1069dd8d 81#include "pe-dll.h"
29e1a6e4 82#include "safe-ctype.h"
14fe918e 83
2be9b2c7
ILT
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
7148cc28
NC
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)
344a211f
NC
99#define DLL_SUPPORT
100#endif
c6c37250 101
e468a046
KT
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
344a211f 108#if defined(TARGET_IS_i386pe) || ! defined(DLL_SUPPORT)
c6c37250 109#define PE_DEF_SUBSYSTEM 3
344a211f
NC
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
7148cc28
NC
115
116#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
344a211f
NC
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
c6c37250 125
252b5132
RH
126static struct internal_extra_pe_aouthdr pe;
127static int dll;
decfa41e 128static int pe_subsystem = ${SUBSYSTEM};
26d2d8a2 129static flagword real_flags = 0;
252b5132 130static int support_old_code = 0;
6f798e5c 131static char * thumb_entry_symbol = NULL;
252b5132 132static lang_assignment_statement_type *image_base_statement = 0;
2f563b51 133static unsigned short pe_dll_characteristics = 0;
252b5132 134
f0c87f88 135#ifdef DLL_SUPPORT
4cd89863 136static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable. */
690a460e
NC
137static char *pe_out_def_filename = NULL;
138static char *pe_implib_filename = NULL;
5f577f7d 139static int pe_enable_auto_image_base = 0;
627427de 140static char *pe_dll_search_prefix = NULL;
f0c87f88 141#endif
252b5132
RH
142
143extern const char *output_filename;
144
145static void
0c7a8e5a 146gld_${EMULATION_NAME}_before_parse (void)
252b5132 147{
5e2f1575 148 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
db8d4f23 149 output_filename = "${EXECUTABLE_NAME:-a.exe}";
c6c37250 150#ifdef DLL_SUPPORT
b34976b6 151 config.dynamic_link = TRUE;
252b5132 152 config.has_shared = 1;
5b9c6027
DK
153EOF
154
155# Cygwin no longer wants these noisy warnings. Other PE
156# targets might like to consider adding themselves here.
157case ${target} in
158 *-*-cygwin*)
159 default_auto_import=1
160 ;;
161 *)
162 default_auto_import=-1
163 ;;
164esac
165
166fragment <<EOF
167 link_info.pei386_auto_import = ${default_auto_import};
e468a046
KT
168 /* Use by default version. */
169 link_info.pei386_runtime_pseudo_reloc = DEFAULT_PSEUDO_RELOC_VERSION;
252b5132
RH
170#endif
171}
172\f
173/* PE format extra command line options. */
174
4cd89863 175/* Used for setting flags in the PE header. */
252b5132
RH
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)
0c7a8e5a
AM
187#define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
188#define OPTION_SUBSYSTEM (OPTION_STACK + 1)
252b5132
RH
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)
2927aaca
NC
194#define OPTION_EXCLUDE_ALL_SYMBOLS (OPTION_EXCLUDE_SYMBOLS + 1)
195#define OPTION_KILL_ATS (OPTION_EXCLUDE_ALL_SYMBOLS + 1)
252b5132
RH
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)
6f798e5c 200#define OPTION_THUMB_ENTRY (OPTION_IMPLIB_FILENAME + 1)
870df5dc
NC
201#define OPTION_WARN_DUPLICATE_EXPORTS (OPTION_THUMB_ENTRY + 1)
202#define OPTION_IMP_COMPAT (OPTION_WARN_DUPLICATE_EXPORTS + 1)
5f577f7d
DD
203#define OPTION_ENABLE_AUTO_IMAGE_BASE (OPTION_IMP_COMPAT + 1)
204#define OPTION_DISABLE_AUTO_IMAGE_BASE (OPTION_ENABLE_AUTO_IMAGE_BASE + 1)
1122a5fc
NC
205#define OPTION_DLL_SEARCH_PREFIX (OPTION_DISABLE_AUTO_IMAGE_BASE + 1)
206#define OPTION_NO_DEFAULT_EXCLUDES (OPTION_DLL_SEARCH_PREFIX + 1)
decc3638
CW
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)
70b0be79 210#define OPTION_EXCLUDE_LIBS (OPTION_ENABLE_EXTRA_PE_DEBUG + 1)
2fa9fc65
NC
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)
26d2d8a2
BF
215#define OPTION_LARGE_ADDRESS_AWARE \
216 (OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC + 1)
6cb442d3
KT
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)
e1c37eb5
DK
221#define OPTION_EXCLUDE_MODULES_FOR_IMPLIB \
222 (OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2 + 1)
ce11ba6c 223#define OPTION_USE_NUL_PREFIXED_IMPORT_TABLES \
88183869 224 (OPTION_EXCLUDE_MODULES_FOR_IMPLIB + 1)
522f09cd 225#define OPTION_NO_LEADING_UNDERSCORE \
88183869 226 (OPTION_USE_NUL_PREFIXED_IMPORT_TABLES + 1)
522f09cd
KT
227#define OPTION_LEADING_UNDERSCORE \
228 (OPTION_NO_LEADING_UNDERSCORE + 1)
229#define OPTION_ENABLE_LONG_SECTION_NAMES \
230 (OPTION_LEADING_UNDERSCORE + 1)
88183869
DK
231#define OPTION_DISABLE_LONG_SECTION_NAMES \
232 (OPTION_ENABLE_LONG_SECTION_NAMES + 1)
2f563b51
DK
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)
252b5132 242
3bcf5557 243static void
0c7a8e5a 244gld${EMULATION_NAME}_add_options
d127ecce
AM
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)
3bcf5557
AM
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},
ce11ba6c
KT
270 {"use-nul-prefixed-import-tables", no_argument, NULL,
271 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
522f09cd
KT
272 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
273 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
c6c37250 274#ifdef DLL_SUPPORT
d127ecce
AM
275 /* getopt allows abbreviations, so we do this to stop it
276 from treating -o as an abbreviation for this option. */
3bcf5557
AM
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},
2927aaca 281 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
3bcf5557 282 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
e1c37eb5 283 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
3bcf5557
AM
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},
6cb442d3
KT
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},
252b5132 305#endif
26d2d8a2 306 {"large-address-aware", no_argument, NULL, OPTION_LARGE_ADDRESS_AWARE},
88183869
DK
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},
2f563b51
DK
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},
3bcf5557
AM
317 {NULL, no_argument, NULL, 0}
318 };
252b5132 319
d127ecce
AM
320 *longopts
321 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
3bcf5557
AM
322 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
323}
252b5132
RH
324
325/* PE/WIN32; added routines to get the subsystem type, heap and/or stack
4cd89863 326 parameters which may be input from the command line. */
252b5132
RH
327
328typedef struct
329{
330 void *ptr;
331 int size;
332 int value;
333 char *symbol;
334 int inited;
522f09cd
KT
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;
252b5132
RH
339} definfo;
340
522f09cd
KT
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}
252b5132
RH
357
358static definfo init[] =
359{
360 /* imagebase must be first */
361#define IMAGEBASEOFF 0
522f09cd 362 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, FALSE),
252b5132 363#define DLLOFF 1
522f09cd 364 {&dll, sizeof(dll), 0, "__dll__", 0, FALSE},
dffc6a64 365#define MSIMAGEBASEOFF 2
522f09cd
KT
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),
7148cc28 373#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
522f09cd 374 D(MajorSubsystemVersion,"__major_subsystem_version__", 3, FALSE),
344a211f 375#else
522f09cd 376 D(MajorSubsystemVersion,"__major_subsystem_version__", 4, FALSE),
344a211f 377#endif
522f09cd
KT
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}
252b5132
RH
387};
388
4cd89863 389
252b5132 390static void
0c7a8e5a 391gld_${EMULATION_NAME}_list_options (FILE *file)
252b5132
RH
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"));
522f09cd 408 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
6f798e5c 409 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
c6c37250 410#ifdef DLL_SUPPORT
252b5132
RH
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"));
2927aaca 415 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
b34976b6 416 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
e1c37eb5
DK
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"));
252b5132
RH
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"));
0752a404 424 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
e3c8793a
NC
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"));
5f577f7d 429 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
489d0400
NC
430 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
431 an importlib, use <string><basename>.dll\n\
e3c8793a 432 in preference to lib<basename>.dll \n"));
489d0400 433 fprintf (file, _(" --enable-auto-import Do sophistcated linking of _sym to\n\
e3c8793a 434 __imp_sym for DATA references\n"));
decc3638 435 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
2fa9fc65 436 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
489d0400
NC
437 adding pseudo-relocations resolved at\n\
438 runtime.\n"));
2fa9fc65
NC
439 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
440 auto-imported DATA.\n"));
e3c8793a
NC
441 fprintf (file, _(" --enable-extra-pe-debug Enable verbose debug output when building\n\
442 or linking to DLLs (esp. auto-import)\n"));
252b5132 443#endif
26d2d8a2
BF
444 fprintf (file, _(" --large-address-aware Executable supports virtual addresses\n\
445 greater than 2 gigabytes\n"));
88183869
DK
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"));
2f563b51
DK
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"));
522f09cd 459 fprintf (file, _(" --tsaware Image is Terminal Server aware\n"));
252b5132
RH
460}
461
4cd89863 462
252b5132 463static void
0c7a8e5a 464set_pe_name (char *name, long val)
252b5132
RH
465{
466 int i;
4cd89863
NC
467
468 /* Find the name and set it. */
252b5132
RH
469 for (i = 0; init[i].ptr; i++)
470 {
522f09cd 471 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
252b5132
RH
472 {
473 init[i].value = val;
474 init[i].inited = 1;
dffc6a64
KT
475 if (strcmp (name,"__image_base__") == 0)
476 set_pe_name (U ("__ImageBase"), val);
252b5132
RH
477 return;
478 }
479 }
939ba9d0 480 abort ();
252b5132
RH
481}
482
decfa41e
KT
483static void
484set_entry_point (void)
485{
486 const char *entry;
487 const char *initial_symbol_char;
522f09cd 488 int i, u = -1;
decfa41e
KT
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
522f09cd
KT
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
decfa41e
KT
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}
252b5132
RH
560
561static void
0c7a8e5a 562set_pe_subsystem (void)
252b5132
RH
563{
564 const char *sver;
33f362e1 565 char *end;
252b5132
RH
566 int len;
567 int i;
33f362e1 568 unsigned long temp_subsystem;
3fc90ddb 569 static const struct
252b5132
RH
570 {
571 const char *name;
572 const int value;
252b5132
RH
573 }
574 v[] =
575 {
decfa41e
KT
576 { "native", 1},
577 { "windows", 2},
578 { "console", 3},
579 { "posix", 7},
580 { "wince", 9},
581 { "xbox", 14},
582 { NULL, 0 }
252b5132
RH
583 };
584
33f362e1 585 /* Check for the presence of a version number. */
252b5132
RH
586 sver = strchr (optarg, ':');
587 if (sver == NULL)
588 len = strlen (optarg);
589 else
590 {
252b5132
RH
591 len = sver - optarg;
592 set_pe_name ("__major_subsystem_version__",
decfa41e 593 strtoul (sver + 1, &end, 0));
252b5132
RH
594 if (*end == '.')
595 set_pe_name ("__minor_subsystem_version__",
decfa41e 596 strtoul (end + 1, &end, 0));
252b5132
RH
597 if (*end != '\0')
598 einfo (_("%P: warning: bad version number in -subsystem option\n"));
599 }
600
33f362e1
NC
601 /* Check for numeric subsystem. */
602 temp_subsystem = strtoul (optarg, & end, 0);
603 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
252b5132 604 {
33f362e1
NC
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
33f362e1 610 /* Use this subsystem. */
decfa41e 611 pe_subsystem = (int) temp_subsystem;
33f362e1
NC
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;
252b5132 620
33f362e1
NC
621 if (v[i].name == NULL)
622 {
623 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
252b5132
RH
624 return;
625 }
33f362e1 626
decfa41e 627 pe_subsystem = v[i].value;
252b5132 628 }
3fc90ddb 629
decfa41e 630 set_pe_name ("__subsystem__", pe_subsystem);
33f362e1
NC
631
632 return;
252b5132
RH
633}
634
635
252b5132 636static void
0c7a8e5a 637set_pe_value (char *name)
252b5132
RH
638{
639 char *end;
3fc90ddb 640
252b5132 641 set_pe_name (name, strtoul (optarg, &end, 0));
3fc90ddb 642
252b5132
RH
643 if (end == optarg)
644 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
645
646 optarg = end;
647}
648
4cd89863 649
252b5132 650static void
0c7a8e5a 651set_pe_stack_heap (char *resname, char *comname)
252b5132
RH
652{
653 set_pe_value (resname);
3fc90ddb 654
252b5132
RH
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
3bcf5557 665static bfd_boolean
0c7a8e5a 666gld${EMULATION_NAME}_handle_option (int optc)
252b5132 667{
252b5132
RH
668 switch (optc)
669 {
670 default:
3bcf5557 671 return FALSE;
252b5132
RH
672
673 case OPTION_BASE_FILE:
0c7a8e5a 674 link_info.base_file = fopen (optarg, FOPEN_WB);
252b5132
RH
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
4cd89863 684 /* PE options. */
3fc90ddb 685 case OPTION_HEAP:
252b5132
RH
686 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
687 break;
3fc90ddb 688 case OPTION_STACK:
252b5132
RH
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;
6f798e5c
NC
727 case OPTION_THUMB_ENTRY:
728 thumb_entry_symbol = optarg;
729 break;
ce11ba6c
KT
730 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
731 pe_use_nul_prefixed_import_tables = TRUE;
732 break;
522f09cd
KT
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;
c6c37250 739#ifdef DLL_SUPPORT
252b5132
RH
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:
e1c37eb5 747 pe_dll_add_excludes (optarg, EXCLUDESYMS);
70b0be79 748 break;
2927aaca
NC
749 case OPTION_EXCLUDE_ALL_SYMBOLS:
750 pe_dll_exclude_all_symbols = 1;
751 break;
70b0be79 752 case OPTION_EXCLUDE_LIBS:
e1c37eb5
DK
753 pe_dll_add_excludes (optarg, EXCLUDELIBS);
754 break;
755 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
756 pe_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
252b5132
RH
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;
870df5dc
NC
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;
5f577f7d
DD
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;
627427de 785 case OPTION_DLL_SEARCH_PREFIX:
939ba9d0 786 pe_dll_search_prefix = xstrdup (optarg);
627427de 787 break;
1122a5fc
NC
788 case OPTION_NO_DEFAULT_EXCLUDES:
789 pe_dll_do_default_excludes = 0;
790 break;
decc3638 791 case OPTION_DLL_ENABLE_AUTO_IMPORT:
e0076ab3 792 link_info.pei386_auto_import = 1;
decc3638
CW
793 break;
794 case OPTION_DLL_DISABLE_AUTO_IMPORT:
e0076ab3 795 link_info.pei386_auto_import = 0;
decc3638 796 break;
2fa9fc65 797 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
e468a046
KT
798 link_info.pei386_runtime_pseudo_reloc =
799 DEFAULT_PSEUDO_RELOC_VERSION;
2fa9fc65 800 break;
6cb442d3
KT
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;
2fa9fc65
NC
807 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
808 link_info.pei386_runtime_pseudo_reloc = 0;
809 break;
decc3638
CW
810 case OPTION_ENABLE_EXTRA_PE_DEBUG:
811 pe_dll_extra_pe_debug = 1;
812 break;
c6c37250 813#endif
26d2d8a2
BF
814 case OPTION_LARGE_ADDRESS_AWARE:
815 real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
816 break;
88183869
DK
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;
2f563b51
DK
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;
252b5132 848 }
2f563b51
DK
849
850 /* Set DLLCharacteristics bits */
851 set_pe_name ("__dll_characteristics__", pe_dll_characteristics);
852
3bcf5557 853 return TRUE;
252b5132
RH
854}
855\f
5f577f7d 856
2ef53d66 857#ifdef DLL_SUPPORT
3fc90ddb 858static unsigned long
5f577f7d
DD
859strhash (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
4cd89863
NC
881/* Use the output file to create a image base for relocatable DLLs. */
882
5f577f7d
DD
883static unsigned long
884compute_dll_image_base (const char *ofile)
885{
886 unsigned long hash = strhash (ofile);
2c7fd14a 887 return 0x61300000 + ((hash << 16) & 0x0FFC0000);
5f577f7d 888}
2ef53d66 889#endif
5f577f7d 890
252b5132
RH
891/* Assign values to the special symbols before the linker script is
892 read. */
893
894static void
0c7a8e5a 895gld_${EMULATION_NAME}_set_symbols (void)
252b5132
RH
896{
897 /* Run through and invent symbols for all the
4cd89863 898 names and insert the defaults. */
252b5132 899 int j;
252b5132
RH
900
901 if (!init[IMAGEBASEOFF].inited)
902 {
1049f94e 903 if (link_info.relocatable)
252b5132 904 init[IMAGEBASEOFF].value = 0;
db8acf26 905 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
d127ecce 906 {
2ef53d66 907#ifdef DLL_SUPPORT
d127ecce
AM
908 init[IMAGEBASEOFF].value = (pe_enable_auto_image_base
909 ? compute_dll_image_base (output_filename)
910 : NT_DLL_IMAGE_BASE);
2ef53d66 911#else
d127ecce 912 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
2ef53d66 913#endif
d127ecce 914 }
252b5132
RH
915 else
916 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
dffc6a64 917 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
252b5132
RH
918 }
919
1049f94e
AM
920 /* Don't do any symbol assignments if this is a relocatable link. */
921 if (link_info.relocatable)
252b5132
RH
922 return;
923
4cd89863 924 /* Glue the assignments into the abs section. */
bde18da4 925 push_stat_ptr (&abs_output_section->children);
252b5132
RH
926
927 for (j = 0; init[j].ptr; j++)
928 {
929 long val = init[j].value;
930 lang_assignment_statement_type *rv;
522f09cd
KT
931
932 rv = lang_add_assignment (exp_assop ('=', GET_INIT_SYMBOL_NAME (j),
2c382fb6 933 exp_intop (val)));
939ba9d0
NC
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;
252b5132 940 /* This might be a long long or other special type. */
939ba9d0
NC
941 else if (init[j].size == sizeof (bfd_vma))
942 *(bfd_vma *) init[j].ptr = val;
943 else abort ();
252b5132
RH
944 if (j == IMAGEBASEOFF)
945 image_base_statement = rv;
946 }
4cd89863 947 /* Restore the pointer. */
bde18da4 948 pop_stat_ptr ();
3fc90ddb 949
d127ecce 950 if (pe.FileAlignment > pe.SectionAlignment)
252b5132
RH
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
959static void
0c7a8e5a 960gld_${EMULATION_NAME}_after_parse (void)
252b5132 961{
8b747e1a
DK
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"));
97b11f40 967
decfa41e
KT
968 set_entry_point ();
969
97b11f40 970 after_parse_default ();
252b5132
RH
971}
972
aa3d9aba
NC
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. */
978char * pe_data_import_dll;
72358f65 979
2ef53d66 980#ifdef DLL_SUPPORT
252b5132
RH
981static struct bfd_link_hash_entry *pe_undef_found_sym;
982
b34976b6 983static bfd_boolean
0c7a8e5a 984pe_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
252b5132 985{
decc3638 986 int sl;
0c7a8e5a 987 char *string = inf;
8a9eab9b 988 const char *hs = h->root.string;
4cd89863 989
0c7a8e5a 990 sl = strlen (string);
252b5132 991 if (h->type == bfd_link_hash_defined
8a9eab9b
KT
992 && ((*hs == '@' && *string == '_'
993 && strncmp (hs + 1, string + 1, sl - 1) == 0)
994 || strncmp (hs, string, sl) == 0)
252b5132 995 && h->root.string[sl] == '@')
decc3638
CW
996 {
997 pe_undef_found_sym = h;
b34976b6 998 return FALSE;
decc3638 999 }
b34976b6 1000 return TRUE;
252b5132
RH
1001}
1002
1003static void
0c7a8e5a 1004pe_fixup_stdcalls (void)
252b5132
RH
1005{
1006 static int gave_warning_message = 0;
1007 struct bfd_link_hash_entry *undef, *sym;
c9e38879 1008
3fc90ddb 1009 if (pe_dll_extra_pe_debug)
c9e38879 1010 printf ("%s\n", __FUNCTION__);
3fc90ddb 1011
f6e332e6 1012 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
252b5132 1013 if (undef->type == bfd_link_hash_undefined)
252b5132 1014 {
c9e38879
NC
1015 char* at = strchr (undef->root.string, '@');
1016 int lead_at = (*undef->root.string == '@');
8a9eab9b
KT
1017 if (lead_at)
1018 at = strchr (undef->root.string + 1, '@');
b34976b6 1019
8a9eab9b 1020 if (at || lead_at)
c9e38879
NC
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. */
8a9eab9b
KT
1024 char *cname = xstrdup (undef->root.string);
1025
1026 if (lead_at)
1027 *cname = '_';
c9e38879 1028 at = strchr (cname, '@');
8a9eab9b
KT
1029 if (at)
1030 *at = 0;
c9e38879
NC
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;
4cd89863 1038
c9e38879
NC
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;
939ba9d0
NC
1046 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1047 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
c9e38879
NC
1048 }
1049 }
1050 }
1051 }
1052 else
1053 {
1054 /* The symbol is a cdecl symbol, so we look for stdcall
4cd89863 1055 symbols - which means scanning the whole symbol table. */
c9e38879
NC
1056 pe_undef_found_sym = 0;
1057 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
0c7a8e5a 1058 (char *) undef->root.string);
c9e38879
NC
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;
4cd89863 1065
c9e38879
NC
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;
939ba9d0
NC
1073 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1074 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
c9e38879
NC
1075 }
1076 }
1077 }
1078 }
252b5132 1079 }
252b5132 1080}
decc3638
CW
1081
1082static int
0c7a8e5a 1083make_import_fixup (arelent *rel, asection *s)
decc3638 1084{
fc0a2244 1085 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
c99b8138 1086 char addend[4];
decc3638 1087
3fc90ddb 1088 if (pe_dll_extra_pe_debug)
c9e38879 1089 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
0af1713e 1090 (unsigned long) rel->address, (long) rel->addend);
0d888aac 1091
c99b8138 1092 if (! bfd_get_section_contents (s->owner, s, addend, rel->address, sizeof (addend)))
c9e38879
NC
1093 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
1094 s->owner, s, rel->address);
0d888aac 1095
c99b8138 1096 pe_create_import_fixup (rel, s, bfd_get_32 (s->owner, addend));
0d888aac 1097
decc3638
CW
1098 return 1;
1099}
1100
decc3638 1101static void
0c7a8e5a 1102pe_find_data_imports (void)
decc3638
CW
1103{
1104 struct bfd_link_hash_entry *undef, *sym;
c9e38879 1105
4cd89863
NC
1106 if (link_info.pei386_auto_import == 0)
1107 return;
1108
f6e332e6 1109 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
decc3638
CW
1110 {
1111 if (undef->type == bfd_link_hash_undefined)
1af699f4
AM
1112 {
1113 /* C++ symbols are *long*. */
1114 char buf[4096];
c9e38879
NC
1115
1116 if (pe_dll_extra_pe_debug)
1117 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
1118
1af699f4 1119 sprintf (buf, "__imp_%s", undef->root.string);
decc3638 1120
1af699f4 1121 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
c9e38879 1122
1af699f4
AM
1123 if (sym && sym->type == bfd_link_hash_defined)
1124 {
4cd89863
NC
1125 bfd *b = sym->u.def.section->owner;
1126 asymbol **symbols;
5c1d2f5f 1127 int nsyms, i;
4cd89863
NC
1128
1129 if (link_info.pei386_auto_import == -1)
8524166d
NC
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\
44e36d8a 1141This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.\n"));
8524166d
NC
1142 }
1143 }
4cd89863 1144
5c1d2f5f
AM
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);
4cd89863
NC
1153
1154 for (i = 0; i < nsyms; i++)
1155 {
522f09cd
KT
1156 if (! CONST_STRNEQ (symbols[i]->name,
1157 U ("_head_")))
4cd89863
NC
1158 continue;
1159
1160 if (pe_dll_extra_pe_debug)
1161 printf ("->%s\n", symbols[i]->name);
1162
522f09cd
KT
1163 pe_data_import_dll = (char *) (symbols[i]->name
1164 + U_SIZE ("_head_") - 1);
4cd89863
NC
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;
1af699f4
AM
1179 }
1180 }
decc3638
CW
1181 }
1182}
252b5132 1183
b34976b6 1184static bfd_boolean
0c7a8e5a 1185pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
decc3638 1186{
3fc90ddb 1187 if (pe_dll_extra_pe_debug)
0c7a8e5a 1188 printf ("+%s\n", h->string);
c9e38879 1189
b34976b6 1190 return TRUE;
decc3638 1191}
4b83d615 1192#endif /* DLL_SUPPORT */
decc3638 1193
3efd345c
DK
1194static void
1195debug_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}
decc3638 1201
252b5132 1202static void
0c7a8e5a 1203gld_${EMULATION_NAME}_after_open (void)
252b5132 1204{
4b83d615 1205#ifdef DLL_SUPPORT
3fc90ddb 1206 if (pe_dll_extra_pe_debug)
decc3638
CW
1207 {
1208 bfd *a;
1209 struct bfd_link_hash_entry *sym;
c9e38879 1210
decc3638
CW
1211 printf ("%s()\n", __FUNCTION__);
1212
f6e332e6 1213 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1af699f4 1214 printf ("-%s\n", sym->root.string);
0c7a8e5a 1215 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
decc3638
CW
1216
1217 for (a = link_info.input_bfds; a; a = a->link_next)
c9e38879 1218 printf ("*%s\n",a->filename);
decc3638 1219 }
4b83d615 1220#endif
3fc90ddb 1221
252b5132
RH
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. */
3fc90ddb 1225
f13a99db
AM
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);
252b5132 1230
f13a99db
AM
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;
3efd345c
DK
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
88183869 1257 pe_output_file_set_long_section_names (link_info.output_bfd);
252b5132 1258
c6c37250 1259#ifdef DLL_SUPPORT
252b5132
RH
1260 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
1261 pe_fixup_stdcalls ();
1262
f13a99db 1263 pe_process_import_defs (link_info.output_bfd, &link_info);
939ba9d0 1264
3fc90ddb 1265 pe_find_data_imports ();
decc3638 1266
8a9eab9b
KT
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
7148cc28
NC
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)
1049f94e 1276 if (!link_info.relocatable)
f13a99db 1277 pe_dll_build_sections (link_info.output_bfd, &link_info);
7148cc28
NC
1278#else
1279 if (link_info.shared)
f13a99db 1280 pe_dll_build_sections (link_info.output_bfd, &link_info);
1491f1f0
DK
1281 else
1282 pe_exe_build_sections (link_info.output_bfd, &link_info);
344a211f 1283#endif
7148cc28 1284#endif /* DLL_SUPPORT */
252b5132 1285
7148cc28 1286#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) || defined(TARGET_IS_arm_wince_pe)
f13a99db 1287 if (strstr (bfd_get_target (link_info.output_bfd), "arm") == NULL)
f11523b0
NC
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 }
252b5132
RH
1296 {
1297 /* Find a BFD that can hold the interworking stubs. */
1298 LANG_FOR_EACH_INPUT_STATEMENT (is)
1299 {
7148cc28 1300 if (bfd_arm_get_bfd_for_interworking (is->the_bfd, & link_info))
252b5132
RH
1301 break;
1302 }
1303 }
1304#endif
c6c37250 1305
486e80e2 1306 {
486e80e2
DD
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
4cd89863 1315 things nicely and produce a valid import table. */
486e80e2
DD
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;
3fc90ddb 1323
6e45556a 1324 /* See if this is an import library thunk. */
486e80e2
DD
1325 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1326 {
1327 if (strcmp (sec->name, ".idata\$2") == 0)
1328 idata2 = 1;
0112cd26 1329 if (CONST_STRNEQ (sec->name, ".idata\$"))
486e80e2
DD
1330 is_imp = 1;
1331 reloc_count += sec->reloc_count;
1332 }
3fc90ddb 1333
486e80e2
DD
1334 if (is_imp && !idata2 && reloc_count)
1335 {
6e45556a
NC
1336 /* It is, look for the reference to head and see if it's
1337 from our own library. */
486e80e2
DD
1338 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1339 {
1340 int i;
6e45556a 1341 long relsize;
102c86f8 1342 asymbol **symbols;
102c86f8
NC
1343 arelent **relocs;
1344 int nrelocs;
3fc90ddb 1345
102c86f8 1346 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
6e45556a
NC
1347 if (relsize < 1)
1348 break;
3fc90ddb 1349
5c1d2f5f 1350 if (!bfd_generic_link_read_symbols (is->the_bfd))
6e45556a 1351 {
5c1d2f5f
AM
1352 einfo (_("%B%F: could not read symbols: %E\n"),
1353 is->the_bfd);
6e45556a
NC
1354 return;
1355 }
5c1d2f5f 1356 symbols = bfd_get_outsymbols (is->the_bfd);
3fc90ddb 1357
d127ecce 1358 relocs = xmalloc ((size_t) relsize);
102c86f8 1359 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
f6e332e6 1360 relocs, symbols);
6e45556a
NC
1361 if (nrelocs < 0)
1362 {
1363 free (relocs);
44e36d8a 1364 einfo ("%X%P: unable to process relocs: %E\n");
6e45556a
NC
1365 return;
1366 }
3fc90ddb 1367
6e45556a 1368 for (i = 0; i < nrelocs; i++)
486e80e2 1369 {
fc0a2244 1370 struct bfd_symbol *s;
6e45556a 1371 struct bfd_link_hash_entry * blhe;
2a08daf3 1372 char *other_bfd_filename;
6e45556a 1373 char *n;
3fc90ddb 1374
486e80e2 1375 s = (relocs[i]->sym_ptr_ptr)[0];
3fc90ddb 1376
6e45556a
NC
1377 if (s->flags & BSF_LOCAL)
1378 continue;
3fc90ddb 1379
6e45556a
NC
1380 /* Thunk section with reloc to another bfd. */
1381 blhe = bfd_link_hash_lookup (link_info.hash,
1382 s->name,
b34976b6 1383 FALSE, FALSE, TRUE);
3fc90ddb 1384
6e45556a
NC
1385 if (blhe == NULL
1386 || blhe->type != bfd_link_hash_defined)
1387 continue;
3fc90ddb 1388
2a08daf3
DS
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);
3fc90ddb 1393
2a08daf3
DS
1394 if (strcmp (bfd_get_filename (is->the_bfd->my_archive),
1395 other_bfd_filename) == 0)
6e45556a 1396 continue;
3fc90ddb 1397
c034e3e0 1398 /* Rename this implib to match the other one. */
2a08daf3
DS
1399 n = xmalloc (strlen (other_bfd_filename) + 1);
1400 strcpy (n, other_bfd_filename);
1401 is->the_bfd->my_archive->filename = n;
486e80e2
DD
1402 }
1403
1404 free (relocs);
6e45556a
NC
1405 /* Note - we do not free the symbols,
1406 they are now cached in the BFD. */
486e80e2
DD
1407 }
1408 }
1409 }
1410 }
1411 }
1412
c6c37250 1413 {
f0c87f88 1414 int is_ms_arch = 0;
1069dd8d 1415 bfd *cur_arch = 0;
c6c37250 1416 lang_input_statement_type *is2;
44dbf363 1417 lang_input_statement_type *is3;
1069dd8d 1418
c6c37250
DD
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
1069dd8d 1427 table for external references. */
c6c37250
DD
1428 LANG_FOR_EACH_INPUT_STATEMENT (is)
1429 {
1430 if (is->the_bfd->my_archive)
1431 {
44dbf363 1432 char *pnt;
c6c37250 1433 bfd *arch = is->the_bfd->my_archive;
44dbf363 1434
c6c37250
DD
1435 if (cur_arch != arch)
1436 {
1437 cur_arch = arch;
1438 is_ms_arch = 1;
44dbf363
NC
1439
1440 for (is3 = is;
1441 is3 && is3->the_bfd->my_archive == arch;
1442 is3 = (lang_input_statement_type *) is3->next)
1443 {
1af699f4 1444 /* A MS dynamic import library can also contain static
44dbf363
NC
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, '.');
07a68a80 1449 if (pnt != NULL && strcmp (pnt, ".dll") == 0)
ff71f4f7 1450 break;
44dbf363
NC
1451 }
1452
1453 if (is3 == NULL)
1454 is_ms_arch = 0;
1455 else
c6c37250 1456 {
44dbf363
NC
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 }
c6c37250
DD
1476 }
1477 }
1478
44dbf363
NC
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))
c6c37250 1485 {
1069dd8d 1486 int idata2 = 0, reloc_count=0;
c6c37250
DD
1487 asection *sec;
1488 char *new_name, seq;
1069dd8d 1489
c6c37250
DD
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
e4e24acb 1504 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
c6c37250
DD
1505 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1506 is->the_bfd->filename = new_name;
1507
690a460e 1508 new_name = xmalloc (strlen (is->filename) + 3);
c6c37250
DD
1509 sprintf (new_name, "%s.%c", is->filename, seq);
1510 is->filename = new_name;
1511 }
1512 }
1513 }
1514 }
54727719
NC
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
d34c1c51 1521 and several .idata\$* sections. The .text section contains the
54727719 1522 symbol definition for <sym> which is a jump stub of the form
d34c1c51 1523 jmp *__imp_<sym>. The .idata\$5 contains the symbol definition
54727719
NC
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
d34c1c51 1528 symbol in .idata\$5, in which case the jump stub code is not
54727719
NC
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 }
92b93329 1549
54727719
NC
1550 if (is_imp && stub_sec)
1551 {
54727719 1552 asymbol **symbols;
5c1d2f5f 1553 long nsyms, src_count;
54727719
NC
1554 struct bfd_link_hash_entry * blhe;
1555
5c1d2f5f
AM
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);
54727719 1564
5c1d2f5f 1565 for (src_count = 0; src_count < nsyms; src_count++)
54727719
NC
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 }
54727719
NC
1582 }
1583 }
1584 }
1585 }
252b5132
RH
1586}
1587\f
3fc90ddb 1588static void
0c7a8e5a 1589gld_${EMULATION_NAME}_before_allocation (void)
252b5132
RH
1590{
1591#ifdef TARGET_IS_ppcpe
c9e38879 1592 /* Here we rummage through the found bfds to collect toc information. */
252b5132
RH
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
c9e38879 1604 /* We have seen it all. Allocate it, and carry on. */
252b5132
RH
1605 ppc_allocate_toc_section (&link_info);
1606#endif /* TARGET_IS_ppcpe */
1607
7148cc28 1608#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) || defined(TARGET_IS_arm_wince_pe)
252b5132
RH
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
c9e38879 1614 option? krk@cygnus.com. */
252b5132
RH
1615 {
1616 LANG_FOR_EACH_INPUT_STATEMENT (is)
1617 {
7148cc28 1618 if (! bfd_arm_process_before_allocation
252b5132
RH
1619 (is->the_bfd, & link_info, support_old_code))
1620 {
1621 /* xgettext:c-format */
44e36d8a 1622 einfo (_("Errors encountered processing file %s for interworking\n"),
252b5132
RH
1623 is->filename);
1624 }
1625 }
1626 }
1627
c9e38879 1628 /* We have seen it all. Allocate it, and carry on. */
7148cc28
NC
1629 bfd_arm_allocate_interworking_sections (& link_info);
1630#endif /* TARGET_IS_armpe || TARGET_IS_arm_epoc_pe || TARGET_IS_arm_wince_pe */
8423293d 1631
1e035701 1632 before_allocation_default ();
252b5132
RH
1633}
1634\f
690a460e 1635#ifdef DLL_SUPPORT
252b5132 1636/* This is called when an input file isn't recognized as a BFD. We
c9e38879 1637 check here for .DEF files and pull them in automatically. */
690a460e 1638
252b5132 1639static int
0c7a8e5a 1640saw_option (char *option)
252b5132
RH
1641{
1642 int i;
c9e38879
NC
1643
1644 for (i = 0; init[i].ptr; i++)
522f09cd 1645 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
252b5132
RH
1646 return init[i].inited;
1647 return 0;
1648}
690a460e 1649#endif /* DLL_SUPPORT */
252b5132 1650
b34976b6 1651static bfd_boolean
0c7a8e5a 1652gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
252b5132 1653{
c6c37250 1654#ifdef DLL_SUPPORT
252b5132
RH
1655 const char *ext = entry->filename + strlen (entry->filename) - 4;
1656
1657 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
252b5132 1658 {
f2d3750d 1659 pe_def_file = def_file_parse (entry->filename, pe_def_file);
c9e38879
NC
1660
1661 if (pe_def_file)
252b5132 1662 {
c9e38879
NC
1663 int i, buflen=0, len;
1664 char *buf;
252b5132 1665
c9e38879 1666 for (i = 0; i < pe_def_file->num_exports; i++)
252b5132 1667 {
939ba9d0 1668 len = strlen (pe_def_file->exports[i].internal_name);
c9e38879
NC
1669 if (buflen < len + 2)
1670 buflen = len + 2;
252b5132 1671 }
252b5132 1672
d127ecce 1673 buf = xmalloc (buflen);
252b5132 1674
c9e38879
NC
1675 for (i = 0; i < pe_def_file->num_exports; i++)
1676 {
1677 struct bfd_link_hash_entry *h;
1678
522f09cd
KT
1679 sprintf (buf, "%s%s", U (""),
1680 pe_def_file->exports[i].internal_name);
c9e38879 1681
b34976b6 1682 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
c9e38879
NC
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 {
f13a99db
AM
1700 pe.ImageBase
1701 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1702 = init[IMAGEBASEOFF].value
1703 = pe_def_file->base_address;
c9e38879
NC
1704 init[IMAGEBASEOFF].inited = 1;
1705 if (image_base_statement)
f13a99db
AM
1706 image_base_statement->exp = exp_assop ('=', "__image_base__",
1707 exp_intop (pe.ImageBase));
c9e38879 1708 }
252b5132 1709
c9e38879
NC
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 }
b34976b6 1724 return TRUE;
c9e38879 1725 }
252b5132 1726 }
252b5132 1727#endif
b34976b6 1728 return FALSE;
252b5132
RH
1729}
1730
b34976b6 1731static bfd_boolean
0c7a8e5a 1732gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
252b5132 1733{
c6c37250 1734#ifdef DLL_SUPPORT
252b5132 1735#ifdef TARGET_IS_i386pe
c6c37250 1736 pe_dll_id_target ("pei-i386");
344a211f
NC
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");
7148cc28
NC
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");
c6c37250 1752#endif
ff2bdb9c
CF
1753 if (pe_bfd_is_dll (entry->the_bfd))
1754 return pe_implied_import_dll (entry->filename);
252b5132 1755#endif
b34976b6 1756 return FALSE;
252b5132
RH
1757}
1758
1759static void
0c7a8e5a 1760gld_${EMULATION_NAME}_finish (void)
252b5132 1761{
7148cc28 1762#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) || defined(TARGET_IS_arm_wince_pe)
6f798e5c
NC
1763 struct bfd_link_hash_entry * h;
1764
1765 if (thumb_entry_symbol != NULL)
1766 {
b34976b6
AM
1767 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol,
1768 FALSE, FALSE, TRUE);
3fc90ddb 1769
6f798e5c
NC
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;
3fc90ddb 1777
6f798e5c
NC
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
f13a99db 1781 + bfd_get_section_vma (link_info.output_bfd,
6f798e5c
NC
1782 h->u.def.section->output_section)
1783 + h->u.def.section->output_offset);
3fc90ddb 1784
6f798e5c 1785 val |= 1;
3fc90ddb 1786
6f798e5c
NC
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';
3fc90ddb 1791
6f798e5c 1792 sprintf_vma (buffer + 2, val);
3fc90ddb 1793
e3e942e9 1794 if (entry_symbol.name != NULL && entry_from_cmdline)
6f798e5c 1795 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
e3e942e9
AM
1796 thumb_entry_symbol, entry_symbol.name);
1797 entry_symbol.name = buffer;
6f798e5c
NC
1798 }
1799 else
c58dea77 1800 einfo (_("%P: warning: cannot find thumb start symbol %s\n"), thumb_entry_symbol);
6f798e5c 1801 }
7148cc28 1802#endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) || defined(TARGET_IS_arm_wince_pe) */
6f798e5c 1803
1e035701
AM
1804 finish_default ();
1805
c6c37250 1806#ifdef DLL_SUPPORT
2b817be1
NC
1807 if (link_info.shared
1808#if !defined(TARGET_IS_shpe) && !defined(TARGET_IS_mipspe)
d127ecce 1809 || (!link_info.relocatable && pe_def_file->num_exports != 0)
2b817be1
NC
1810#endif
1811 )
252b5132 1812 {
f13a99db 1813 pe_dll_fill_sections (link_info.output_bfd, &link_info);
252b5132 1814 if (pe_implib_filename)
e1c37eb5 1815 pe_dll_generate_implib (pe_def_file, pe_implib_filename, &link_info);
252b5132 1816 }
344a211f 1817#if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe)
690a460e 1818 /* ARM doesn't need relocs. */
344a211f
NC
1819 else
1820 {
f13a99db 1821 pe_exe_fill_sections (link_info.output_bfd, &link_info);
344a211f
NC
1822 }
1823#endif
3fc90ddb 1824
252b5132
RH
1825 if (pe_out_def_filename)
1826 pe_dll_generate_def_file (pe_out_def_filename);
690a460e 1827#endif /* DLL_SUPPORT */
decc3638 1828
c9e38879 1829 /* I don't know where .idata gets set as code, but it shouldn't be. */
decc3638 1830 {
f13a99db 1831 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
c9e38879 1832
decc3638
CW
1833 if (asec)
1834 {
1af699f4
AM
1835 asec->flags &= ~SEC_CODE;
1836 asec->flags |= SEC_DATA;
decc3638
CW
1837 }
1838 }
252b5132
RH
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
d34c1c51 1847 We handle grouped sections here as well. A section named .foo\$nn
252b5132
RH
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
c2edb4b8 1855static lang_output_section_statement_type *
8a99a385
AM
1856gld_${EMULATION_NAME}_place_orphan (asection *s,
1857 const char *secname,
1858 int constraint)
252b5132 1859{
bcacc0f5 1860 const char *orig_secname = secname;
f0c87f88 1861 char *dollar = NULL;
aea4bd9d 1862 lang_output_section_statement_type *os;
5ba47421 1863 lang_statement_list_type add_child;
b5f14a6d 1864 lang_output_section_statement_type *match_by_name = NULL;
011aa75f 1865 lang_statement_union_type **pl;
252b5132 1866
252b5132 1867 /* Look through the script to see where to place this section. */
afd7a018 1868 if (!link_info.relocatable
d34c1c51 1869 && (dollar = strchr (secname, '\$')) != NULL)
6eec49fc 1870 {
bcacc0f5 1871 size_t len = dollar - secname;
afd7a018 1872 char *newname = xmalloc (len + 1);
bcacc0f5 1873 memcpy (newname, secname, len);
afd7a018
AM
1874 newname[len] = '\0';
1875 secname = newname;
6eec49fc 1876 }
252b5132 1877
5ba47421
AM
1878 lang_list_init (&add_child);
1879
d127ecce
AM
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 }
b5f14a6d
DD
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;
d127ecce
AM
1908 }
1909
b5f14a6d
DD
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
d127ecce 1918 if (os == NULL)
252b5132 1919 {
afd7a018
AM
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;
6a345e87 1943 struct orphan_save *place;
afd7a018 1944 lang_output_section_statement_type *after;
252b5132 1945 etree_type *address;
afd7a018
AM
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 }
252b5132
RH
1959
1960 /* Try to put the new output section in a reasonable place based
1961 on the section name and section flags. */
aea4bd9d 1962
252b5132 1963 place = NULL;
5ba47421
AM
1964 if ((s->flags & SEC_ALLOC) == 0)
1965 ;
afd7a018
AM
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];
1af699f4 1972 else
afd7a018 1973 place = &hold[orphan_text];
1af699f4 1974
afd7a018 1975 after = NULL;
1af699f4
AM
1976 if (place != NULL)
1977 {
afd7a018
AM
1978 if (place->os == NULL)
1979 place->os = lang_output_section_find (place->name);
1980 after = place->os;
1981 if (after == NULL)
390fbbf1 1982 after = lang_output_section_find_by_flags (s, &place->os, NULL);
afd7a018
AM
1983 if (after == NULL)
1984 /* *ABS* is always the first output section statement. */
1985 after = (&lang_output_section_statement.head
1986 ->output_section_statement);
252b5132
RH
1987 }
1988
afd7a018
AM
1989 /* All sections in an executable must be aligned to a page boundary. */
1990 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
8a99a385
AM
1991 os = lang_insert_orphan (s, secname, constraint, after, place, address,
1992 &add_child);
252b5132
RH
1993 }
1994
011aa75f
AM
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;
252b5132 2001
011aa75f
AM
2002 if ((*pl)->header.type != lang_input_section_enum)
2003 continue;
252b5132 2004
011aa75f 2005 ls = &(*pl)->input_section;
252b5132 2006
011aa75f
AM
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 }
5ba47421 2012
011aa75f
AM
2013 if (add_child.head != NULL)
2014 {
2015 *add_child.tail = *pl;
2016 *pl = add_child.head;
2017 }
252b5132 2018
c2edb4b8 2019 return os;
252b5132
RH
2020}
2021
b34976b6 2022static bfd_boolean
0c7a8e5a 2023gld_${EMULATION_NAME}_open_dynamic_archive
d127ecce
AM
2024 (const char *arch ATTRIBUTE_UNUSED,
2025 search_dirs_type *search,
0c7a8e5a 2026 lang_input_statement_type *entry)
690a460e 2027{
45e948fe
NC
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 },
92b93329 2043 /* The 'native' spelling of an import lib name is "foo.lib". */
45e948fe
NC
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 };
b1d9458a 2058 static unsigned int format_max_len = 0;
690a460e 2059 const char * filename;
45e948fe
NC
2060 char * full_string;
2061 char * base_string;
2062 unsigned int i;
2063
690a460e
NC
2064
2065 if (! entry->is_archive)
b34976b6 2066 return FALSE;
690a460e
NC
2067
2068 filename = entry->filename;
2069
b1d9458a
NC
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
45e948fe
NC
2083 full_string = xmalloc (strlen (search->name)
2084 + strlen (filename)
b1d9458a 2085 + format_max_len
b9a69188 2086#ifdef DLL_SUPPORT
45e948fe
NC
2087 + (pe_dll_search_prefix
2088 ? strlen (pe_dll_search_prefix) : 0)
b9a69188 2089#endif
b1d9458a
NC
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);
690a460e 2094
45e948fe
NC
2095 sprintf (full_string, "%s/", search->name);
2096 base_string = full_string + strlen (full_string);
690a460e 2097
45e948fe 2098 for (i = 0; libname_fmt[i].format; i++)
690a460e 2099 {
92b93329 2100#ifdef DLL_SUPPORT
45e948fe 2101 if (libname_fmt[i].use_prefix)
1af699f4 2102 {
45e948fe
NC
2103 if (!pe_dll_search_prefix)
2104 continue;
2105 sprintf (base_string, libname_fmt[i].format, pe_dll_search_prefix, filename);
1af699f4 2106 }
45e948fe
NC
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;
690a460e 2119 }
602e90d3 2120
45e948fe 2121 entry->filename = full_string;
690a460e 2122
b34976b6 2123 return TRUE;
690a460e
NC
2124}
2125
344a211f 2126static int
0c7a8e5a
AM
2127gld_${EMULATION_NAME}_find_potential_libraries
2128 (char *name, lang_input_statement_type *entry)
344a211f
NC
2129{
2130 return ldfile_open_file_search (name, entry, "", ".lib");
2131}
252b5132
RH
2132\f
2133static char *
0c7a8e5a 2134gld_${EMULATION_NAME}_get_script (int *isfile)
252b5132
RH
2135EOF
2136# Scripts compiled in.
2137# sed commands to quote an ld script as a C string.
597e2591 2138sc="-f stringify.sed"
252b5132 2139
92b93329 2140fragment <<EOF
3fc90ddb 2141{
252b5132
RH
2142 *isfile = 0;
2143
1049f94e 2144 if (link_info.relocatable && config.build_constructors)
252b5132
RH
2145 return
2146EOF
1af699f4
AM
2147sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
2148echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
2149sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
2150echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
2151sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
2152echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
2153sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
e2a83dd0
NC
2154if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2155echo ' ; else if (link_info.pei386_auto_import == 1) return' >> e${EMULATION_NAME}.c
2156sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
2157fi
1af699f4
AM
2158echo ' ; else return' >> e${EMULATION_NAME}.c
2159sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
2160echo '; }' >> e${EMULATION_NAME}.c
252b5132 2161
92b93329 2162fragment <<EOF
252b5132
RH
2163
2164
3fc90ddb 2165struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
252b5132
RH
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}",
4cd89863
NC
2179 gld_${EMULATION_NAME}_finish,
2180 NULL, /* Create output section statements. */
690a460e 2181 gld_${EMULATION_NAME}_open_dynamic_archive,
252b5132
RH
2182 gld_${EMULATION_NAME}_place_orphan,
2183 gld_${EMULATION_NAME}_set_symbols,
3bcf5557
AM
2184 NULL, /* parse_args */
2185 gld${EMULATION_NAME}_add_options,
2186 gld${EMULATION_NAME}_handle_option,
252b5132
RH
2187 gld_${EMULATION_NAME}_unrecognized_file,
2188 gld_${EMULATION_NAME}_list_options,
344a211f 2189 gld_${EMULATION_NAME}_recognized_file,
fac1652d 2190 gld_${EMULATION_NAME}_find_potential_libraries,
4cd89863 2191 NULL /* new_vers_pattern. */
252b5132
RH
2192};
2193EOF
This page took 0.588369 seconds and 4 git commands to generate.