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