]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/emultempl/pe.em
Correct spelling of "relocatable".
[thirdparty/binutils-gdb.git] / ld / emultempl / pe.em
CommitLineData
252b5132
RH
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3fc90ddb 3if [ -z "$MACHINE" ]; then
86af25fe
L
4 OUTPUT_ARCH=${ARCH}
5else
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
7fi
361d94e7
ILT
8rm -f e${EMULATION_NAME}.c
9(echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
c6c37250 10cat >>e${EMULATION_NAME}.c <<EOF
252b5132 11/* This file is part of GLD, the Gnu Linker.
489d0400 12 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
a2b64bed 13 Free Software Foundation, Inc.
252b5132 14
2fa9fc65
NC
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
252b5132 19
2fa9fc65
NC
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
252b5132 24
2fa9fc65
NC
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
252b5132
RH
28
29/* For WINDOWS_NT */
30/* The original file generated returned different default scripts depending
31 on whether certain switches were set, but these switches pertain to the
32 Linux system and that particular version of coff. In the NT case, we
33 only determine if the subsystem is console or windows in order to select
3fc90ddb
AJ
34 the correct entry point by default. */
35
db09f25b
AM
36#define TARGET_IS_${EMULATION_NAME}
37
38/* Do this before including bfd.h, so we prototype the right functions. */
39#ifdef TARGET_IS_arm_epoc_pe
40#define bfd_arm_pe_allocate_interworking_sections \
41 bfd_arm_epoc_pe_allocate_interworking_sections
42#define bfd_arm_pe_get_bfd_for_interworking \
43 bfd_arm_epoc_pe_get_bfd_for_interworking
44#define bfd_arm_pe_process_before_allocation \
45 bfd_arm_epoc_pe_process_before_allocation
46#endif
47
252b5132
RH
48#include "bfd.h"
49#include "sysdep.h"
50#include "bfdlink.h"
51#include "getopt.h"
52#include "libiberty.h"
53#include "ld.h"
54#include "ldmain.h"
252b5132
RH
55#include "ldexp.h"
56#include "ldlang.h"
b71e2778 57#include "ldfile.h"
252b5132 58#include "ldemul.h"
df2a7313 59#include <ldgram.h>
252b5132
RH
60#include "ldlex.h"
61#include "ldmisc.h"
62#include "ldctor.h"
252b5132 63#include "coff/internal.h"
71add731
ILT
64
65/* FIXME: This is a BFD internal header file, and we should not be
66 using it here. */
252b5132 67#include "../bfd/libcoff.h"
71add731 68
252b5132 69#include "deffile.h"
1069dd8d 70#include "pe-dll.h"
29e1a6e4 71#include "safe-ctype.h"
14fe918e 72
2be9b2c7
ILT
73/* Permit the emulation parameters to override the default section
74 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
75 it seem that include/coff/internal.h should not define
76 PE_DEF_SECTION_ALIGNMENT. */
77#if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
78#undef PE_DEF_SECTION_ALIGNMENT
79#define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
80#endif
81
c6c37250
DD
82#if defined(TARGET_IS_i386pe)
83#define DLL_SUPPORT
84#endif
344a211f
NC
85#if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe) || defined(TARGET_IS_armpe)
86#define DLL_SUPPORT
87#endif
c6c37250 88
344a211f 89#if defined(TARGET_IS_i386pe) || ! defined(DLL_SUPPORT)
c6c37250 90#define PE_DEF_SUBSYSTEM 3
344a211f
NC
91#else
92#undef NT_EXE_IMAGE_BASE
93#undef PE_DEF_SECTION_ALIGNMENT
94#undef PE_DEF_FILE_ALIGNMENT
95#define NT_EXE_IMAGE_BASE 0x00010000
96#ifdef TARGET_IS_armpe
97#define PE_DEF_SECTION_ALIGNMENT 0x00001000
98#define PE_DEF_SUBSYSTEM 9
99#else
100#define PE_DEF_SECTION_ALIGNMENT 0x00000400
101#define PE_DEF_SUBSYSTEM 2
102#endif
103#define PE_DEF_FILE_ALIGNMENT 0x00000200
104#endif
c6c37250 105
252b5132
RH
106static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void));
107static void gld_${EMULATION_NAME}_after_open PARAMS ((void));
108static void gld_${EMULATION_NAME}_before_parse PARAMS ((void));
109static void gld_${EMULATION_NAME}_after_parse PARAMS ((void));
110static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void));
ae7fb08f
AM
111static asection *output_prev_sec_find
112 PARAMS ((lang_output_section_statement_type *));
b34976b6 113static bfd_boolean gld_${EMULATION_NAME}_place_orphan
252b5132 114 PARAMS ((lang_input_statement_type *, asection *));
252b5132 115static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
6f798e5c 116static void gld_${EMULATION_NAME}_finish PARAMS ((void));
b34976b6 117static bfd_boolean gld_${EMULATION_NAME}_open_dynamic_archive
690a460e 118 PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
3bcf5557
AM
119static void gld${EMULATION_NAME}_add_options
120 PARAMS ((int, char **, int, struct option **, int, struct option **));
121static bfd_boolean gld${EMULATION_NAME}_handle_option PARAMS ((int));
3fc90ddb
AJ
122static void gld_${EMULATION_NAME}_list_options PARAMS ((FILE *));
123static void set_pe_name PARAMS ((char *, long));
124static void set_pe_subsystem PARAMS ((void));
125static void set_pe_value PARAMS ((char *));
126static void set_pe_stack_heap PARAMS ((char *, char *));
127
128#ifdef DLL_SUPPORT
b34976b6 129static bfd_boolean pe_undef_cdecl_match
3fc90ddb
AJ
130 PARAMS ((struct bfd_link_hash_entry *, PTR));
131static void pe_fixup_stdcalls PARAMS ((void));
0d888aac 132static int make_import_fixup PARAMS ((arelent *, asection *));
3fc90ddb
AJ
133static void pe_find_data_imports PARAMS ((void));
134#endif
135
4cd89863 136static bfd_boolean pr_sym PARAMS ((struct bfd_hash_entry *, PTR));
b34976b6 137static bfd_boolean gld_${EMULATION_NAME}_unrecognized_file
3fc90ddb 138 PARAMS ((lang_input_statement_type *));
b34976b6 139static bfd_boolean gld_${EMULATION_NAME}_recognized_file
3fc90ddb
AJ
140 PARAMS ((lang_input_statement_type *));
141static int gld_${EMULATION_NAME}_find_potential_libraries
142 PARAMS ((char *, lang_input_statement_type *));
143
252b5132
RH
144
145static struct internal_extra_pe_aouthdr pe;
146static int dll;
147static int support_old_code = 0;
6f798e5c 148static char * thumb_entry_symbol = NULL;
252b5132
RH
149static lang_assignment_statement_type *image_base_statement = 0;
150
f0c87f88 151#ifdef DLL_SUPPORT
4cd89863 152static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable. */
690a460e
NC
153static char *pe_out_def_filename = NULL;
154static char *pe_implib_filename = NULL;
5f577f7d 155static int pe_enable_auto_image_base = 0;
627427de 156static char *pe_dll_search_prefix = NULL;
f0c87f88 157#endif
252b5132
RH
158
159extern const char *output_filename;
160
161static void
162gld_${EMULATION_NAME}_before_parse()
163{
86af25fe
L
164 const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
165 if (arch)
166 {
167 ldfile_output_architecture = arch->arch;
168 ldfile_output_machine = arch->mach;
169 ldfile_output_machine_name = arch->printable_name;
170 }
171 else
172 ldfile_output_architecture = bfd_arch_${ARCH};
db8d4f23 173 output_filename = "${EXECUTABLE_NAME:-a.exe}";
c6c37250 174#ifdef DLL_SUPPORT
b34976b6 175 config.dynamic_link = TRUE;
252b5132 176 config.has_shared = 1;
e0076ab3 177 link_info.pei386_auto_import = -1;
b34976b6 178 link_info.pei386_runtime_pseudo_reloc = FALSE;
344a211f
NC
179
180#if (PE_DEF_SUBSYSTEM == 9) || (PE_DEF_SUBSYSTEM == 2)
181#if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
182 lang_add_entry ("WinMainCRTStartup", 1);
183#else
184 lang_add_entry ("_WinMainCRTStartup", 1);
185#endif
186#endif
252b5132
RH
187#endif
188}
189\f
190/* PE format extra command line options. */
191
4cd89863 192/* Used for setting flags in the PE header. */
252b5132
RH
193#define OPTION_BASE_FILE (300 + 1)
194#define OPTION_DLL (OPTION_BASE_FILE + 1)
195#define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
196#define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
197#define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
198#define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
199#define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
200#define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
201#define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
202#define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
203#define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
204#define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
205#define OPTION_SUBSYSTEM (OPTION_STACK + 1)
206#define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
207#define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
208#define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1)
209#define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1)
210#define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1)
211#define OPTION_KILL_ATS (OPTION_EXCLUDE_SYMBOLS + 1)
212#define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1)
213#define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1)
214#define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1)
215#define OPTION_IMPLIB_FILENAME (OPTION_DISABLE_STDCALL_FIXUP + 1)
6f798e5c 216#define OPTION_THUMB_ENTRY (OPTION_IMPLIB_FILENAME + 1)
870df5dc
NC
217#define OPTION_WARN_DUPLICATE_EXPORTS (OPTION_THUMB_ENTRY + 1)
218#define OPTION_IMP_COMPAT (OPTION_WARN_DUPLICATE_EXPORTS + 1)
5f577f7d
DD
219#define OPTION_ENABLE_AUTO_IMAGE_BASE (OPTION_IMP_COMPAT + 1)
220#define OPTION_DISABLE_AUTO_IMAGE_BASE (OPTION_ENABLE_AUTO_IMAGE_BASE + 1)
1122a5fc
NC
221#define OPTION_DLL_SEARCH_PREFIX (OPTION_DISABLE_AUTO_IMAGE_BASE + 1)
222#define OPTION_NO_DEFAULT_EXCLUDES (OPTION_DLL_SEARCH_PREFIX + 1)
decc3638
CW
223#define OPTION_DLL_ENABLE_AUTO_IMPORT (OPTION_NO_DEFAULT_EXCLUDES + 1)
224#define OPTION_DLL_DISABLE_AUTO_IMPORT (OPTION_DLL_ENABLE_AUTO_IMPORT + 1)
225#define OPTION_ENABLE_EXTRA_PE_DEBUG (OPTION_DLL_DISABLE_AUTO_IMPORT + 1)
70b0be79 226#define OPTION_EXCLUDE_LIBS (OPTION_ENABLE_EXTRA_PE_DEBUG + 1)
2fa9fc65
NC
227#define OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC \
228 (OPTION_EXCLUDE_LIBS + 1)
229#define OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC \
230 (OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC + 1)
252b5132 231
3bcf5557
AM
232static void
233gld${EMULATION_NAME}_add_options (ns, shortopts, nl, longopts, nrl, really_longopts)
234 int ns ATTRIBUTE_UNUSED;
235 char **shortopts ATTRIBUTE_UNUSED;
236 int nl;
237 struct option **longopts;
238 int nrl ATTRIBUTE_UNUSED;
239 struct option **really_longopts ATTRIBUTE_UNUSED;
240{
241 static const struct option xtra_long[] = {
242 /* PE options */
243 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
244 {"dll", no_argument, NULL, OPTION_DLL},
245 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
246 {"heap", required_argument, NULL, OPTION_HEAP},
247 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
248 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
249 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
250 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
251 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
252 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
253 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
254 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
255 {"stack", required_argument, NULL, OPTION_STACK},
256 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
257 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
258 {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
c6c37250 259#ifdef DLL_SUPPORT
3bcf5557
AM
260 /* getopt allows abbreviations, so we do this to stop it from treating -o
261 as an abbreviation for this option */
262 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
263 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
264 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
265 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
266 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
267 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
268 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
269 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
270 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
271 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
272 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
273 /* getopt() allows abbreviations, so we do this to stop it from
274 treating -c as an abbreviation for these --compat-implib. */
275 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
276 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
277 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
278 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
279 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
280 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
281 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
282 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
283 {"enable-extra-pe-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
284 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
285 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
252b5132 286#endif
3bcf5557
AM
287 {NULL, no_argument, NULL, 0}
288 };
252b5132 289
3bcf5557
AM
290 *longopts = (struct option *)
291 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
292 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
293}
252b5132
RH
294
295/* PE/WIN32; added routines to get the subsystem type, heap and/or stack
4cd89863 296 parameters which may be input from the command line. */
252b5132
RH
297
298typedef struct
299{
300 void *ptr;
301 int size;
302 int value;
303 char *symbol;
304 int inited;
305} definfo;
306
307#define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
308
309static definfo init[] =
310{
311 /* imagebase must be first */
312#define IMAGEBASEOFF 0
313 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
314#define DLLOFF 1
1069dd8d 315 {&dll, sizeof(dll), 0, "__dll__", 0},
252b5132
RH
316 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
317 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
318 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
319 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
320 D(MajorImageVersion,"__major_image_version__", 1),
321 D(MinorImageVersion,"__minor_image_version__", 0),
344a211f
NC
322#ifdef TARGET_IS_armpe
323 D(MajorSubsystemVersion,"__major_subsystem_version__", 2),
324#else
252b5132 325 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
344a211f 326#endif
252b5132 327 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
2be9b2c7 328 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
3763134d 329 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000),
252b5132
RH
330 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
331 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
332 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
333 D(LoaderFlags,"__loader_flags__", 0x0),
334 { NULL, 0, 0, NULL, 0 }
335};
336
4cd89863 337
252b5132
RH
338static void
339gld_${EMULATION_NAME}_list_options (file)
340 FILE * file;
341{
342 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
343 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
344 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
345 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
346 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
347 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
348 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
349 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
350 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
351 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
352 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
353 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
354 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
355 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
356 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
6f798e5c 357 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
c6c37250 358#ifdef DLL_SUPPORT
252b5132
RH
359 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
360 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
361 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
362 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
b34976b6 363 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
252b5132
RH
364 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
365 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
366 fprintf (file, _(" --out-implib <file> Generate import library\n"));
367 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
0752a404 368 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
e3c8793a
NC
369 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
370 create __imp_<SYMBOL> as well.\n"));
371 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
372 unless user specifies one\n"));
5f577f7d 373 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
489d0400
NC
374 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
375 an importlib, use <string><basename>.dll\n\
e3c8793a 376 in preference to lib<basename>.dll \n"));
489d0400 377 fprintf (file, _(" --enable-auto-import Do sophistcated linking of _sym to\n\
e3c8793a 378 __imp_sym for DATA references\n"));
decc3638 379 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
2fa9fc65 380 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
489d0400
NC
381 adding pseudo-relocations resolved at\n\
382 runtime.\n"));
2fa9fc65
NC
383 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
384 auto-imported DATA.\n"));
e3c8793a
NC
385 fprintf (file, _(" --enable-extra-pe-debug Enable verbose debug output when building\n\
386 or linking to DLLs (esp. auto-import)\n"));
252b5132
RH
387#endif
388}
389
4cd89863 390
252b5132
RH
391static void
392set_pe_name (name, val)
393 char *name;
394 long val;
395{
396 int i;
4cd89863
NC
397
398 /* Find the name and set it. */
252b5132
RH
399 for (i = 0; init[i].ptr; i++)
400 {
401 if (strcmp (name, init[i].symbol) == 0)
402 {
403 init[i].value = val;
404 init[i].inited = 1;
405 return;
406 }
407 }
939ba9d0 408 abort ();
252b5132
RH
409}
410
411
412static void
413set_pe_subsystem ()
414{
415 const char *sver;
416 int len;
417 int i;
3fc90ddb 418 static const struct
252b5132
RH
419 {
420 const char *name;
421 const int value;
422 const char *entry;
423 }
424 v[] =
425 {
2be9b2c7 426 { "native", 1, "NtProcessStartup" },
344a211f 427#if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
2be9b2c7 428 { "windows", 2, "WinMainCRTStartup" },
344a211f
NC
429#else
430 { "windows", 2, "WinMainCRTStartup" },
431#endif
2be9b2c7 432 { "console", 3, "mainCRTStartup" },
252b5132
RH
433#if 0
434 /* The Microsoft linker does not recognize this. */
435 { "os2", 5, "" },
436#endif
2be9b2c7 437 { "posix", 7, "__PosixProcessStartup"},
344a211f 438 { "wince", 9, "_WinMainCRTStartup" },
252b5132
RH
439 { 0, 0, 0 }
440 };
441
442 sver = strchr (optarg, ':');
443 if (sver == NULL)
444 len = strlen (optarg);
445 else
446 {
447 char *end;
448
449 len = sver - optarg;
450 set_pe_name ("__major_subsystem_version__",
451 strtoul (sver + 1, &end, 0));
452 if (*end == '.')
453 set_pe_name ("__minor_subsystem_version__",
454 strtoul (end + 1, &end, 0));
455 if (*end != '\0')
456 einfo (_("%P: warning: bad version number in -subsystem option\n"));
457 }
458
459 for (i = 0; v[i].name; i++)
460 {
461 if (strncmp (optarg, v[i].name, len) == 0
462 && v[i].name[len] == '\0')
463 {
2be9b2c7
ILT
464 const char *initial_symbol_char;
465 const char *entry;
466
252b5132
RH
467 set_pe_name ("__subsystem__", v[i].value);
468
2be9b2c7
ILT
469 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
470 if (*initial_symbol_char == '\0')
471 entry = v[i].entry;
472 else
473 {
474 char *alc_entry;
475
476 /* lang_add_entry expects its argument to be permanently
477 allocated, so we don't free this string. */
478 alc_entry = xmalloc (strlen (initial_symbol_char)
479 + strlen (v[i].entry)
480 + 1);
481 strcpy (alc_entry, initial_symbol_char);
482 strcat (alc_entry, v[i].entry);
483 entry = alc_entry;
484 }
485
54f24647 486 lang_add_entry (entry, 0);
252b5132
RH
487
488 return;
489 }
490 }
3fc90ddb 491
252b5132
RH
492 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
493}
494
495
252b5132
RH
496static void
497set_pe_value (name)
498 char *name;
3fc90ddb 499
252b5132
RH
500{
501 char *end;
3fc90ddb 502
252b5132 503 set_pe_name (name, strtoul (optarg, &end, 0));
3fc90ddb 504
252b5132
RH
505 if (end == optarg)
506 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
507
508 optarg = end;
509}
510
4cd89863 511
252b5132
RH
512static void
513set_pe_stack_heap (resname, comname)
514 char *resname;
515 char *comname;
516{
517 set_pe_value (resname);
3fc90ddb 518
252b5132
RH
519 if (*optarg == ',')
520 {
521 optarg++;
522 set_pe_value (comname);
523 }
524 else if (*optarg)
525 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
526}
527
528
3bcf5557
AM
529static bfd_boolean
530gld${EMULATION_NAME}_handle_option (optc)
531 int optc;
252b5132 532{
252b5132
RH
533 switch (optc)
534 {
535 default:
3bcf5557 536 return FALSE;
252b5132
RH
537
538 case OPTION_BASE_FILE:
539 link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
540 if (link_info.base_file == NULL)
541 {
542 /* xgettext:c-format */
543 fprintf (stderr, _("%s: Can't open base file %s\n"),
544 program_name, optarg);
545 xexit (1);
546 }
547 break;
548
4cd89863 549 /* PE options. */
3fc90ddb 550 case OPTION_HEAP:
252b5132
RH
551 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
552 break;
3fc90ddb 553 case OPTION_STACK:
252b5132
RH
554 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
555 break;
556 case OPTION_SUBSYSTEM:
557 set_pe_subsystem ();
558 break;
559 case OPTION_MAJOR_OS_VERSION:
560 set_pe_value ("__major_os_version__");
561 break;
562 case OPTION_MINOR_OS_VERSION:
563 set_pe_value ("__minor_os_version__");
564 break;
565 case OPTION_MAJOR_SUBSYSTEM_VERSION:
566 set_pe_value ("__major_subsystem_version__");
567 break;
568 case OPTION_MINOR_SUBSYSTEM_VERSION:
569 set_pe_value ("__minor_subsystem_version__");
570 break;
571 case OPTION_MAJOR_IMAGE_VERSION:
572 set_pe_value ("__major_image_version__");
573 break;
574 case OPTION_MINOR_IMAGE_VERSION:
575 set_pe_value ("__minor_image_version__");
576 break;
577 case OPTION_FILE_ALIGNMENT:
578 set_pe_value ("__file_alignment__");
579 break;
580 case OPTION_SECTION_ALIGNMENT:
581 set_pe_value ("__section_alignment__");
582 break;
583 case OPTION_DLL:
584 set_pe_name ("__dll__", 1);
585 break;
586 case OPTION_IMAGE_BASE:
587 set_pe_value ("__image_base__");
588 break;
589 case OPTION_SUPPORT_OLD_CODE:
590 support_old_code = 1;
591 break;
6f798e5c
NC
592 case OPTION_THUMB_ENTRY:
593 thumb_entry_symbol = optarg;
594 break;
c6c37250 595#ifdef DLL_SUPPORT
252b5132
RH
596 case OPTION_OUT_DEF:
597 pe_out_def_filename = xstrdup (optarg);
598 break;
599 case OPTION_EXPORT_ALL:
600 pe_dll_export_everything = 1;
601 break;
602 case OPTION_EXCLUDE_SYMBOLS:
70b0be79
CF
603 pe_dll_add_excludes (optarg, 0);
604 break;
605 case OPTION_EXCLUDE_LIBS:
606 pe_dll_add_excludes (optarg, 1);
252b5132
RH
607 break;
608 case OPTION_KILL_ATS:
609 pe_dll_kill_ats = 1;
610 break;
611 case OPTION_STDCALL_ALIASES:
612 pe_dll_stdcall_aliases = 1;
613 break;
614 case OPTION_ENABLE_STDCALL_FIXUP:
615 pe_enable_stdcall_fixup = 1;
616 break;
617 case OPTION_DISABLE_STDCALL_FIXUP:
618 pe_enable_stdcall_fixup = 0;
619 break;
620 case OPTION_IMPLIB_FILENAME:
621 pe_implib_filename = xstrdup (optarg);
622 break;
870df5dc
NC
623 case OPTION_WARN_DUPLICATE_EXPORTS:
624 pe_dll_warn_dup_exports = 1;
625 break;
626 case OPTION_IMP_COMPAT:
627 pe_dll_compat_implib = 1;
628 break;
5f577f7d
DD
629 case OPTION_ENABLE_AUTO_IMAGE_BASE:
630 pe_enable_auto_image_base = 1;
631 break;
632 case OPTION_DISABLE_AUTO_IMAGE_BASE:
633 pe_enable_auto_image_base = 0;
634 break;
627427de 635 case OPTION_DLL_SEARCH_PREFIX:
939ba9d0 636 pe_dll_search_prefix = xstrdup (optarg);
627427de 637 break;
1122a5fc
NC
638 case OPTION_NO_DEFAULT_EXCLUDES:
639 pe_dll_do_default_excludes = 0;
640 break;
decc3638 641 case OPTION_DLL_ENABLE_AUTO_IMPORT:
e0076ab3 642 link_info.pei386_auto_import = 1;
decc3638
CW
643 break;
644 case OPTION_DLL_DISABLE_AUTO_IMPORT:
e0076ab3 645 link_info.pei386_auto_import = 0;
decc3638 646 break;
2fa9fc65
NC
647 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
648 link_info.pei386_runtime_pseudo_reloc = 1;
649 break;
650 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
651 link_info.pei386_runtime_pseudo_reloc = 0;
652 break;
decc3638
CW
653 case OPTION_ENABLE_EXTRA_PE_DEBUG:
654 pe_dll_extra_pe_debug = 1;
655 break;
c6c37250 656#endif
252b5132 657 }
3bcf5557 658 return TRUE;
252b5132
RH
659}
660\f
5f577f7d 661
2ef53d66 662#ifdef DLL_SUPPORT
3fc90ddb 663static unsigned long
5f577f7d
DD
664strhash (const char *str)
665{
666 const unsigned char *s;
667 unsigned long hash;
668 unsigned int c;
669 unsigned int len;
670
671 hash = 0;
672 len = 0;
673 s = (const unsigned char *) str;
674 while ((c = *s++) != '\0')
675 {
676 hash += c + (c << 17);
677 hash ^= hash >> 2;
678 ++len;
679 }
680 hash += len + (len << 17);
681 hash ^= hash >> 2;
682
683 return hash;
684}
685
4cd89863
NC
686/* Use the output file to create a image base for relocatable DLLs. */
687
5f577f7d
DD
688static unsigned long
689compute_dll_image_base (const char *ofile)
690{
691 unsigned long hash = strhash (ofile);
692 return 0x60000000 | ((hash << 16) & 0x0FFC0000);
693}
2ef53d66 694#endif
5f577f7d 695
252b5132
RH
696/* Assign values to the special symbols before the linker script is
697 read. */
698
699static void
700gld_${EMULATION_NAME}_set_symbols ()
701{
702 /* Run through and invent symbols for all the
4cd89863 703 names and insert the defaults. */
252b5132
RH
704 int j;
705 lang_statement_list_type *save;
706
707 if (!init[IMAGEBASEOFF].inited)
708 {
1049f94e 709 if (link_info.relocatable)
252b5132
RH
710 init[IMAGEBASEOFF].value = 0;
711 else if (init[DLLOFF].value || link_info.shared)
2ef53d66 712#ifdef DLL_SUPPORT
5f577f7d
DD
713 init[IMAGEBASEOFF].value = (pe_enable_auto_image_base) ?
714 compute_dll_image_base (output_filename) : NT_DLL_IMAGE_BASE;
2ef53d66
L
715#else
716 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
717#endif
252b5132
RH
718 else
719 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
720 }
721
1049f94e
AM
722 /* Don't do any symbol assignments if this is a relocatable link. */
723 if (link_info.relocatable)
252b5132
RH
724 return;
725
4cd89863 726 /* Glue the assignments into the abs section. */
252b5132
RH
727 save = stat_ptr;
728
729 stat_ptr = &(abs_output_section->children);
730
731 for (j = 0; init[j].ptr; j++)
732 {
733 long val = init[j].value;
734 lang_assignment_statement_type *rv;
2c382fb6
AM
735 rv = lang_add_assignment (exp_assop ('=', init[j].symbol,
736 exp_intop (val)));
939ba9d0
NC
737 if (init[j].size == sizeof (short))
738 *(short *) init[j].ptr = val;
739 else if (init[j].size == sizeof (int))
740 *(int *) init[j].ptr = val;
741 else if (init[j].size == sizeof (long))
742 *(long *) init[j].ptr = val;
252b5132 743 /* This might be a long long or other special type. */
939ba9d0
NC
744 else if (init[j].size == sizeof (bfd_vma))
745 *(bfd_vma *) init[j].ptr = val;
746 else abort ();
252b5132
RH
747 if (j == IMAGEBASEOFF)
748 image_base_statement = rv;
749 }
4cd89863 750 /* Restore the pointer. */
252b5132 751 stat_ptr = save;
3fc90ddb 752
252b5132
RH
753 if (pe.FileAlignment >
754 pe.SectionAlignment)
755 {
756 einfo (_("%P: warning, file alignment > section alignment.\n"));
757 }
758}
759
760/* This is called after the linker script and the command line options
761 have been read. */
762
763static void
764gld_${EMULATION_NAME}_after_parse ()
765{
766 /* The Windows libraries are designed for the linker to treat the
767 entry point as an undefined symbol. Otherwise, the .obj that
768 defines mainCRTStartup is brought in because it is the first
769 encountered in libc.lib and it has other symbols in it which will
770 be pulled in by the link process. To avoid this, we act as
771 though the user specified -u with the entry point symbol.
772
773 This function is called after the linker script and command line
774 options have been read, so at this point we know the right entry
775 point. This function is called before the input files are
776 opened, so registering the symbol as undefined will make a
777 difference. */
778
1049f94e 779 if (! link_info.relocatable && entry_symbol.name != NULL)
e3e942e9 780 ldlang_add_undef (entry_symbol.name);
252b5132
RH
781}
782
aa3d9aba
NC
783/* pe-dll.c directly accesses pe_data_import_dll,
784 so it must be defined outside of #ifdef DLL_SUPPORT.
785 Note - this variable is deliberately not initialised.
786 This allows it to be treated as a common varaible, and only
787 exist in one incarnation in a multiple target enabled linker. */
788char * pe_data_import_dll;
72358f65 789
2ef53d66 790#ifdef DLL_SUPPORT
252b5132
RH
791static struct bfd_link_hash_entry *pe_undef_found_sym;
792
b34976b6 793static bfd_boolean
252b5132
RH
794pe_undef_cdecl_match (h, string)
795 struct bfd_link_hash_entry *h;
796 PTR string;
797{
decc3638 798 int sl;
4cd89863
NC
799
800 sl = strlen (string); /* Silence compiler warning. */
252b5132
RH
801 if (h->type == bfd_link_hash_defined
802 && strncmp (h->root.string, string, sl) == 0
803 && h->root.string[sl] == '@')
decc3638
CW
804 {
805 pe_undef_found_sym = h;
b34976b6 806 return FALSE;
decc3638 807 }
b34976b6 808 return TRUE;
252b5132
RH
809}
810
811static void
812pe_fixup_stdcalls ()
813{
814 static int gave_warning_message = 0;
815 struct bfd_link_hash_entry *undef, *sym;
c9e38879 816
3fc90ddb 817 if (pe_dll_extra_pe_debug)
c9e38879 818 printf ("%s\n", __FUNCTION__);
3fc90ddb 819
252b5132
RH
820 for (undef = link_info.hash->undefs; undef; undef=undef->next)
821 if (undef->type == bfd_link_hash_undefined)
252b5132 822 {
c9e38879
NC
823 char* at = strchr (undef->root.string, '@');
824 int lead_at = (*undef->root.string == '@');
825 /* For now, don't try to fixup fastcall symbols. */
b34976b6 826
c9e38879
NC
827 if (at && !lead_at)
828 {
829 /* The symbol is a stdcall symbol, so let's look for a
830 cdecl symbol with the same name and resolve to that. */
831 char *cname = xstrdup (undef->root.string /* + lead_at */);
832 at = strchr (cname, '@');
833 *at = 0;
834 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
835
836 if (sym && sym->type == bfd_link_hash_defined)
837 {
838 undef->type = bfd_link_hash_defined;
839 undef->u.def.value = sym->u.def.value;
840 undef->u.def.section = sym->u.def.section;
4cd89863 841
c9e38879
NC
842 if (pe_enable_stdcall_fixup == -1)
843 {
844 einfo (_("Warning: resolving %s by linking to %s\n"),
845 undef->root.string, cname);
846 if (! gave_warning_message)
847 {
848 gave_warning_message = 1;
939ba9d0
NC
849 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
850 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
c9e38879
NC
851 }
852 }
853 }
854 }
855 else
856 {
857 /* The symbol is a cdecl symbol, so we look for stdcall
4cd89863 858 symbols - which means scanning the whole symbol table. */
c9e38879
NC
859 pe_undef_found_sym = 0;
860 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
861 (PTR) undef->root.string);
862 sym = pe_undef_found_sym;
863 if (sym)
864 {
865 undef->type = bfd_link_hash_defined;
866 undef->u.def.value = sym->u.def.value;
867 undef->u.def.section = sym->u.def.section;
4cd89863 868
c9e38879
NC
869 if (pe_enable_stdcall_fixup == -1)
870 {
871 einfo (_("Warning: resolving %s by linking to %s\n"),
872 undef->root.string, sym->root.string);
873 if (! gave_warning_message)
874 {
875 gave_warning_message = 1;
939ba9d0
NC
876 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
877 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
c9e38879
NC
878 }
879 }
880 }
881 }
252b5132 882 }
252b5132 883}
decc3638
CW
884
885static int
0d888aac 886make_import_fixup (rel, s)
decc3638 887 arelent *rel;
0d888aac 888 asection *s;
decc3638
CW
889{
890 struct symbol_cache_entry *sym = *rel->sym_ptr_ptr;
c9e38879 891 int addend = 0;
decc3638 892
3fc90ddb 893 if (pe_dll_extra_pe_debug)
c9e38879
NC
894 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
895 (long) rel->address, (long) rel->addend);
0d888aac 896
939ba9d0 897 if (! bfd_get_section_contents (s->owner, s, &addend, rel->address, sizeof (addend)))
c9e38879
NC
898 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
899 s->owner, s, rel->address);
0d888aac 900
2fa9fc65 901 pe_create_import_fixup (rel, s, addend);
0d888aac 902
decc3638
CW
903 return 1;
904}
905
decc3638
CW
906static void
907pe_find_data_imports ()
908{
909 struct bfd_link_hash_entry *undef, *sym;
c9e38879 910
4cd89863
NC
911 if (link_info.pei386_auto_import == 0)
912 return;
913
decc3638
CW
914 for (undef = link_info.hash->undefs; undef; undef=undef->next)
915 {
916 if (undef->type == bfd_link_hash_undefined)
917 {
4cd89863 918 /* C++ symbols are *long*. */
decc3638 919 char buf[4096];
c9e38879
NC
920
921 if (pe_dll_extra_pe_debug)
922 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
923
decc3638
CW
924 sprintf (buf, "__imp_%s", undef->root.string);
925
926 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
c9e38879 927
decc3638
CW
928 if (sym && sym->type == bfd_link_hash_defined)
929 {
4cd89863
NC
930 bfd *b = sym->u.def.section->owner;
931 asymbol **symbols;
932 int nsyms, symsize, i;
933
934 if (link_info.pei386_auto_import == -1)
935 info_msg (_("Info: resolving %s by linking to %s (auto-import)\n"),
936 undef->root.string, buf);
937
938 symsize = bfd_get_symtab_upper_bound (b);
939 symbols = (asymbol **) xmalloc (symsize);
940 nsyms = bfd_canonicalize_symtab (b, symbols);
941
942 for (i = 0; i < nsyms; i++)
943 {
944 if (memcmp (symbols[i]->name, "__head_",
945 sizeof ("__head_") - 1))
946 continue;
947
948 if (pe_dll_extra_pe_debug)
949 printf ("->%s\n", symbols[i]->name);
950
951 pe_data_import_dll = (char*) (symbols[i]->name +
952 sizeof ("__head_") - 1);
953 break;
954 }
955
956 pe_walk_relocs_of_symbol (&link_info, undef->root.string,
957 make_import_fixup);
958
959 /* Let's differentiate it somehow from defined. */
960 undef->type = bfd_link_hash_defweak;
961 /* We replace original name with __imp_ prefixed, this
962 1) may trash memory 2) leads to duplicate symbol generation.
963 Still, IMHO it's better than having name poluted. */
964 undef->root.string = sym->root.string;
965 undef->u.def.value = sym->u.def.value;
966 undef->u.def.section = sym->u.def.section;
decc3638
CW
967 }
968 }
969 }
970}
f0c87f88 971#endif /* DLL_SUPPORT */
252b5132 972
b34976b6 973static bfd_boolean
decc3638
CW
974pr_sym (h, string)
975 struct bfd_hash_entry *h;
3fc90ddb 976 PTR string ATTRIBUTE_UNUSED;
decc3638 977{
3fc90ddb 978 if (pe_dll_extra_pe_debug)
c9e38879
NC
979 printf ("+%s\n",h->string);
980
b34976b6 981 return TRUE;
decc3638
CW
982}
983
984
252b5132
RH
985static void
986gld_${EMULATION_NAME}_after_open ()
987{
3fc90ddb 988 if (pe_dll_extra_pe_debug)
decc3638
CW
989 {
990 bfd *a;
991 struct bfd_link_hash_entry *sym;
c9e38879 992
decc3638
CW
993 printf ("%s()\n", __FUNCTION__);
994
995 for (sym = link_info.hash->undefs; sym; sym=sym->next)
996 printf ("-%s\n", sym->root.string);
997 bfd_hash_traverse (&link_info.hash->table, pr_sym,NULL);
998
999 for (a = link_info.input_bfds; a; a = a->link_next)
c9e38879 1000 printf ("*%s\n",a->filename);
decc3638 1001 }
3fc90ddb 1002
252b5132
RH
1003 /* Pass the wacky PE command line options into the output bfd.
1004 FIXME: This should be done via a function, rather than by
1005 including an internal BFD header. */
3fc90ddb 1006
db09f25b 1007 if (coff_data (output_bfd) == NULL || coff_data (output_bfd)->pe == 0)
252b5132
RH
1008 einfo (_("%F%P: PE operations on non PE file.\n"));
1009
1010 pe_data (output_bfd)->pe_opthdr = pe;
1011 pe_data (output_bfd)->dll = init[DLLOFF].value;
1012
c6c37250 1013#ifdef DLL_SUPPORT
252b5132
RH
1014 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
1015 pe_fixup_stdcalls ();
1016
939ba9d0
NC
1017 pe_process_import_defs (output_bfd, & link_info);
1018
3fc90ddb 1019 pe_find_data_imports ();
decc3638 1020
2b817be1 1021#if ! (defined (TARGET_IS_i386pe) || defined (TARGET_IS_armpe))
252b5132 1022 if (link_info.shared)
2b817be1 1023#else
1049f94e 1024 if (!link_info.relocatable)
2b817be1 1025#endif
252b5132 1026 pe_dll_build_sections (output_bfd, &link_info);
344a211f
NC
1027
1028#ifndef TARGET_IS_i386pe
1029#ifndef TARGET_IS_armpe
1030 else
1031 pe_exe_build_sections (output_bfd, &link_info);
1032#endif
1033#endif
252b5132
RH
1034#endif
1035
626e0105 1036#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
f11523b0
NC
1037 if (strstr (bfd_get_target (output_bfd), "arm") == NULL)
1038 {
1039 /* The arm backend needs special fields in the output hash structure.
1040 These will only be created if the output format is an arm format,
1041 hence we do not support linking and changing output formats at the
1042 same time. Use a link followed by objcopy to change output formats. */
1043 einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n");
1044 return;
1045 }
252b5132
RH
1046 {
1047 /* Find a BFD that can hold the interworking stubs. */
1048 LANG_FOR_EACH_INPUT_STATEMENT (is)
1049 {
46d23b7c 1050 if (bfd_arm_pe_get_bfd_for_interworking (is->the_bfd, & link_info))
252b5132
RH
1051 break;
1052 }
1053 }
1054#endif
c6c37250 1055
486e80e2 1056 {
486e80e2
DD
1057 /* This next chunk of code tries to detect the case where you have
1058 two import libraries for the same DLL (specifically,
1059 symbolically linking libm.a and libc.a in cygwin to
1060 libcygwin.a). In those cases, it's possible for function
1061 thunks from the second implib to be used but without the
1062 head/tail objects, causing an improper import table. We detect
1063 those cases and rename the "other" import libraries to match
1064 the one the head/tail come from, so that the linker will sort
4cd89863 1065 things nicely and produce a valid import table. */
486e80e2
DD
1066
1067 LANG_FOR_EACH_INPUT_STATEMENT (is)
1068 {
1069 if (is->the_bfd->my_archive)
1070 {
1071 int idata2 = 0, reloc_count=0, is_imp = 0;
1072 asection *sec;
3fc90ddb 1073
6e45556a 1074 /* See if this is an import library thunk. */
486e80e2
DD
1075 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1076 {
1077 if (strcmp (sec->name, ".idata\$2") == 0)
1078 idata2 = 1;
1079 if (strncmp (sec->name, ".idata\$", 7) == 0)
1080 is_imp = 1;
1081 reloc_count += sec->reloc_count;
1082 }
3fc90ddb 1083
486e80e2
DD
1084 if (is_imp && !idata2 && reloc_count)
1085 {
6e45556a
NC
1086 /* It is, look for the reference to head and see if it's
1087 from our own library. */
486e80e2
DD
1088 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1089 {
1090 int i;
6e45556a
NC
1091 long symsize;
1092 long relsize;
102c86f8 1093 asymbol **symbols;
102c86f8
NC
1094 arelent **relocs;
1095 int nrelocs;
3fc90ddb 1096
102c86f8 1097 symsize = bfd_get_symtab_upper_bound (is->the_bfd);
6e45556a
NC
1098 if (symsize < 1)
1099 break;
102c86f8 1100 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
6e45556a
NC
1101 if (relsize < 1)
1102 break;
3fc90ddb 1103
6e45556a 1104 symbols = (asymbol **) xmalloc (symsize);
3fc90ddb 1105 symsize = bfd_canonicalize_symtab (is->the_bfd, symbols);
6e45556a
NC
1106 if (symsize < 0)
1107 {
1108 einfo ("%X%P: unable to process symbols: %E");
1109 return;
1110 }
3fc90ddb 1111
102c86f8
NC
1112 relocs = (arelent **) xmalloc ((size_t) relsize);
1113 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
486e80e2 1114 relocs, symbols);
6e45556a
NC
1115 if (nrelocs < 0)
1116 {
1117 free (relocs);
1118 einfo ("%X%P: unable to process relocs: %E");
1119 return;
1120 }
3fc90ddb 1121
6e45556a 1122 for (i = 0; i < nrelocs; i++)
486e80e2
DD
1123 {
1124 struct symbol_cache_entry *s;
6e45556a
NC
1125 struct bfd_link_hash_entry * blhe;
1126 bfd *other_bfd;
1127 char *n;
3fc90ddb 1128
486e80e2 1129 s = (relocs[i]->sym_ptr_ptr)[0];
3fc90ddb 1130
6e45556a
NC
1131 if (s->flags & BSF_LOCAL)
1132 continue;
3fc90ddb 1133
6e45556a
NC
1134 /* Thunk section with reloc to another bfd. */
1135 blhe = bfd_link_hash_lookup (link_info.hash,
1136 s->name,
b34976b6 1137 FALSE, FALSE, TRUE);
3fc90ddb 1138
6e45556a
NC
1139 if (blhe == NULL
1140 || blhe->type != bfd_link_hash_defined)
1141 continue;
3fc90ddb 1142
6e45556a 1143 other_bfd = blhe->u.def.section->owner;
3fc90ddb 1144
6e45556a
NC
1145 if (strcmp (is->the_bfd->my_archive->filename,
1146 other_bfd->my_archive->filename) == 0)
1147 continue;
3fc90ddb 1148
6e45556a
NC
1149 /* Rename this implib to match the other. */
1150 n = (char *) xmalloc (strlen (other_bfd->my_archive->filename) + 1);
3fc90ddb 1151
6e45556a 1152 strcpy (n, other_bfd->my_archive->filename);
3fc90ddb 1153
6e45556a 1154 is->the_bfd->my_archive->filename = n;
486e80e2
DD
1155 }
1156
1157 free (relocs);
6e45556a
NC
1158 /* Note - we do not free the symbols,
1159 they are now cached in the BFD. */
486e80e2
DD
1160 }
1161 }
1162 }
1163 }
1164 }
1165
c6c37250 1166 {
f0c87f88 1167 int is_ms_arch = 0;
1069dd8d 1168 bfd *cur_arch = 0;
c6c37250 1169 lang_input_statement_type *is2;
1069dd8d 1170
c6c37250
DD
1171 /* Careful - this is a shell script. Watch those dollar signs! */
1172 /* Microsoft import libraries have every member named the same,
1173 and not in the right order for us to link them correctly. We
1174 must detect these and rename the members so that they'll link
1175 correctly. There are three types of objects: the head, the
1176 thunks, and the sentinel(s). The head is easy; it's the one
1177 with idata2. We assume that the sentinels won't have relocs,
1178 and the thunks will. It's easier than checking the symbol
1069dd8d 1179 table for external references. */
c6c37250
DD
1180 LANG_FOR_EACH_INPUT_STATEMENT (is)
1181 {
1182 if (is->the_bfd->my_archive)
1183 {
1184 bfd *arch = is->the_bfd->my_archive;
1185 if (cur_arch != arch)
1186 {
1187 cur_arch = arch;
1188 is_ms_arch = 1;
1189 for (is2 = is;
1190 is2 && is2->the_bfd->my_archive == arch;
1191 is2 = (lang_input_statement_type *)is2->next)
1192 {
1193 if (strcmp (is->the_bfd->filename, is2->the_bfd->filename))
1194 is_ms_arch = 0;
1195 }
1196 }
1197
1198 if (is_ms_arch)
1199 {
1069dd8d 1200 int idata2 = 0, reloc_count=0;
c6c37250
DD
1201 asection *sec;
1202 char *new_name, seq;
1069dd8d 1203
c6c37250
DD
1204 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1205 {
1206 if (strcmp (sec->name, ".idata\$2") == 0)
1207 idata2 = 1;
1208 reloc_count += sec->reloc_count;
1209 }
1210
1211 if (idata2) /* .idata2 is the TOC */
1212 seq = 'a';
1213 else if (reloc_count > 0) /* thunks */
1214 seq = 'b';
1215 else /* sentinel */
1216 seq = 'c';
1217
e4e24acb 1218 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
c6c37250
DD
1219 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1220 is->the_bfd->filename = new_name;
1221
690a460e 1222 new_name = xmalloc (strlen (is->filename) + 3);
c6c37250
DD
1223 sprintf (new_name, "%s.%c", is->filename, seq);
1224 is->filename = new_name;
1225 }
1226 }
1227 }
1228 }
252b5132
RH
1229}
1230\f
3fc90ddb 1231static void
939ba9d0 1232gld_${EMULATION_NAME}_before_allocation ()
252b5132
RH
1233{
1234#ifdef TARGET_IS_ppcpe
c9e38879 1235 /* Here we rummage through the found bfds to collect toc information. */
252b5132
RH
1236 {
1237 LANG_FOR_EACH_INPUT_STATEMENT (is)
1238 {
1239 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
1240 {
1241 /* xgettext:c-format */
1242 einfo (_("Errors encountered processing file %s\n"), is->filename);
1243 }
1244 }
1245 }
1246
c9e38879 1247 /* We have seen it all. Allocate it, and carry on. */
252b5132
RH
1248 ppc_allocate_toc_section (&link_info);
1249#endif /* TARGET_IS_ppcpe */
1250
626e0105 1251#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
252b5132
RH
1252 /* FIXME: we should be able to set the size of the interworking stub
1253 section.
1254
1255 Here we rummage through the found bfds to collect glue
1256 information. FIXME: should this be based on a command line
c9e38879 1257 option? krk@cygnus.com. */
252b5132
RH
1258 {
1259 LANG_FOR_EACH_INPUT_STATEMENT (is)
1260 {
46d23b7c 1261 if (! bfd_arm_pe_process_before_allocation
252b5132
RH
1262 (is->the_bfd, & link_info, support_old_code))
1263 {
1264 /* xgettext:c-format */
1265 einfo (_("Errors encountered processing file %s for interworking"),
1266 is->filename);
1267 }
1268 }
1269 }
1270
c9e38879 1271 /* We have seen it all. Allocate it, and carry on. */
46d23b7c 1272 bfd_arm_pe_allocate_interworking_sections (& link_info);
252b5132
RH
1273#endif /* TARGET_IS_armpe */
1274}
1275\f
690a460e 1276#ifdef DLL_SUPPORT
252b5132 1277/* This is called when an input file isn't recognized as a BFD. We
c9e38879 1278 check here for .DEF files and pull them in automatically. */
690a460e 1279
252b5132 1280static int
c9e38879 1281saw_option (char * option)
252b5132
RH
1282{
1283 int i;
c9e38879
NC
1284
1285 for (i = 0; init[i].ptr; i++)
252b5132
RH
1286 if (strcmp (init[i].symbol, option) == 0)
1287 return init[i].inited;
1288 return 0;
1289}
690a460e 1290#endif /* DLL_SUPPORT */
252b5132 1291
b34976b6 1292static bfd_boolean
939ba9d0 1293gld_${EMULATION_NAME}_unrecognized_file (entry)
f0c87f88 1294 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
252b5132 1295{
c6c37250 1296#ifdef DLL_SUPPORT
252b5132
RH
1297 const char *ext = entry->filename + strlen (entry->filename) - 4;
1298
1299 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
252b5132 1300 {
c9e38879
NC
1301 if (pe_def_file == 0)
1302 pe_def_file = def_file_empty ();
1303
1304 def_file_parse (entry->filename, pe_def_file);
1305
1306 if (pe_def_file)
252b5132 1307 {
c9e38879
NC
1308 int i, buflen=0, len;
1309 char *buf;
252b5132 1310
c9e38879 1311 for (i = 0; i < pe_def_file->num_exports; i++)
252b5132 1312 {
939ba9d0 1313 len = strlen (pe_def_file->exports[i].internal_name);
c9e38879
NC
1314 if (buflen < len + 2)
1315 buflen = len + 2;
252b5132 1316 }
252b5132 1317
c9e38879 1318 buf = (char *) xmalloc (buflen);
252b5132 1319
c9e38879
NC
1320 for (i = 0; i < pe_def_file->num_exports; i++)
1321 {
1322 struct bfd_link_hash_entry *h;
1323
939ba9d0 1324 sprintf (buf, "_%s", pe_def_file->exports[i].internal_name);
c9e38879 1325
b34976b6 1326 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
c9e38879
NC
1327 if (h == (struct bfd_link_hash_entry *) NULL)
1328 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1329 if (h->type == bfd_link_hash_new)
1330 {
1331 h->type = bfd_link_hash_undefined;
1332 h->u.undef.abfd = NULL;
1333 bfd_link_add_undef (link_info.hash, h);
1334 }
1335 }
1336 free (buf);
1337
1338 /* def_file_print (stdout, pe_def_file); */
1339 if (pe_def_file->is_dll == 1)
1340 link_info.shared = 1;
1341
1342 if (pe_def_file->base_address != (bfd_vma)(-1))
1343 {
1344 pe.ImageBase =
1345 pe_data (output_bfd)->pe_opthdr.ImageBase =
1346 init[IMAGEBASEOFF].value = pe_def_file->base_address;
1347 init[IMAGEBASEOFF].inited = 1;
1348 if (image_base_statement)
1349 image_base_statement->exp =
1350 exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
1351 }
252b5132
RH
1352
1353#if 0
4cd89863 1354 /* Not sure if these *should* be set. */
c9e38879
NC
1355 if (pe_def_file->version_major != -1)
1356 {
1357 pe.MajorImageVersion = pe_def_file->version_major;
1358 pe.MinorImageVersion = pe_def_file->version_minor;
1359 }
252b5132 1360#endif
c9e38879
NC
1361 if (pe_def_file->stack_reserve != -1
1362 && ! saw_option ("__size_of_stack_reserve__"))
1363 {
1364 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
1365 if (pe_def_file->stack_commit != -1)
1366 pe.SizeOfStackCommit = pe_def_file->stack_commit;
1367 }
1368 if (pe_def_file->heap_reserve != -1
1369 && ! saw_option ("__size_of_heap_reserve__"))
1370 {
1371 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
1372 if (pe_def_file->heap_commit != -1)
1373 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
1374 }
b34976b6 1375 return TRUE;
c9e38879 1376 }
252b5132 1377 }
252b5132 1378#endif
b34976b6 1379 return FALSE;
252b5132
RH
1380}
1381
b34976b6 1382static bfd_boolean
939ba9d0 1383gld_${EMULATION_NAME}_recognized_file (entry)
f0c87f88 1384 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
252b5132 1385{
c6c37250 1386#ifdef DLL_SUPPORT
252b5132 1387#ifdef TARGET_IS_i386pe
c6c37250 1388 pe_dll_id_target ("pei-i386");
344a211f
NC
1389#endif
1390#ifdef TARGET_IS_shpe
1391 pe_dll_id_target ("pei-shl");
1392#endif
1393#ifdef TARGET_IS_mipspe
1394 pe_dll_id_target ("pei-mips");
1395#endif
1396#ifdef TARGET_IS_armpe
1397 pe_dll_id_target ("pei-arm-little");
c6c37250 1398#endif
252b5132
RH
1399 if (bfd_get_format (entry->the_bfd) == bfd_object)
1400 {
2a8ac465 1401 char fbuf[LD_PATHMAX + 1];
939ba9d0
NC
1402 const char *ext;
1403
1404 if (REALPATH (entry->filename, fbuf) == NULL)
2a8ac465 1405 strncpy (fbuf, entry->filename, sizeof (fbuf));
939ba9d0
NC
1406
1407 ext = fbuf + strlen (fbuf) - 4;
c9e38879 1408
252b5132 1409 if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0)
939ba9d0 1410 return pe_implied_import_dll (fbuf);
252b5132
RH
1411 }
1412#endif
b34976b6 1413 return FALSE;
252b5132
RH
1414}
1415
1416static void
1417gld_${EMULATION_NAME}_finish ()
1418{
6f798e5c
NC
1419#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
1420 struct bfd_link_hash_entry * h;
1421
1422 if (thumb_entry_symbol != NULL)
1423 {
b34976b6
AM
1424 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol,
1425 FALSE, FALSE, TRUE);
3fc90ddb 1426
6f798e5c
NC
1427 if (h != (struct bfd_link_hash_entry *) NULL
1428 && (h->type == bfd_link_hash_defined
1429 || h->type == bfd_link_hash_defweak)
1430 && h->u.def.section->output_section != NULL)
1431 {
1432 static char buffer[32];
1433 bfd_vma val;
3fc90ddb 1434
6f798e5c
NC
1435 /* Special procesing is required for a Thumb entry symbol. The
1436 bottom bit of its address must be set. */
1437 val = (h->u.def.value
1438 + bfd_get_section_vma (output_bfd,
1439 h->u.def.section->output_section)
1440 + h->u.def.section->output_offset);
3fc90ddb 1441
6f798e5c 1442 val |= 1;
3fc90ddb 1443
6f798e5c
NC
1444 /* Now convert this value into a string and store it in entry_symbol
1445 where the lang_finish() function will pick it up. */
1446 buffer[0] = '0';
1447 buffer[1] = 'x';
3fc90ddb 1448
6f798e5c 1449 sprintf_vma (buffer + 2, val);
3fc90ddb 1450
e3e942e9 1451 if (entry_symbol.name != NULL && entry_from_cmdline)
6f798e5c 1452 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
e3e942e9
AM
1453 thumb_entry_symbol, entry_symbol.name);
1454 entry_symbol.name = buffer;
6f798e5c
NC
1455 }
1456 else
1457 einfo (_("%P: warning: connot find thumb start symbol %s\n"), thumb_entry_symbol);
1458 }
1459#endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) */
1460
c6c37250 1461#ifdef DLL_SUPPORT
2b817be1
NC
1462 if (link_info.shared
1463#if !defined(TARGET_IS_shpe) && !defined(TARGET_IS_mipspe)
1049f94e 1464 || (!link_info.relocatable && pe_def_file->num_exports != 0)
2b817be1
NC
1465#endif
1466 )
252b5132
RH
1467 {
1468 pe_dll_fill_sections (output_bfd, &link_info);
1469 if (pe_implib_filename)
1470 pe_dll_generate_implib (pe_def_file, pe_implib_filename);
1471 }
344a211f 1472#if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe)
690a460e 1473 /* ARM doesn't need relocs. */
344a211f
NC
1474 else
1475 {
1476 pe_exe_fill_sections (output_bfd, &link_info);
1477 }
1478#endif
3fc90ddb 1479
252b5132
RH
1480 if (pe_out_def_filename)
1481 pe_dll_generate_def_file (pe_out_def_filename);
690a460e 1482#endif /* DLL_SUPPORT */
decc3638 1483
c9e38879 1484 /* I don't know where .idata gets set as code, but it shouldn't be. */
decc3638
CW
1485 {
1486 asection *asec = bfd_get_section_by_name (output_bfd, ".idata");
c9e38879 1487
decc3638
CW
1488 if (asec)
1489 {
1490 asec->flags &= ~SEC_CODE;
1491 asec->flags |= SEC_DATA;
1492 }
1493 }
252b5132
RH
1494}
1495
1496\f
ae7fb08f
AM
1497/* Find the last output section before given output statement.
1498 Used by place_orphan. */
1499
1500static asection *
1501output_prev_sec_find (os)
1502 lang_output_section_statement_type *os;
1503{
1504 asection *s = (asection *) NULL;
1505 lang_statement_union_type *u;
1506 lang_output_section_statement_type *lookup;
1507
1508 for (u = lang_output_section_statement.head;
1509 u != (lang_statement_union_type *) NULL;
1510 u = lookup->next)
1511 {
1512 lookup = &u->output_section_statement;
1513 if (lookup == os)
1514 return s;
1515
1516 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1517 s = lookup->bfd_section;
1518 }
1519
1520 return NULL;
1521}
1522
252b5132
RH
1523/* Place an orphan section.
1524
1525 We use this to put sections in a reasonable place in the file, and
1526 to ensure that they are aligned as required.
1527
1528 We handle grouped sections here as well. A section named .foo$nn
1529 goes into the output section .foo. All grouped sections are sorted
1530 by name.
1531
1532 Grouped sections for the default sections are handled by the
1533 default linker script using wildcards, and are sorted by
1534 sort_sections. */
1535
6a345e87
AM
1536struct orphan_save
1537{
1538 lang_output_section_statement_type *os;
5ba47421 1539 asection **section;
6a345e87
AM
1540 lang_statement_union_type **stmt;
1541};
252b5132 1542
b34976b6 1543static bfd_boolean
252b5132
RH
1544gld_${EMULATION_NAME}_place_orphan (file, s)
1545 lang_input_statement_type *file;
1546 asection *s;
1547{
1548 const char *secname;
aea4bd9d 1549 char *hold_section_name;
f0c87f88 1550 char *dollar = NULL;
2e418547 1551 const char *ps = NULL;
aea4bd9d 1552 lang_output_section_statement_type *os;
5ba47421 1553 lang_statement_list_type add_child;
252b5132 1554
252b5132
RH
1555 secname = bfd_get_section_name (s->owner, s);
1556
1557 /* Look through the script to see where to place this section. */
252b5132 1558 hold_section_name = xstrdup (secname);
1049f94e 1559 if (!link_info.relocatable)
6eec49fc
DD
1560 {
1561 dollar = strchr (hold_section_name, '$');
1562 if (dollar != NULL)
1563 *dollar = '\0';
1564 }
252b5132 1565
aea4bd9d 1566 os = lang_output_section_find (hold_section_name);
252b5132 1567
5ba47421
AM
1568 lang_list_init (&add_child);
1569
aea4bd9d 1570 if (os != NULL
ae7fb08f
AM
1571 && (os->bfd_section == NULL
1572 || ((s->flags ^ os->bfd_section->flags)
1573 & (SEC_LOAD | SEC_ALLOC)) == 0))
5ba47421 1574 {
ae7fb08f
AM
1575 /* We already have an output section statement with this
1576 name, and its bfd section, if any, has compatible flags. */
39dcfe18 1577 lang_add_section (&add_child, s, os, file);
5ba47421
AM
1578 }
1579 else
252b5132 1580 {
6a345e87 1581 struct orphan_save *place;
aea4bd9d
AM
1582 static struct orphan_save hold_text;
1583 static struct orphan_save hold_rdata;
1584 static struct orphan_save hold_data;
1585 static struct orphan_save hold_bss;
252b5132 1586 char *outsecname;
252b5132
RH
1587 lang_statement_list_type *old;
1588 lang_statement_list_type add;
1589 etree_type *address;
1590
1591 /* Try to put the new output section in a reasonable place based
1592 on the section name and section flags. */
aea4bd9d
AM
1593#define HAVE_SECTION(hold, name) \
1594(hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1595
252b5132 1596 place = NULL;
5ba47421
AM
1597 if ((s->flags & SEC_ALLOC) == 0)
1598 ;
1599 else if ((s->flags & SEC_HAS_CONTENTS) == 0
aea4bd9d 1600 && HAVE_SECTION (hold_bss, ".bss"))
6a345e87 1601 place = &hold_bss;
252b5132 1602 else if ((s->flags & SEC_READONLY) == 0
aea4bd9d 1603 && HAVE_SECTION (hold_data, ".data"))
6a345e87 1604 place = &hold_data;
252b5132
RH
1605 else if ((s->flags & SEC_CODE) == 0
1606 && (s->flags & SEC_READONLY) != 0
aea4bd9d 1607 && HAVE_SECTION (hold_rdata, ".rdata"))
6a345e87 1608 place = &hold_rdata;
252b5132 1609 else if ((s->flags & SEC_READONLY) != 0
aea4bd9d 1610 && HAVE_SECTION (hold_text, ".text"))
6a345e87 1611 place = &hold_text;
252b5132 1612
aea4bd9d
AM
1613#undef HAVE_SECTION
1614
252b5132
RH
1615 /* Choose a unique name for the section. This will be needed if
1616 the same section name appears in the input file with
ae7fb08f 1617 different loadable or allocatable characteristics. */
252b5132
RH
1618 outsecname = xstrdup (hold_section_name);
1619 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1620 {
1621 unsigned int len;
1622 char *newname;
1623 unsigned int i;
1624
1625 len = strlen (outsecname);
1626 newname = xmalloc (len + 5);
1627 strcpy (newname, outsecname);
1628 i = 0;
1629 do
1630 {
1631 sprintf (newname + len, "%d", i);
1632 ++i;
1633 }
1634 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
1635
1636 free (outsecname);
1637 outsecname = newname;
1638 }
1639
252b5132
RH
1640 /* Start building a list of statements for this section. */
1641 old = stat_ptr;
1642 stat_ptr = &add;
1643 lang_list_init (stat_ptr);
1644
2e418547
PB
1645 if (config.build_constructors)
1646 {
1647 /* If the name of the section is representable in C, then create
1648 symbols to mark the start and the end of the section. */
1649 for (ps = outsecname; *ps != '\0'; ps++)
29e1a6e4 1650 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
2e418547
PB
1651 break;
1652 if (*ps == '\0')
1653 {
1654 char *symname;
1655 etree_type *e_align;
3fc90ddb 1656
2e418547
PB
1657 symname = (char *) xmalloc (ps - outsecname + sizeof "___start_");
1658 sprintf (symname, "___start_%s", outsecname);
1659 e_align = exp_unop (ALIGN_K,
1660 exp_intop ((bfd_vma) 1 << s->alignment_power));
1661 lang_add_assignment (exp_assop ('=', symname, e_align));
1662 }
1663 }
3fc90ddb 1664
1049f94e 1665 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
01cc8ff8 1666 address = exp_intop ((bfd_vma) 0);
252b5132
RH
1667 else
1668 {
1669 /* All sections in an executable must be aligned to a page
1670 boundary. */
1671 address = exp_unop (ALIGN_K,
1672 exp_nameop (NAME, "__section_alignment__"));
1673 }
1674
aea4bd9d
AM
1675 os = lang_enter_output_section_statement (outsecname, address, 0,
1676 (bfd_vma) 0,
1677 (etree_type *) NULL,
1678 (etree_type *) NULL,
1679 (etree_type *) NULL);
252b5132 1680
39dcfe18 1681 lang_add_section (&add_child, s, os, file);
252b5132
RH
1682
1683 lang_leave_output_section_statement
1684 ((bfd_vma) 0, "*default*",
ee3cc2e2 1685 (struct lang_output_section_phdr_list *) NULL, NULL);
252b5132 1686
2e418547
PB
1687 if (config.build_constructors && *ps == '\0')
1688 {
1689 char *symname;
1690
c9e38879
NC
1691 /* lang_leave_ouput_section_statement resets stat_ptr.
1692 Put stat_ptr back where we want it. */
2e418547
PB
1693 if (place != NULL)
1694 stat_ptr = &add;
3fc90ddb 1695
2e418547
PB
1696 symname = (char *) xmalloc (ps - outsecname + sizeof "___stop_");
1697 sprintf (symname, "___stop_%s", outsecname);
1698 lang_add_assignment (exp_assop ('=', symname,
1699 exp_nameop (NAME, ".")));
1700 }
1701
5ba47421
AM
1702 stat_ptr = old;
1703
25844aae 1704 if (place != NULL && os->bfd_section != NULL)
252b5132 1705 {
5ba47421
AM
1706 asection *snew, **pps;
1707
aea4bd9d 1708 snew = os->bfd_section;
ae7fb08f
AM
1709
1710 /* Shuffle the bfd section list to make the output file look
1711 neater. This is really only cosmetic. */
1712 if (place->section == NULL)
5ba47421 1713 {
ae7fb08f
AM
1714 asection *bfd_section = place->os->bfd_section;
1715
1716 /* If the output statement hasn't been used to place
1717 any input sections (and thus doesn't have an output
1718 bfd_section), look for the closest prior output statement
1719 having an output section. */
1720 if (bfd_section == NULL)
1721 bfd_section = output_prev_sec_find (place->os);
1722
1723 if (bfd_section != NULL && bfd_section != snew)
1724 place->section = &bfd_section->next;
1725 }
5ba47421 1726
ae7fb08f
AM
1727 if (place->section != NULL)
1728 {
3dfe7a8c 1729 /* Unlink the section. */
ae7fb08f
AM
1730 for (pps = &output_bfd->sections;
1731 *pps != snew;
1732 pps = &(*pps)->next)
5ba47421 1733 ;
3dfe7a8c 1734 bfd_section_list_remove (output_bfd, pps);
5ba47421
AM
1735
1736 /* Now tack it on to the "place->os" section list. */
3dfe7a8c 1737 bfd_section_list_insert (output_bfd, place->section, snew);
5ba47421 1738 }
5ba47421 1739
ae7fb08f
AM
1740 /* Save the end of this list. Further ophans of this type will
1741 follow the one we've just added. */
1742 place->section = &snew->next;
1743
1744 /* The following is non-cosmetic. We try to put the output
1745 statements in some sort of reasonable order here, because
1746 they determine the final load addresses of the orphan
1747 sections. In addition, placing output statements in the
1748 wrong order may require extra segments. For instance,
1749 given a typical situation of all read-only sections placed
1750 in one segment and following that a segment containing all
1751 the read-write sections, we wouldn't want to place an orphan
1752 read/write section before or amongst the read-only ones. */
1753 if (add.head != NULL)
6a345e87 1754 {
ae7fb08f
AM
1755 if (place->stmt == NULL)
1756 {
1757 /* Put the new statement list right at the head. */
1758 *add.tail = place->os->header.next;
1759 place->os->header.next = add.head;
1760 }
1761 else
1762 {
1763 /* Put it after the last orphan statement we added. */
1764 *add.tail = *place->stmt;
1765 *place->stmt = add.head;
1766 }
1767
1768 /* Fix the global list pointer if we happened to tack our
1769 new list at the tail. */
1770 if (*old->tail == add.head)
1771 old->tail = add.tail;
1772
1773 /* Save the end of this list. */
1774 place->stmt = add.tail;
6a345e87 1775 }
252b5132 1776 }
252b5132
RH
1777 }
1778
5ba47421 1779 {
aea4bd9d 1780 lang_statement_union_type **pl = &os->children.head;
252b5132 1781
5ba47421
AM
1782 if (dollar != NULL)
1783 {
b34976b6 1784 bfd_boolean found_dollar;
252b5132 1785
5ba47421
AM
1786 /* The section name has a '$'. Sort it with the other '$'
1787 sections. */
b34976b6 1788 found_dollar = FALSE;
bba1a0c0 1789 for ( ; *pl != NULL; pl = &(*pl)->header.next)
5ba47421
AM
1790 {
1791 lang_input_section_type *ls;
1792 const char *lname;
252b5132 1793
5ba47421
AM
1794 if ((*pl)->header.type != lang_input_section_enum)
1795 continue;
252b5132 1796
5ba47421 1797 ls = &(*pl)->input_section;
252b5132 1798
5ba47421
AM
1799 lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section);
1800 if (strchr (lname, '$') == NULL)
1801 {
1802 if (found_dollar)
1803 break;
1804 }
1805 else
1806 {
b34976b6 1807 found_dollar = TRUE;
5ba47421
AM
1808 if (strcmp (secname, lname) < 0)
1809 break;
1810 }
1811 }
1812 }
1813
1814 if (add_child.head != NULL)
1815 {
bba1a0c0 1816 add_child.head->header.next = *pl;
5ba47421
AM
1817 *pl = add_child.head;
1818 }
1819 }
252b5132
RH
1820
1821 free (hold_section_name);
1822
b34976b6 1823 return TRUE;
252b5132
RH
1824}
1825
b34976b6 1826static bfd_boolean
690a460e 1827gld_${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
2ef53d66 1828 const char * arch ATTRIBUTE_UNUSED;
690a460e
NC
1829 search_dirs_type * search;
1830 lang_input_statement_type * entry;
1831{
1832 const char * filename;
1833 char * string;
1834
1835 if (! entry->is_archive)
b34976b6 1836 return FALSE;
690a460e
NC
1837
1838 filename = entry->filename;
1839
1840 string = (char *) xmalloc (strlen (search->name)
3fc90ddb 1841 + strlen (filename)
602e90d3 1842 + sizeof "/lib.a.dll"
b9a69188
NC
1843#ifdef DLL_SUPPORT
1844 + (pe_dll_search_prefix ? strlen (pe_dll_search_prefix) : 0)
1845#endif
690a460e
NC
1846 + 1);
1847
c9e38879 1848 /* Try "libfoo.dll.a" first (preferred explicit import library for dll's. */
602e90d3 1849 sprintf (string, "%s/lib%s.dll.a", search->name, filename);
690a460e
NC
1850
1851 if (! ldfile_try_open_bfd (string, entry))
1852 {
c9e38879 1853 /* Try "foo.dll.a" next (alternate explicit import library for dll's. */
602e90d3 1854 sprintf (string, "%s/%s.dll.a", search->name, filename);
690a460e
NC
1855 if (! ldfile_try_open_bfd (string, entry))
1856 {
c9e38879
NC
1857 /* Try libfoo.a next. Normally, this would be interpreted as a static
1858 library, but it *could* be an import library. For backwards compatibility,
1859 libfoo.a needs to ==precede== libfoo.dll and foo.dll in the search,
1860 or sometimes errors occur when building legacy packages.
1861
1862 Putting libfoo.a here means that in a failure case (i.e. the library
1863 -lfoo is not found) we will search for libfoo.a twice before
1864 giving up -- once here, and once when searching for a "static" lib.
1865 for a "static" lib. */
0ad8cf4c 1866 /* Try "libfoo.a" (import lib, or static lib, but must
4cd89863 1867 take precedence over dll's). */
0ad8cf4c 1868 sprintf (string, "%s/lib%s.a", search->name, filename);
602e90d3 1869 if (! ldfile_try_open_bfd (string, entry))
627427de 1870 {
b9a69188
NC
1871#ifdef DLL_SUPPORT
1872 if (pe_dll_search_prefix)
3fc90ddb 1873 {
4cd89863 1874 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
627427de 1875 sprintf (string, "%s/%s%s.dll", search->name, pe_dll_search_prefix, filename);
0ad8cf4c
DD
1876 if (! ldfile_try_open_bfd (string, entry))
1877 {
4cd89863 1878 /* Try "libfoo.dll" (default preferred dll name). */
627427de
DD
1879 sprintf (string, "%s/lib%s.dll", search->name, filename);
1880 if (! ldfile_try_open_bfd (string, entry))
1881 {
4cd89863 1882 /* Finally, try "foo.dll" (alternate dll name). */
627427de
DD
1883 sprintf (string, "%s/%s.dll", search->name, filename);
1884 if (! ldfile_try_open_bfd (string, entry))
1885 {
1886 free (string);
b34976b6 1887 return FALSE;
627427de
DD
1888 }
1889 }
0ad8cf4c 1890 }
602e90d3 1891 }
c9e38879 1892 else /* pe_dll_search_prefix not specified. */
3fc90ddb 1893#endif
627427de 1894 {
c9e38879 1895 /* Try "libfoo.dll" (preferred dll name). */
627427de
DD
1896 sprintf (string, "%s/lib%s.dll", search->name, filename);
1897 if (! ldfile_try_open_bfd (string, entry))
1898 {
c9e38879 1899 /* Finally, try "foo.dll" (alternate dll name). */
627427de
DD
1900 sprintf (string, "%s/%s.dll", search->name, filename);
1901 if (! ldfile_try_open_bfd (string, entry))
1902 {
1903 free (string);
b34976b6 1904 return FALSE;
627427de
DD
1905 }
1906 }
b9a69188 1907 }
602e90d3 1908 }
690a460e
NC
1909 }
1910 }
602e90d3 1911
690a460e
NC
1912 entry->filename = string;
1913
b34976b6 1914 return TRUE;
690a460e
NC
1915}
1916
344a211f
NC
1917static int
1918gld_${EMULATION_NAME}_find_potential_libraries (name, entry)
1919 char * name;
1920 lang_input_statement_type * entry;
1921{
1922 return ldfile_open_file_search (name, entry, "", ".lib");
1923}
252b5132
RH
1924\f
1925static char *
939ba9d0 1926gld_${EMULATION_NAME}_get_script (isfile)
252b5132
RH
1927 int *isfile;
1928EOF
1929# Scripts compiled in.
1930# sed commands to quote an ld script as a C string.
597e2591 1931sc="-f stringify.sed"
252b5132
RH
1932
1933cat >>e${EMULATION_NAME}.c <<EOF
3fc90ddb 1934{
252b5132
RH
1935 *isfile = 0;
1936
1049f94e 1937 if (link_info.relocatable && config.build_constructors)
252b5132
RH
1938 return
1939EOF
b34976b6 1940sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1049f94e 1941echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
b34976b6
AM
1942sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1943echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1944sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1945echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1946sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1947echo ' ; else return' >> e${EMULATION_NAME}.c
1948sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1949echo '; }' >> e${EMULATION_NAME}.c
252b5132
RH
1950
1951cat >>e${EMULATION_NAME}.c <<EOF
1952
1953
3fc90ddb 1954struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
252b5132
RH
1955{
1956 gld_${EMULATION_NAME}_before_parse,
1957 syslib_default,
1958 hll_default,
1959 gld_${EMULATION_NAME}_after_parse,
1960 gld_${EMULATION_NAME}_after_open,
1961 after_allocation_default,
1962 set_output_arch_default,
1963 ldemul_default_target,
1964 gld_${EMULATION_NAME}_before_allocation,
1965 gld_${EMULATION_NAME}_get_script,
1966 "${EMULATION_NAME}",
1967 "${OUTPUT_FORMAT}",
4cd89863
NC
1968 gld_${EMULATION_NAME}_finish,
1969 NULL, /* Create output section statements. */
690a460e 1970 gld_${EMULATION_NAME}_open_dynamic_archive,
252b5132
RH
1971 gld_${EMULATION_NAME}_place_orphan,
1972 gld_${EMULATION_NAME}_set_symbols,
3bcf5557
AM
1973 NULL, /* parse_args */
1974 gld${EMULATION_NAME}_add_options,
1975 gld${EMULATION_NAME}_handle_option,
252b5132
RH
1976 gld_${EMULATION_NAME}_unrecognized_file,
1977 gld_${EMULATION_NAME}_list_options,
344a211f 1978 gld_${EMULATION_NAME}_recognized_file,
fac1652d 1979 gld_${EMULATION_NAME}_find_potential_libraries,
4cd89863 1980 NULL /* new_vers_pattern. */
252b5132
RH
1981};
1982EOF