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