* emultempl/pe.em (gld_i386_finish): generate import library
[deliverable/binutils-gdb.git] / ld / emultempl / pe.em
CommitLineData
5f8ac7e7
SC
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3cat >e${EMULATION_NAME}.c <<EOF
5f8ac7e7 4/* This file is part of GLD, the Gnu Linker.
3535c3c0 5 Copyright 1995, 96, 97, 1998 Free Software Foundation, Inc.
5f8ac7e7
SC
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
3535c3c0
NC
21/* For WINDOWS_NT */
22/* The original file generated returned different default scripts depending
23 on whether certain switches were set, but these switches pertain to the
24 Linux system and that particular version of coff. In the NT case, we
25 only determine if the subsystem is console or windows in order to select
26 the correct entry point by default. */
27
5f8ac7e7
SC
28#include "bfd.h"
29#include "sysdep.h"
30#include "bfdlink.h"
31#include "getopt.h"
3535c3c0 32#include "libiberty.h"
5f8ac7e7
SC
33#include "ld.h"
34#include "ldmain.h"
35#include "ldgram.h"
36#include "ldexp.h"
37#include "ldlang.h"
38#include "ldemul.h"
39#include "ldlex.h"
40#include "ldmisc.h"
41#include "ldctor.h"
42#include "ldfile.h"
43#include "coff/internal.h"
44#include "../bfd/libcoff.h"
eb8061bf 45#include "deffile.h"
5f8ac7e7 46
d4279937
SC
47#define TARGET_IS_${EMULATION_NAME}
48
fe6e2957
DE
49static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void));
50static void gld_${EMULATION_NAME}_after_open PARAMS ((void));
5f8ac7e7 51static void gld_${EMULATION_NAME}_before_parse PARAMS ((void));
5b6ca067 52static void gld_${EMULATION_NAME}_after_parse PARAMS ((void));
fe6e2957 53static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void));
5b6ca067 54static boolean gld_${EMULATION_NAME}_place_orphan
a1613b5f 55 PARAMS ((lang_input_statement_type *, asection *));
7617a822
ILT
56static void gld${EMULATION_NAME}_place_section
57 PARAMS ((lang_statement_union_type *));
fe6e2957
DE
58static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
59static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
60
5f8ac7e7
SC
61static struct internal_extra_pe_aouthdr pe;
62static int dll;
5b6ca067 63static int support_old_code = 0;
eb8061bf
DD
64def_file *pe_def_file = 0;
65static lang_assignment_statement_type *image_base_statement = 0;
66
67static char *pe_out_def_filename = 0;
68int pe_dll_export_everything = 0;
69int pe_dll_do_default_excludes = 1;
70int pe_dll_kill_ats = 0;
71int pe_dll_stdcall_aliases = 0;
e2586bc8 72int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable */
d3ca9a53 73static char *pe_implib_filename = 0;
5f8ac7e7 74
3535c3c0
NC
75extern const char *output_filename;
76
5f8ac7e7
SC
77static void
78gld_${EMULATION_NAME}_before_parse()
79{
3535c3c0 80 output_filename = "a.exe";
5f8ac7e7 81 ldfile_output_architecture = bfd_arch_${ARCH};
eb8061bf
DD
82#ifdef TARGET_IS_i386pe
83 config.has_shared = 1;
84#endif
5f8ac7e7 85}
fe6e2957
DE
86\f
87/* PE format extra command line options. */
5f8ac7e7
SC
88
89/* Used for setting flags in the PE header. */
90#define OPTION_BASE_FILE (300 + 1)
91#define OPTION_DLL (OPTION_BASE_FILE + 1)
92#define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
93#define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
94#define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
95#define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
96#define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
97#define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
98#define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
99#define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
100#define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
101#define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
102#define OPTION_SUBSYSTEM (OPTION_STACK + 1)
103#define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
5b6ca067 104#define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
eb8061bf
DD
105#define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1)
106#define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1)
107#define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1)
108#define OPTION_KILL_ATS (OPTION_EXCLUDE_SYMBOLS + 1)
109#define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1)
e2586bc8
DD
110#define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1)
111#define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1)
d3ca9a53 112#define OPTION_IMPLIB_FILENAME (OPTION_DISABLE_STDCALL_FIXUP + 1)
5f8ac7e7 113
5b6ca067
ILT
114static struct option longopts[] =
115{
5f8ac7e7 116 /* PE options */
5b6ca067
ILT
117 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
118 {"dll", no_argument, NULL, OPTION_DLL},
119 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
120 {"heap", required_argument, NULL, OPTION_HEAP},
121 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
122 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
123 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
124 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
125 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
126 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
127 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
128 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
129 {"stack", required_argument, NULL, OPTION_STACK},
130 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
131 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
d3ca9a53 132#ifdef TARGET_IS_i386pe
eb8061bf
DD
133 /* getopt allows abbreviations, so we do this to stop it from treating -o
134 as an abbreviation for this option */
135 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
136 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
137 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
138 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
139 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
140 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
e2586bc8
DD
141 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
142 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
d3ca9a53
DD
143 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
144#endif
5b6ca067
ILT
145 {NULL, no_argument, NULL, 0}
146};
5f8ac7e7
SC
147
148
149/* PE/WIN32; added routines to get the subsystem type, heap and/or stack
150 parameters which may be input from the command line */
151
5b6ca067
ILT
152typedef struct
153{
5f8ac7e7
SC
154 void *ptr;
155 int size;
156 int value;
157 char *symbol;
158 int inited;
159} definfo;
160
161#define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
162
163static definfo init[] =
164{
165 /* imagebase must be first */
166#define IMAGEBASEOFF 0
167 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
168#define DLLOFF 1
169 {&dll, sizeof(dll), 0, "__dll__"},
170 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
171 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
172 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
173 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
174 D(MajorImageVersion,"__major_image_version__", 1),
175 D(MinorImageVersion,"__minor_image_version__", 0),
176 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
177 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
178 D(Subsystem,"__subsystem__", 3),
417fe276 179 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000),
5f8ac7e7
SC
180 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
181 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
182 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
183 D(LoaderFlags,"__loader_flags__", 0x0),
3535c3c0 184 { NULL, 0, 0, NULL, 0 }
5f8ac7e7
SC
185};
186
5b6ca067
ILT
187static void
188gld_${EMULATION_NAME}_list_options (file)
189 FILE * file;
190{
191 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
192 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
193 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
194 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
195 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
196 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
197 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
198 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
199 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
200 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
201 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
202 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
203 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
204 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
205 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
d3ca9a53 206#ifdef TARGET_IS_i386pe
854d50bd 207 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
e2586bc8 208 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
d3ca9a53
DD
209 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
210 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
211 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
212 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
213 fprintf (file, _(" --out-implib <file> Generate import library\n"));
214 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
215#endif
5b6ca067 216}
5f8ac7e7
SC
217
218static void
219set_pe_name (name, val)
220 char *name;
221 long val;
222{
223 int i;
224 /* Find the name and set it. */
225 for (i = 0; init[i].ptr; i++)
226 {
227 if (strcmp (name, init[i].symbol) == 0)
228 {
229 init[i].value = val;
230 init[i].inited = 1;
231 return;
232 }
233 }
234 abort();
235}
236
237
238static void
239set_pe_subsystem ()
240{
3535c3c0
NC
241 const char *sver;
242 int len;
5f8ac7e7 243 int i;
3535c3c0 244 static const struct
5f8ac7e7 245 {
3535c3c0
NC
246 const char *name;
247 const int value;
248 const char *entry;
5f8ac7e7
SC
249 }
250 v[] =
251 {
3535c3c0
NC
252 { "native", 1, "_NtProcessStartup" },
253 { "windows", 2, "_WinMainCRTStartup" },
254 { "console", 3, "_mainCRTStartup" },
255#if 0
256 /* The Microsoft linker does not recognize this. */
257 { "os2", 5, "" },
258#endif
259 { "posix", 7, "___PosixProcessStartup"},
260 { 0, 0, 0 }
5f8ac7e7
SC
261 };
262
3535c3c0
NC
263 sver = strchr (optarg, ':');
264 if (sver == NULL)
265 len = strlen (optarg);
266 else
267 {
268 char *end;
269
270 len = sver - optarg;
271 set_pe_name ("__major_subsystem_version__",
272 strtoul (sver + 1, &end, 0));
273 if (*end == '.')
274 set_pe_name ("__minor_subsystem_version__",
275 strtoul (end + 1, &end, 0));
276 if (*end != '\0')
5b6ca067 277 einfo (_("%P: warning: bad version number in -subsystem option\n"));
3535c3c0
NC
278 }
279
5f8ac7e7
SC
280 for (i = 0; v[i].name; i++)
281 {
3535c3c0
NC
282 if (strncmp (optarg, v[i].name, len) == 0
283 && v[i].name[len] == '\0')
5f8ac7e7
SC
284 {
285 set_pe_name ("__subsystem__", v[i].value);
d0d63887 286
3535c3c0 287 lang_add_entry (v[i].entry, 1);
d0d63887 288
5f8ac7e7
SC
289 return;
290 }
291 }
5b6ca067
ILT
292
293 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
5f8ac7e7
SC
294}
295
296
297
298static void
299set_pe_value (name)
300 char *name;
301
302{
303 char *end;
5b6ca067 304
fe6e2957 305 set_pe_name (name, strtoul (optarg, &end, 0));
5b6ca067 306
5f8ac7e7 307 if (end == optarg)
5b6ca067 308 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
5f8ac7e7
SC
309
310 optarg = end;
311}
312
313static void
314set_pe_stack_heap (resname, comname)
315 char *resname;
316 char *comname;
317{
5f8ac7e7 318 set_pe_value (resname);
5b6ca067 319
5f8ac7e7
SC
320 if (*optarg == ',')
321 {
322 optarg++;
323 set_pe_value (comname);
324 }
325 else if (*optarg)
5b6ca067 326 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
5f8ac7e7
SC
327}
328
329
330
331static int
332gld_${EMULATION_NAME}_parse_args(argc, argv)
333 int argc;
334 char **argv;
335{
336 int longind;
337 int optc;
338 int prevoptind = optind;
339 int prevopterr = opterr;
2543860d
SC
340 int wanterror;
341 static int lastoptind = -1;
342
343 if (lastoptind != optind)
344 opterr = 0;
345 wanterror = opterr;
346
347 lastoptind = optind;
348
5f8ac7e7
SC
349 optc = getopt_long_only (argc, argv, "-", longopts, &longind);
350 opterr = prevopterr;
2543860d 351
5f8ac7e7
SC
352 switch (optc)
353 {
354 default:
2543860d
SC
355 if (wanterror)
356 xexit (1);
357 optind = prevoptind;
5f8ac7e7
SC
358 return 0;
359
360 case OPTION_BASE_FILE:
d4279937 361 link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
5f8ac7e7
SC
362 if (link_info.base_file == NULL)
363 {
5b6ca067
ILT
364 /* xgettext:c-format */
365 fprintf (stderr, _("%s: Can't open base file %s\n"),
5f8ac7e7
SC
366 program_name, optarg);
367 xexit (1);
368 }
369 break;
370
371 /* PE options */
372 case OPTION_HEAP:
a1613b5f 373 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
5f8ac7e7
SC
374 break;
375 case OPTION_STACK:
a1613b5f 376 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
5f8ac7e7
SC
377 break;
378 case OPTION_SUBSYSTEM:
379 set_pe_subsystem ();
380 break;
381 case OPTION_MAJOR_OS_VERSION:
382 set_pe_value ("__major_os_version__");
383 break;
384 case OPTION_MINOR_OS_VERSION:
385 set_pe_value ("__minor_os_version__");
386 break;
387 case OPTION_MAJOR_SUBSYSTEM_VERSION:
388 set_pe_value ("__major_subsystem_version__");
389 break;
390 case OPTION_MINOR_SUBSYSTEM_VERSION:
2543860d 391 set_pe_value ("__minor_subsystem_version__");
5f8ac7e7
SC
392 break;
393 case OPTION_MAJOR_IMAGE_VERSION:
394 set_pe_value ("__major_image_version__");
395 break;
396 case OPTION_MINOR_IMAGE_VERSION:
397 set_pe_value ("__minor_image_version__");
398 break;
399 case OPTION_FILE_ALIGNMENT:
400 set_pe_value ("__file_alignment__");
401 break;
402 case OPTION_SECTION_ALIGNMENT:
403 set_pe_value ("__section_alignment__");
404 break;
405 case OPTION_DLL:
406 set_pe_name ("__dll__", 1);
407 break;
408 case OPTION_IMAGE_BASE:
409 set_pe_value ("__image_base__");
410 break;
5b6ca067
ILT
411 case OPTION_SUPPORT_OLD_CODE:
412 support_old_code = 1;
413 break;
eb8061bf
DD
414 case OPTION_OUT_DEF:
415 pe_out_def_filename = xstrdup (optarg);
416 break;
417 case OPTION_EXPORT_ALL:
418 pe_dll_export_everything = 1;
419 break;
420 case OPTION_EXCLUDE_SYMBOLS:
421 pe_dll_add_excludes (optarg);
422 break;
423 case OPTION_KILL_ATS:
424 pe_dll_kill_ats = 1;
425 break;
426 case OPTION_STDCALL_ALIASES:
427 pe_dll_stdcall_aliases = 1;
428 break;
e2586bc8
DD
429 case OPTION_ENABLE_STDCALL_FIXUP:
430 pe_enable_stdcall_fixup = 1;
431 break;
432 case OPTION_DISABLE_STDCALL_FIXUP:
433 pe_enable_stdcall_fixup = 0;
434 break;
d3ca9a53
DD
435 case OPTION_IMPLIB_FILENAME:
436 pe_implib_filename = xstrdup (optarg);
437 break;
5f8ac7e7
SC
438 }
439 return 1;
440}
fe6e2957 441\f
3535c3c0
NC
442/* Assign values to the special symbols before the linker script is
443 read. */
444
5f8ac7e7 445static void
5b6ca067 446gld_${EMULATION_NAME}_set_symbols ()
5f8ac7e7 447{
5f8ac7e7
SC
448 /* Run through and invent symbols for all the
449 names and insert the defaults. */
450 int j;
2543860d 451 lang_statement_list_type *save;
5f8ac7e7
SC
452
453 if (!init[IMAGEBASEOFF].inited)
3535c3c0
NC
454 {
455 if (link_info.relocateable)
456 init[IMAGEBASEOFF].value = 0;
eb8061bf 457 else if (init[DLLOFF].value || link_info.shared)
3535c3c0
NC
458 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
459 else
460 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
461 }
462
463 /* Don't do any symbol assignments if this is a relocateable link. */
464 if (link_info.relocateable)
465 return;
5f8ac7e7 466
2543860d 467 /* Glue the assignments into the abs section */
d4279937 468 save = stat_ptr;
2543860d
SC
469
470 stat_ptr = &(abs_output_section->children);
d4279937 471
5f8ac7e7
SC
472 for (j = 0; init[j].ptr; j++)
473 {
474 long val = init[j].value;
eb8061bf
DD
475 lang_assignment_statement_type *rv;
476 rv = lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val)));
5f8ac7e7
SC
477 if (init[j].size == sizeof(short))
478 *(short *)init[j].ptr = val;
479 else if (init[j].size == sizeof(int))
480 *(int *)init[j].ptr = val;
481 else if (init[j].size == sizeof(long))
482 *(long *)init[j].ptr = val;
3535c3c0
NC
483 /* This might be a long long or other special type. */
484 else if (init[j].size == sizeof(bfd_vma))
485 *(bfd_vma *)init[j].ptr = val;
5f8ac7e7 486 else abort();
eb8061bf
DD
487 if (j == IMAGEBASEOFF)
488 image_base_statement = rv;
5f8ac7e7 489 }
2543860d
SC
490 /* Restore the pointer. */
491 stat_ptr = save;
492
5f8ac7e7
SC
493 if (pe.FileAlignment >
494 pe.SectionAlignment)
495 {
5b6ca067 496 einfo (_("%P: warning, file alignment > section alignment.\n"));
5f8ac7e7
SC
497 }
498}
499
5b6ca067
ILT
500/* This is called after the linker script and the command line options
501 have been read. */
502
503static void
504gld_${EMULATION_NAME}_after_parse ()
505{
506 /* The Windows libraries are designed for the linker to treat the
507 entry point as an undefined symbol. Otherwise, the .obj that
508 defines mainCRTStartup is brought in because it is the first
509 encountered in libc.lib and it has other symbols in it which will
510 be pulled in by the link process. To avoid this, we act as
511 though the user specified -u with the entry point symbol.
512
513 This function is called after the linker script and command line
514 options have been read, so at this point we know the right entry
515 point. This function is called before the input files are
516 opened, so registering the symbol as undefined will make a
517 difference. */
518
519 ldlang_add_undef (entry_symbol);
520}
521
eb8061bf
DD
522static struct bfd_link_hash_entry *pe_undef_found_sym;
523
524static boolean
525pe_undef_cdecl_match (h, string)
526 struct bfd_link_hash_entry *h;
527 PTR string;
528{
529 int sl = strlen (string);
530 if (h->type == bfd_link_hash_defined
531 && strncmp (h->root.string, string, sl) == 0
532 && h->root.string[sl] == '@')
533 {
534 pe_undef_found_sym = h;
535 return false;
536 }
537 return true;
538}
539
540static void
541pe_fixup_stdcalls ()
542{
e2586bc8 543 static int gave_warning_message = 0;
eb8061bf
DD
544 struct bfd_link_hash_entry *undef, *sym;
545 char *at;
546 for (undef = link_info.hash->undefs; undef; undef=undef->next)
547 if (undef->type == bfd_link_hash_undefined)
548 {
549 at = strchr (undef->root.string, '@');
550 if (at)
551 {
552 /* The symbol is a stdcall symbol, so let's look for a cdecl
553 symbol with the same name and resolve to that */
554 char *cname = xstrdup (undef->root.string);
555 at = strchr (cname, '@');
556 *at = 0;
557 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
558 if (sym && sym->type == bfd_link_hash_defined)
559 {
560 undef->type = bfd_link_hash_defined;
561 undef->u.def.value = sym->u.def.value;
562 undef->u.def.section = sym->u.def.section;
e2586bc8
DD
563 if (pe_enable_stdcall_fixup == -1)
564 {
565 einfo (_("Warning: resolving %s by linking to %s\n"),
566 undef->root.string, cname);
567 if (! gave_warning_message)
568 {
569 gave_warning_message = 1;
570 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
571 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
572 }
573 }
eb8061bf
DD
574 }
575 }
576 else
577 {
578 /* The symbol is a cdecl symbol, so we don't look for stdcall
579 symbols - you should have included the right header */
580 pe_undef_found_sym = 0;
581 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
582 (PTR) undef->root.string);
583 sym = pe_undef_found_sym;
584 if (sym)
585 {
586 undef->type = bfd_link_hash_defined;
587 undef->u.def.value = sym->u.def.value;
588 undef->u.def.section = sym->u.def.section;
e2586bc8
DD
589 if (pe_enable_stdcall_fixup == -1)
590 {
591 einfo (_("Warning: resolving %s by linking to %s\n"),
592 undef->root.string, sym->root.string);
593 if (! gave_warning_message)
594 {
595 gave_warning_message = 1;
596 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
597 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
598 }
599 }
eb8061bf
DD
600 }
601 }
602 }
603}
604
5f8ac7e7 605static void
5b6ca067 606gld_${EMULATION_NAME}_after_open ()
5f8ac7e7 607{
3535c3c0
NC
608 /* Pass the wacky PE command line options into the output bfd.
609 FIXME: This should be done via a function, rather than by
610 including an internal BFD header. */
5b6ca067
ILT
611
612 if (!coff_data (output_bfd)->pe)
613 einfo (_("%F%P: PE operations on non PE file.\n"));
5f8ac7e7 614
5b6ca067
ILT
615 pe_data (output_bfd)->pe_opthdr = pe;
616 pe_data (output_bfd)->dll = init[DLLOFF].value;
5f8ac7e7 617
e2586bc8
DD
618 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
619 pe_fixup_stdcalls ();
eb8061bf
DD
620
621#ifdef TARGET_IS_i386pe
622 if (link_info.shared)
623 pe_dll_build_sections (output_bfd, &link_info);
624#endif
625
5b6ca067
ILT
626#ifdef TARGET_IS_armpe
627 {
628 /* Find a BFD that can hold the interworking stubs. */
629 LANG_FOR_EACH_INPUT_STATEMENT (is)
630 {
631 if (bfd_arm_get_bfd_for_interworking (is->the_bfd, & link_info))
632 break;
633 }
634 }
635#endif
5f8ac7e7 636}
a1613b5f 637\f
5f8ac7e7
SC
638static void
639gld_${EMULATION_NAME}_before_allocation()
640{
d4279937
SC
641#ifdef TARGET_IS_ppcpe
642 /* Here we rummage through the found bfds to collect toc information */
643 {
644 LANG_FOR_EACH_INPUT_STATEMENT (is)
645 {
5b6ca067 646 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
417fe276 647 {
5b6ca067
ILT
648 /* xgettext:c-format */
649 einfo (_("Errors encountered processing file %s\n"), is->filename);
417fe276 650 }
d4279937
SC
651 }
652 }
653
654 /* We have seen it all. Allocate it, and carry on */
655 ppc_allocate_toc_section (&link_info);
5b6ca067
ILT
656#endif /* TARGET_IS_ppcpe */
657
658#ifdef TARGET_IS_armpe
659 /* FIXME: we should be able to set the size of the interworking stub
660 section.
661
662 Here we rummage through the found bfds to collect glue
663 information. FIXME: should this be based on a command line
664 option? krk@cygnus.com */
3535c3c0
NC
665 {
666 LANG_FOR_EACH_INPUT_STATEMENT (is)
667 {
5b6ca067
ILT
668 if (! bfd_arm_process_before_allocation
669 (is->the_bfd, & link_info, support_old_code))
3535c3c0 670 {
5b6ca067
ILT
671 /* xgettext:c-format */
672 einfo (_("Errors encountered processing file %s for interworking"),
673 is->filename);
3535c3c0
NC
674 }
675 }
676 }
677
678 /* We have seen it all. Allocate it, and carry on */
5b6ca067
ILT
679 bfd_arm_allocate_interworking_sections (& link_info);
680#endif /* TARGET_IS_armpe */
5f8ac7e7 681}
eb8061bf
DD
682\f
683
684/* This is called when an input file isn't recognized as a BFD. We
685 check here for .DEF files and pull them in automatically. */
686
687static int
688saw_option(char *option)
689{
690 int i;
691 for (i=0; init[i].ptr; i++)
692 if (strcmp (init[i].symbol, option) == 0)
693 return init[i].inited;
694 return 0;
695}
696
697static boolean
698gld_${EMULATION_NAME}_unrecognized_file(entry)
699 lang_input_statement_type *entry;
700{
701#ifdef TARGET_IS_i386pe
702 const char *ext = entry->filename + strlen (entry->filename) - 4;
703
704 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
705 {
706 if (pe_def_file == 0)
707 pe_def_file = def_file_empty ();
708 def_file_parse (entry->filename, pe_def_file);
709 if (pe_def_file)
710 {
e2586bc8
DD
711 int i, buflen=0, len;
712 char *buf;
713 for (i=0; i<pe_def_file->num_exports; i++)
714 {
715 len = strlen(pe_def_file->exports[i].internal_name);
716 if (buflen < len+2)
717 buflen = len+2;
718 }
719 buf = (char *) xmalloc (buflen);
720 for (i=0; i<pe_def_file->num_exports; i++)
721 {
722 struct bfd_link_hash_entry *h;
723 sprintf(buf, "_%s", pe_def_file->exports[i].internal_name);
724
725 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
726 if (h == (struct bfd_link_hash_entry *) NULL)
727 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
728 if (h->type == bfd_link_hash_new)
729 {
730 h->type = bfd_link_hash_undefined;
731 h->u.undef.abfd = NULL;
732 bfd_link_add_undef (link_info.hash, h);
733 }
734 }
735 free (buf);
736
eb8061bf
DD
737 /* def_file_print (stdout, pe_def_file); */
738 if (pe_def_file->is_dll == 1)
739 link_info.shared = 1;
740
741 if (pe_def_file->base_address != (bfd_vma)(-1))
742 {
743 pe.ImageBase =
744 pe_data (output_bfd)->pe_opthdr.ImageBase =
745 init[IMAGEBASEOFF].value = pe_def_file->base_address;
746 init[IMAGEBASEOFF].inited = 1;
747 if (image_base_statement)
748 image_base_statement->exp =
749 exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
750 }
751
752#if 0
753 /* Not sure if these *should* be set */
754 if (pe_def_file->version_major != -1)
755 {
756 pe.MajorImageVersion = pe_def_file->version_major;
757 pe.MinorImageVersion = pe_def_file->version_minor;
758 }
759#endif
760 if (pe_def_file->stack_reserve != -1
761 && ! saw_option ("__size_of_stack_reserve__"))
762 {
763 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
764 if (pe_def_file->stack_commit != -1)
765 pe.SizeOfStackCommit = pe_def_file->stack_commit;
766 }
767 if (pe_def_file->heap_reserve != -1
768 && ! saw_option ("__size_of_heap_reserve__"))
769 {
770 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
771 if (pe_def_file->heap_commit != -1)
772 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
773 }
774 return true;
775 }
776 }
777#endif
778 return false;
779
780}
781
782static void
783gld_${EMULATION_NAME}_finish ()
784{
785#ifdef TARGET_IS_i386pe
786 if (link_info.shared)
d3ca9a53
DD
787 {
788 pe_dll_fill_sections (output_bfd, &link_info);
789 if (pe_implib_filename)
790 pe_dll_generate_implib (pe_def_file, pe_implib_filename);
791 }
eb8061bf
DD
792 if (pe_out_def_filename)
793 pe_dll_generate_def_file (pe_out_def_filename);
794#endif
795}
796
a1613b5f 797\f
7617a822
ILT
798/* Place an orphan section.
799
800 We use this to put sections in a reasonable place in the file, and
801 to ensure that they are aligned as required.
802
803 We handle grouped sections here as well. A section named .foo$nn
804 goes into the output section .foo. All grouped sections are sorted
805 by name.
3535c3c0 806
7617a822
ILT
807 Grouped sections for the default sections are handled by the
808 default linker script using wildcards, and are sorted by
809 sort_sections. */
810
811static asection *hold_section;
812static char *hold_section_name;
813static lang_output_section_statement_type *hold_use;
814static lang_output_section_statement_type *hold_text;
815static lang_output_section_statement_type *hold_rdata;
816static lang_output_section_statement_type *hold_data;
817static lang_output_section_statement_type *hold_bss;
818
819/* Place an orphan section. We use this to put random SHF_ALLOC
820 sections in the right segment. */
a1613b5f
DE
821
822/*ARGSUSED*/
823static boolean
5b6ca067 824gld_${EMULATION_NAME}_place_orphan (file, s)
a1613b5f
DE
825 lang_input_statement_type *file;
826 asection *s;
827{
fe6e2957 828 const char *secname;
7617a822 829 char *dollar;
a1613b5f
DE
830
831 if ((s->flags & SEC_ALLOC) == 0)
832 return false;
833
a1613b5f
DE
834 secname = bfd_get_section_name (s->owner, s);
835
7617a822
ILT
836 /* Look through the script to see where to place this section. */
837
838 hold_section = s;
839
840 hold_section_name = xstrdup (secname);
841 dollar = strchr (hold_section_name, '$');
842 if (dollar != NULL)
843 *dollar = '\0';
844
845 hold_use = NULL;
846 lang_for_each_statement (gld${EMULATION_NAME}_place_section);
a1613b5f 847
7617a822 848 if (hold_use == NULL)
a1613b5f 849 {
7617a822
ILT
850 lang_output_section_statement_type *place;
851 char *outsecname;
852 asection *snew, **pps;
853 lang_statement_list_type *old;
854 lang_statement_list_type add;
855 etree_type *address;
856
857 /* Try to put the new output section in a reasonable place based
858 on the section name and section flags. */
859 place = NULL;
860 if ((s->flags & SEC_HAS_CONTENTS) == 0
861 && hold_bss != NULL)
862 place = hold_bss;
863 else if ((s->flags & SEC_READONLY) == 0
864 && hold_data != NULL)
865 place = hold_data;
866 else if ((s->flags & SEC_CODE) == 0
867 && (s->flags & SEC_READONLY) != 0
868 && hold_rdata != NULL)
869 place = hold_rdata;
870 else if ((s->flags & SEC_READONLY) != 0
871 && hold_text != NULL)
872 place = hold_text;
873
874 /* Choose a unique name for the section. This will be needed if
875 the same section name appears in the input file with
876 different loadable or allocateable characteristics. */
877 outsecname = xstrdup (hold_section_name);
878 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
879 {
880 unsigned int len;
881 char *newname;
882 unsigned int i;
883
884 len = strlen (outsecname);
885 newname = xmalloc (len + 5);
886 strcpy (newname, outsecname);
887 i = 0;
888 do
889 {
890 sprintf (newname + len, "%d", i);
891 ++i;
892 }
893 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
894
895 free (outsecname);
896 outsecname = newname;
897 }
898
899 /* We don't want to free OUTSECNAME, as it may get attached to
900 the output section statement. */
901
902 /* Create the section in the output file, and put it in the
903 right place. This shuffling is to make the output file look
904 neater. */
905 snew = bfd_make_section (output_bfd, outsecname);
906 if (snew == NULL)
907 einfo ("%P%F: output format %s cannot represent section called %s\n",
908 output_bfd->xvec->name, outsecname);
909 if (place != NULL && place->bfd_section != NULL)
910 {
911 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
912 ;
913 *pps = snew->next;
914 snew->next = place->bfd_section->next;
915 place->bfd_section->next = snew;
916 }
917
918 /* Start building a list of statements for this section. */
919 old = stat_ptr;
920 stat_ptr = &add;
921 lang_list_init (stat_ptr);
922
923 if (link_info.relocateable)
924 address = NULL;
925 else
926 {
927 /* All sections in an executable must be aligned to a page
928 boundary. */
929 address = exp_unop (ALIGN_K,
930 exp_nameop (NAME, "__section_alignment__"));
931 }
932
933 lang_enter_output_section_statement (outsecname, address, 0,
934 (bfd_vma) 0,
935 (etree_type *) NULL,
936 (etree_type *) NULL,
937 (etree_type *) NULL);
938
939 hold_use = lang_output_section_statement_lookup (outsecname);
940
941 lang_leave_output_section_statement
942 ((bfd_vma) 0, "*default*",
943 (struct lang_output_section_phdr_list *) NULL);
944
945 /* Now stick the new statement list right after PLACE. */
946 if (place != NULL)
947 {
948 *add.tail = place->header.next;
949 place->header.next = add.head;
950 }
951
952 stat_ptr = old;
a1613b5f 953 }
7617a822
ILT
954
955 if (dollar == NULL)
956 wild_doit (&hold_use->children, s, hold_use, file);
957 else
a1613b5f 958 {
7617a822
ILT
959 lang_statement_union_type **pl;
960 boolean found_dollar;
961 lang_statement_list_type list;
962
963 /* The section name has a '$'. Sort it with the other '$'
964 sections. */
965
966 found_dollar = false;
967 for (pl = &hold_use->children.head; *pl != NULL; pl = &(*pl)->next)
968 {
969 lang_input_section_type *ls;
970 const char *lname;
971
972 if ((*pl)->header.type != lang_input_section_enum)
973 continue;
974
975 ls = &(*pl)->input_section;
976
977 lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section);
978 if (strchr (lname, '$') == NULL)
979 {
980 if (found_dollar)
981 break;
982 }
983 else
984 {
985 found_dollar = true;
986 if (strcmp (secname, lname) < 0)
987 break;
988 }
989 }
990
991 lang_list_init (&list);
992 wild_doit (&list, s, hold_use, file);
eb8061bf
DD
993 if (list.head != NULL)
994 {
995 ASSERT (list.head->next == NULL);
996 list.head->next = *pl;
997 *pl = list.head;
998 }
a1613b5f 999 }
a1613b5f 1000
7617a822 1001 free (hold_section_name);
a1613b5f
DE
1002
1003 return true;
1004}
7617a822
ILT
1005
1006static void
1007gld${EMULATION_NAME}_place_section (s)
1008 lang_statement_union_type *s;
1009{
1010 lang_output_section_statement_type *os;
1011
1012 if (s->header.type != lang_output_section_statement_enum)
1013 return;
1014
1015 os = &s->output_section_statement;
1016
1017 if (strcmp (os->name, hold_section_name) == 0
1018 && os->bfd_section != NULL
1019 && ((hold_section->flags & (SEC_LOAD | SEC_ALLOC))
1020 == (os->bfd_section->flags & (SEC_LOAD | SEC_ALLOC))))
1021 hold_use = os;
1022
1023 if (strcmp (os->name, ".text") == 0)
1024 hold_text = os;
1025 else if (strcmp (os->name, ".rdata") == 0)
1026 hold_rdata = os;
1027 else if (strcmp (os->name, ".data") == 0)
1028 hold_data = os;
1029 else if (strcmp (os->name, ".bss") == 0)
1030 hold_bss = os;
1031}
a1613b5f 1032\f
5f8ac7e7
SC
1033static char *
1034gld_${EMULATION_NAME}_get_script(isfile)
1035 int *isfile;
1036EOF
1037# Scripts compiled in.
1038# sed commands to quote an ld script as a C string.
1039sc="-f ${srcdir}/emultempl/stringify.sed"
1040
1041cat >>e${EMULATION_NAME}.c <<EOF
1042{
1043 *isfile = 0;
1044
1045 if (link_info.relocateable == true && config.build_constructors == true)
1046 return
1047EOF
1048sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1049echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1050sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1051echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1052sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1053echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1054sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1055echo ' ; else return' >> e${EMULATION_NAME}.c
1056sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1057echo '; }' >> e${EMULATION_NAME}.c
1058
1059cat >>e${EMULATION_NAME}.c <<EOF
1060
1061
5f8ac7e7
SC
1062struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1063{
1064 gld_${EMULATION_NAME}_before_parse,
1065 syslib_default,
1066 hll_default,
5b6ca067 1067 gld_${EMULATION_NAME}_after_parse,
5f8ac7e7
SC
1068 gld_${EMULATION_NAME}_after_open,
1069 after_allocation_default,
1070 set_output_arch_default,
1071 ldemul_default_target,
1072 gld_${EMULATION_NAME}_before_allocation,
1073 gld_${EMULATION_NAME}_get_script,
1074 "${EMULATION_NAME}",
1075 "${OUTPUT_FORMAT}",
eb8061bf 1076 gld_${EMULATION_NAME}_finish, /* finish */
5f8ac7e7
SC
1077 NULL, /* create output section statements */
1078 NULL, /* open dynamic archive */
5b6ca067 1079 gld_${EMULATION_NAME}_place_orphan,
5f8ac7e7 1080 gld_${EMULATION_NAME}_set_symbols,
5b6ca067 1081 gld_${EMULATION_NAME}_parse_args,
eb8061bf 1082 gld_${EMULATION_NAME}_unrecognized_file,
5b6ca067 1083 gld_${EMULATION_NAME}_list_options
5f8ac7e7
SC
1084};
1085EOF
This page took 0.163214 seconds and 4 git commands to generate.