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