]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/pe-dll.c
* ld-selective/selective.exp: Rearrange to be table-driven.
[thirdparty/binutils-gdb.git] / ld / pe-dll.c
CommitLineData
252b5132 1/* Routines to help build PEI-format DLLs (Win32 etc)
a6483292 2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
252b5132
RH
3 Written by DJ Delorie <dj@cygnus.com>
4
5 This file is part of GLD, the Gnu Linker.
6
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22#include "bfd.h"
23#include "sysdep.h"
24#include "bfdlink.h"
25#include "libiberty.h"
26
27#include <time.h>
28#include <ctype.h>
29
30#include "ld.h"
31#include "ldexp.h"
32#include "ldlang.h"
33#include "ldwrite.h"
34#include "ldmisc.h"
35#include "ldgram.h"
36#include "ldmain.h"
b71e2778 37#include "ldfile.h"
252b5132
RH
38#include "ldemul.h"
39#include "coff/internal.h"
40#include "../bfd/libcoff.h"
41#include "deffile.h"
1069dd8d 42#include "pe-dll.h"
252b5132
RH
43
44/************************************************************************
45
46 This file turns a regular Windows PE image into a DLL. Because of
47 the complexity of this operation, it has been broken down into a
48 number of separate modules which are all called by the main function
49 at the end of this file. This function is not re-entrant and is
50 normally only called once, so static variables are used to reduce
51 the number of parameters and return values required.
52
53 See also: ld/emultempl/pe.em
54
55 ************************************************************************/
56
57/* for emultempl/pe.em */
58
59def_file *pe_def_file = 0;
60int pe_dll_export_everything = 0;
61int pe_dll_do_default_excludes = 1;
62int pe_dll_kill_ats = 0;
63int pe_dll_stdcall_aliases = 0;
870df5dc
NC
64int pe_dll_warn_dup_exports = 0;
65int pe_dll_compat_implib = 0;
252b5132
RH
66
67/************************************************************************
68
69 static variables and types
70
71 ************************************************************************/
72
73static bfd_vma image_base;
74
75static bfd *filler_bfd;
76static struct sec *edata_s, *reloc_s;
77static unsigned char *edata_d, *reloc_d;
1069dd8d 78static size_t edata_sz, reloc_sz;
252b5132 79
c6c37250
DD
80typedef struct {
81 char *target_name;
82 char *object_target;
1069dd8d 83 unsigned int imagebase_reloc;
c6c37250
DD
84 int pe_arch;
85 int bfd_arch;
86 int underscored;
87} pe_details_type;
88
89#define PE_ARCH_i386 1
344a211f
NC
90#define PE_ARCH_sh 2
91#define PE_ARCH_mips 3
92#define PE_ARCH_arm 4
c6c37250
DD
93
94static pe_details_type pe_detail_list[] = {
95 {
96 "pei-i386",
97 "pe-i386",
98 7 /* R_IMAGEBASE */,
99 PE_ARCH_i386,
100 bfd_arch_i386,
101 1
102 },
344a211f
NC
103 {
104 "pei-shl",
105 "pe-shl",
106 16 /* R_SH_IMAGEBASE */,
107 PE_ARCH_sh,
108 bfd_arch_sh,
109 1
110 },
111 {
112 "pei-mips",
113 "pe-mips",
114 34 /* MIPS_R_RVA */,
115 PE_ARCH_mips,
116 bfd_arch_mips,
117 0
118 },
119 {
120 "pei-arm-little",
121 "pe-arm-little",
122 11 /* ARM_RVA32 */,
123 PE_ARCH_arm,
124 bfd_arch_arm,
125 0
126 },
1069dd8d 127 { NULL, NULL, 0, 0, 0, 0 }
c6c37250
DD
128};
129
130static pe_details_type *pe_details;
131
132#define U(str) (pe_details->underscored ? "_" str : str)
133
134void
135pe_dll_id_target (target)
1069dd8d 136 const char *target;
c6c37250
DD
137{
138 int i;
139 for (i=0; pe_detail_list[i].target_name; i++)
9d68bc82
DD
140 if (strcmp (pe_detail_list[i].target_name, target) == 0
141 || strcmp (pe_detail_list[i].object_target, target) == 0)
c6c37250
DD
142 {
143 pe_details = pe_detail_list+i;
144 return;
145 }
146 einfo (_("%XUnsupported PEI architecture: %s\n"), target);
147 exit (1);
148}
149
252b5132
RH
150/************************************************************************
151
152 Helper functions for qsort. Relocs must be sorted so that we can write
153 them out by pages.
154
155 ************************************************************************/
156
c6c37250
DD
157typedef struct {
158 bfd_vma vma;
159 char type;
160 short extra;
161} reloc_data_type;
162
252b5132
RH
163static int
164reloc_sort (va, vb)
165 const void *va, *vb;
166{
c6c37250
DD
167 bfd_vma a = ((reloc_data_type *) va)->vma;
168 bfd_vma b = ((reloc_data_type *) vb)->vma;
169 return (a > b) ? 1 : ((a < b) ? -1 : 0);
252b5132
RH
170}
171
172static int
173pe_export_sort (va, vb)
174 const void *va, *vb;
175{
176 def_file_export *a = (def_file_export *) va;
177 def_file_export *b = (def_file_export *) vb;
178 return strcmp (a->name, b->name);
179}
180
181/************************************************************************
182
183 Read and process the .DEF file
184
185 ************************************************************************/
186
187/* These correspond to the entries in pe_def_file->exports[]. I use
188 exported_symbol_sections[i] to tag whether or not the symbol was
5cc18311 189 defined, since we can't export symbols we don't have. */
252b5132
RH
190
191static bfd_vma *exported_symbol_offsets;
192static struct sec **exported_symbol_sections;
193
194static int export_table_size;
195static int count_exported;
196static int count_exported_byname;
197static int count_with_ordinals;
198static const char *dll_name;
199static int min_ordinal, max_ordinal;
200static int *exported_symbols;
201
202typedef struct exclude_list_struct
203 {
204 char *string;
205 struct exclude_list_struct *next;
206 }
207exclude_list_struct;
208static struct exclude_list_struct *excludes = 0;
209
210void
211pe_dll_add_excludes (new_excludes)
212 const char *new_excludes;
213{
214 char *local_copy;
215 char *exclude_string;
216
217 local_copy = xstrdup (new_excludes);
218
219 exclude_string = strtok (local_copy, ",:");
220 for (; exclude_string; exclude_string = strtok (NULL, ",:"))
221 {
222 struct exclude_list_struct *new_exclude;
223
224 new_exclude = ((struct exclude_list_struct *)
225 xmalloc (sizeof (struct exclude_list_struct)));
226 new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 1);
227 strcpy (new_exclude->string, exclude_string);
228 new_exclude->next = excludes;
229 excludes = new_exclude;
230 }
231
232 free (local_copy);
233}
234
235static int
236auto_export (d, n)
237 def_file *d;
238 const char *n;
239{
240 int i;
241 struct exclude_list_struct *ex;
242 for (i = 0; i < d->num_exports; i++)
243 if (strcmp (d->exports[i].name, n) == 0)
244 return 0;
245 if (pe_dll_do_default_excludes)
246 {
247 if (strcmp (n, "DllMain@12") == 0)
248 return 0;
249 if (strcmp (n, "DllEntryPoint@0") == 0)
250 return 0;
251 if (strcmp (n, "impure_ptr") == 0)
252 return 0;
253 }
254 for (ex = excludes; ex; ex = ex->next)
255 if (strcmp (n, ex->string) == 0)
256 return 0;
257 return 1;
258}
259
260static void
261process_def_file (abfd, info)
1069dd8d 262 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
263 struct bfd_link_info *info;
264{
265 int i, j;
266 struct bfd_link_hash_entry *blhe;
267 bfd *b;
268 struct sec *s;
269 def_file_export *e=0;
270
271 if (!pe_def_file)
272 pe_def_file = def_file_empty ();
273
274 /* First, run around to all the objects looking for the .drectve
275 sections, and push those into the def file too */
276
277 for (b = info->input_bfds; b; b = b->link_next)
278 {
279 s = bfd_get_section_by_name (b, ".drectve");
280 if (s)
281 {
282 int size = bfd_get_section_size_before_reloc (s);
283 char *buf = xmalloc (size);
284 bfd_get_section_contents (b, s, buf, 0, size);
285 def_file_add_directive (pe_def_file, buf, size);
286 free (buf);
287 }
288 }
289
290 /* Now, maybe export everything else the default way */
291
292 if (pe_dll_export_everything || pe_def_file->num_exports == 0)
293 {
294 for (b = info->input_bfds; b; b = b->link_next)
295 {
296 asymbol **symbols;
297 int nsyms, symsize;
298
299 symsize = bfd_get_symtab_upper_bound (b);
300 symbols = (asymbol **) xmalloc (symsize);
301 nsyms = bfd_canonicalize_symtab (b, symbols);
302
303 for (j = 0; j < nsyms; j++)
304 {
45b1f63c
DD
305 /* We should export symbols which are either global or not
306 anything at all (.bss data is the latter) */
307 if ((symbols[j]->flags & BSF_GLOBAL)
308 || (symbols[j]->flags == BSF_NO_FLAGS))
252b5132
RH
309 {
310 const char *sn = symbols[j]->name;
311 if (*sn == '_')
312 sn++;
313 if (auto_export (pe_def_file, sn))
314 def_file_add_export (pe_def_file, sn, 0, -1);
315 }
316 }
317 }
318 }
319
320#undef NE
321#define NE pe_def_file->num_exports
322
323 /* Canonicalize the export list */
324
325 if (pe_dll_kill_ats)
326 {
327 for (i = 0; i < NE; i++)
328 {
329 if (strchr (pe_def_file->exports[i].name, '@'))
330 {
331 /* This will preserve internal_name, which may have been pointing
332 to the same memory as name, or might not have */
333 char *tmp = xstrdup (pe_def_file->exports[i].name);
334 *(strchr (tmp, '@')) = 0;
335 pe_def_file->exports[i].name = tmp;
336 }
337 }
338 }
339
340 if (pe_dll_stdcall_aliases)
341 {
342 for (i = 0; i < NE; i++)
343 {
344 if (strchr (pe_def_file->exports[i].name, '@'))
345 {
346 char *tmp = xstrdup (pe_def_file->exports[i].name);
347 *(strchr (tmp, '@')) = 0;
348 if (auto_export (pe_def_file, tmp))
349 def_file_add_export (pe_def_file, tmp,
350 pe_def_file->exports[i].internal_name, -1);
351 else
352 free (tmp);
353 }
354 }
355 }
356
357 e = pe_def_file->exports; /* convenience, but watch out for it changing */
358
359 exported_symbol_offsets = (bfd_vma *) xmalloc (NE * sizeof (bfd_vma));
360 exported_symbol_sections = (struct sec **) xmalloc (NE * sizeof (struct sec *));
361
362 memset (exported_symbol_sections, 0, NE * sizeof (struct sec *));
363 max_ordinal = 0;
364 min_ordinal = 65536;
365 count_exported = 0;
366 count_exported_byname = 0;
367 count_with_ordinals = 0;
368
369 qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]), pe_export_sort);
370 for (i = 0, j = 0; i < NE; i++)
371 {
372 if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0)
373 {
870df5dc 374 /* This is a duplicate. */
252b5132
RH
375 if (e[j - 1].ordinal != -1
376 && e[i].ordinal != -1
377 && e[j - 1].ordinal != e[i].ordinal)
378 {
870df5dc
NC
379 if (pe_dll_warn_dup_exports)
380 /* xgettext:c-format */
486e80e2 381 einfo (_("%XError, duplicate EXPORT with ordinals: %s (%d vs %d)\n"),
870df5dc 382 e[j - 1].name, e[j - 1].ordinal, e[i].ordinal);
252b5132
RH
383 }
384 else
385 {
870df5dc
NC
386 if (pe_dll_warn_dup_exports)
387 /* xgettext:c-format */
388 einfo (_("Warning, duplicate EXPORT: %s\n"),
389 e[j - 1].name);
252b5132 390 }
486e80e2 391 if (e[i].ordinal != -1)
252b5132
RH
392 e[j - 1].ordinal = e[i].ordinal;
393 e[j - 1].flag_private |= e[i].flag_private;
394 e[j - 1].flag_constant |= e[i].flag_constant;
395 e[j - 1].flag_noname |= e[i].flag_noname;
396 e[j - 1].flag_data |= e[i].flag_data;
397 }
398 else
399 {
400 if (i != j)
401 e[j] = e[i];
402 j++;
403 }
404 }
405 pe_def_file->num_exports = j; /* == NE */
406
407 for (i = 0; i < NE; i++)
408 {
409 char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
c6c37250
DD
410 if (pe_details->underscored)
411 {
412 *name = '_';
413 strcpy (name + 1, pe_def_file->exports[i].internal_name);
414 }
415 else
416 strcpy (name, pe_def_file->exports[i].internal_name);
252b5132
RH
417
418 blhe = bfd_link_hash_lookup (info->hash,
419 name,
420 false, false, true);
421
8a5b676c
DD
422 if (blhe
423 && (blhe->type == bfd_link_hash_defined
424 || (blhe->type == bfd_link_hash_common)))
252b5132
RH
425 {
426 count_exported++;
427 if (!pe_def_file->exports[i].flag_noname)
428 count_exported_byname++;
8a5b676c
DD
429
430 /* Only fill in the sections. The actual offsets are computed
431 in fill_exported_offsets() after common symbols are laid
432 out. */
433 if (blhe->type == bfd_link_hash_defined)
434 exported_symbol_sections[i] = blhe->u.def.section;
435 else
436 exported_symbol_sections[i] = blhe->u.c.p->section;
5cc18311 437
252b5132
RH
438 if (pe_def_file->exports[i].ordinal != -1)
439 {
440 if (max_ordinal < pe_def_file->exports[i].ordinal)
441 max_ordinal = pe_def_file->exports[i].ordinal;
442 if (min_ordinal > pe_def_file->exports[i].ordinal)
443 min_ordinal = pe_def_file->exports[i].ordinal;
444 count_with_ordinals++;
445 }
446 }
447 else if (blhe && blhe->type == bfd_link_hash_undefined)
448 {
449 /* xgettext:c-format */
450 einfo (_("%XCannot export %s: symbol not defined\n"),
451 pe_def_file->exports[i].internal_name);
452 }
453 else if (blhe)
454 {
455 /* xgettext:c-format */
456 einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"),
457 pe_def_file->exports[i].internal_name,
458 blhe->type, bfd_link_hash_defined);
459 }
460 else
461 {
462 /* xgettext:c-format */
463 einfo (_("%XCannot export %s: symbol not found\n"),
464 pe_def_file->exports[i].internal_name);
465 }
466 free (name);
467 }
468}
469
470/************************************************************************
471
472 Build the bfd that will contain .edata and .reloc sections
473
474 ************************************************************************/
475
476static void
c6c37250
DD
477build_filler_bfd (include_edata)
478 int include_edata;
252b5132
RH
479{
480 lang_input_statement_type *filler_file;
481 filler_file = lang_add_input_file ("dll stuff",
482 lang_input_file_is_fake_enum,
483 NULL);
484 filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd);
485 if (filler_bfd == NULL
486 || !bfd_set_arch_mach (filler_bfd,
487 bfd_get_arch (output_bfd),
488 bfd_get_mach (output_bfd)))
489 {
490 einfo ("%X%P: can not create BFD %E\n");
491 return;
492 }
493
c6c37250 494 if (include_edata)
252b5132 495 {
c6c37250
DD
496 edata_s = bfd_make_section_old_way (filler_bfd, ".edata");
497 if (edata_s == NULL
498 || !bfd_set_section_flags (filler_bfd, edata_s,
499 (SEC_HAS_CONTENTS
500 | SEC_ALLOC
501 | SEC_LOAD
502 | SEC_KEEP
503 | SEC_IN_MEMORY)))
504 {
505 einfo ("%X%P: can not create .edata section: %E\n");
506 return;
507 }
508 bfd_set_section_size (filler_bfd, edata_s, edata_sz);
252b5132 509 }
252b5132
RH
510
511 reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc");
512 if (reloc_s == NULL
513 || !bfd_set_section_flags (filler_bfd, reloc_s,
514 (SEC_HAS_CONTENTS
515 | SEC_ALLOC
516 | SEC_LOAD
517 | SEC_KEEP
518 | SEC_IN_MEMORY)))
519 {
520 einfo ("%X%P: can not create .reloc section: %E\n");
521 return;
522 }
523 bfd_set_section_size (filler_bfd, reloc_s, 0);
524
525 ldlang_add_file (filler_file);
526}
527
528/************************************************************************
529
530 Gather all the exported symbols and build the .edata section
531
532 ************************************************************************/
533
534static void
535generate_edata (abfd, info)
536 bfd *abfd;
1069dd8d 537 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
538{
539 int i, next_ordinal;
540 int name_table_size = 0;
541 const char *dlnp;
542
543 /* First, we need to know how many exported symbols there are,
5cc18311 544 and what the range of ordinals is. */
252b5132
RH
545
546 if (pe_def_file->name)
547 {
548 dll_name = pe_def_file->name;
549 }
550 else
551 {
552 dll_name = abfd->filename;
553 for (dlnp = dll_name; *dlnp; dlnp++)
554 {
555 if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':')
556 dll_name = dlnp + 1;
557 }
558 }
559
560 if (count_with_ordinals && max_ordinal > count_exported)
561 {
562 if (min_ordinal > max_ordinal - count_exported + 1)
563 min_ordinal = max_ordinal - count_exported + 1;
564 }
565 else
566 {
567 min_ordinal = 1;
568 max_ordinal = count_exported;
569 }
570 export_table_size = max_ordinal - min_ordinal + 1;
571
572 exported_symbols = (int *) xmalloc (export_table_size * sizeof (int));
573 for (i = 0; i < export_table_size; i++)
574 exported_symbols[i] = -1;
575
576 /* Now we need to assign ordinals to those that don't have them */
577 for (i = 0; i < NE; i++)
578 {
579 if (exported_symbol_sections[i])
580 {
581 if (pe_def_file->exports[i].ordinal != -1)
582 {
583 int ei = pe_def_file->exports[i].ordinal - min_ordinal;
584 int pi = exported_symbols[ei];
585 if (pi != -1)
586 {
587 /* xgettext:c-format */
486e80e2 588 einfo (_("%XError, ordinal used twice: %d (%s vs %s)\n"),
252b5132
RH
589 pe_def_file->exports[i].ordinal,
590 pe_def_file->exports[i].name,
591 pe_def_file->exports[pi].name);
592 }
593 exported_symbols[ei] = i;
594 }
595 name_table_size += strlen (pe_def_file->exports[i].name) + 1;
596 }
597 }
598
599 next_ordinal = min_ordinal;
600 for (i = 0; i < NE; i++)
601 if (exported_symbol_sections[i])
602 if (pe_def_file->exports[i].ordinal == -1)
603 {
604 while (exported_symbols[next_ordinal - min_ordinal] != -1)
605 next_ordinal++;
606 exported_symbols[next_ordinal - min_ordinal] = i;
607 pe_def_file->exports[i].ordinal = next_ordinal;
608 }
609
610 /* OK, now we can allocate some memory */
611
612 edata_sz = (40 /* directory */
613 + 4 * export_table_size /* addresses */
614 + 4 * count_exported_byname /* name ptrs */
615 + 2 * count_exported_byname /* ordinals */
616 + name_table_size + strlen (dll_name) + 1);
617}
618
8a5b676c
DD
619/* Fill the exported symbol offsets. The preliminary work has already
620 been done in process_def_file(). */
621
622static void
623fill_exported_offsets (abfd, info)
f0c87f88 624 bfd *abfd ATTRIBUTE_UNUSED;
8a5b676c
DD
625 struct bfd_link_info *info;
626{
f0c87f88 627 int i;
8a5b676c 628 struct bfd_link_hash_entry *blhe;
5cc18311 629
8a5b676c
DD
630 for (i = 0; i < pe_def_file->num_exports; i++)
631 {
632 char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
633 if (pe_details->underscored)
634 {
635 *name = '_';
636 strcpy (name + 1, pe_def_file->exports[i].internal_name);
637 }
638 else
639 strcpy (name, pe_def_file->exports[i].internal_name);
640
641 blhe = bfd_link_hash_lookup (info->hash,
642 name,
643 false, false, true);
644
645 if (blhe && (blhe->type == bfd_link_hash_defined))
646 {
647 exported_symbol_offsets[i] = blhe->u.def.value;
648 }
649 free (name);
650 }
651}
652
252b5132
RH
653static void
654fill_edata (abfd, info)
655 bfd *abfd;
1069dd8d 656 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
657{
658 int i, hint;
659 unsigned char *edirectory;
660 unsigned long *eaddresses;
661 unsigned long *enameptrs;
662 unsigned short *eordinals;
663 unsigned char *enamestr;
664 time_t now;
665
666 time (&now);
667
668 edata_d = (unsigned char *) xmalloc (edata_sz);
669
670 /* Note use of array pointer math here */
671 edirectory = edata_d;
672 eaddresses = (unsigned long *) (edata_d + 40);
673 enameptrs = eaddresses + export_table_size;
674 eordinals = (unsigned short *) (enameptrs + count_exported_byname);
675 enamestr = (char *) (eordinals + count_exported_byname);
676
677#define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base)
678
c2a94a7a 679 memset (edata_d, 0, edata_sz);
252b5132
RH
680 bfd_put_32 (abfd, now, edata_d + 4);
681 if (pe_def_file->version_major != -1)
682 {
683 bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8);
684 bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10);
685 }
686 bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12);
687 strcpy (enamestr, dll_name);
688 enamestr += strlen (enamestr) + 1;
689 bfd_put_32 (abfd, min_ordinal, edata_d + 16);
690 bfd_put_32 (abfd, export_table_size, edata_d + 20);
691 bfd_put_32 (abfd, count_exported_byname, edata_d + 24);
692 bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28);
693 bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32);
694 bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36);
695
8a5b676c
DD
696 fill_exported_offsets (abfd, info);
697
252b5132
RH
698 /* Ok, now for the filling in part */
699 hint = 0;
700 for (i = 0; i < export_table_size; i++)
701 {
702 int s = exported_symbols[i];
703 if (s != -1)
704 {
705 struct sec *ssec = exported_symbol_sections[s];
706 unsigned long srva = (exported_symbol_offsets[s]
707 + ssec->output_section->vma
708 + ssec->output_offset);
45b1f63c 709 int ord = pe_def_file->exports[s].ordinal;
252b5132 710
45b1f63c
DD
711 bfd_put_32 (abfd, srva - image_base,
712 (void *) (eaddresses + ord - min_ordinal));
252b5132
RH
713 if (!pe_def_file->exports[s].flag_noname)
714 {
715 char *ename = pe_def_file->exports[s].name;
716 bfd_put_32 (abfd, ERVA (enamestr), (void *) enameptrs);
45b1f63c 717 enameptrs++;
252b5132
RH
718 strcpy (enamestr, ename);
719 enamestr += strlen (enamestr) + 1;
45b1f63c
DD
720 bfd_put_16 (abfd, ord - min_ordinal, (void *) eordinals);
721 eordinals++;
252b5132
RH
722 pe_def_file->exports[s].hint = hint++;
723 }
252b5132
RH
724 }
725 }
726}
727
728/************************************************************************
729
730 Gather all the relocations and build the .reloc section
731
732 ************************************************************************/
733
734static void
735generate_reloc (abfd, info)
736 bfd *abfd;
737 struct bfd_link_info *info;
738{
739
740 /* for .reloc stuff */
c6c37250 741 reloc_data_type *reloc_data;
252b5132
RH
742 int total_relocs = 0;
743 int i;
744 unsigned long sec_page = (unsigned long) (-1);
745 unsigned long page_ptr, page_count;
746 int bi;
747 bfd *b;
748 struct sec *s;
749
750 total_relocs = 0;
751 for (b = info->input_bfds; b; b = b->link_next)
752 for (s = b->sections; s; s = s->next)
753 total_relocs += s->reloc_count;
754
c6c37250 755 reloc_data = (reloc_data_type *) xmalloc (total_relocs * sizeof (reloc_data_type));
252b5132
RH
756
757 total_relocs = 0;
758 bi = 0;
759 for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next)
760 {
761 arelent **relocs;
762 int relsize, nrelocs, i;
763
764 for (s = b->sections; s; s = s->next)
765 {
766 unsigned long sec_vma = s->output_section->vma + s->output_offset;
767 asymbol **symbols;
768 int nsyms, symsize;
769
770 /* if it's not loaded, we don't need to relocate it this way */
771 if (!(s->output_section->flags & SEC_LOAD))
772 continue;
773
774 /* I don't know why there would be a reloc for these, but I've
775 seen it happen - DJ */
776 if (s->output_section == &bfd_abs_section)
777 continue;
778
779 if (s->output_section->vma == 0)
780 {
781 /* Huh? Shouldn't happen, but punt if it does */
782 einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n",
783 s->output_section->name, s->output_section->index,
784 s->output_section->flags);
785 continue;
786 }
787
788 symsize = bfd_get_symtab_upper_bound (b);
789 symbols = (asymbol **) xmalloc (symsize);
790 nsyms = bfd_canonicalize_symtab (b, symbols);
791
792 relsize = bfd_get_reloc_upper_bound (b, s);
793 relocs = (arelent **) xmalloc ((size_t) relsize);
794 nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
795
796 for (i = 0; i < nrelocs; i++)
797 {
798 if (!relocs[i]->howto->pc_relative
c6c37250 799 && relocs[i]->howto->type != pe_details->imagebase_reloc)
252b5132 800 {
c6c37250
DD
801 bfd_vma sym_vma;
802 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
803 sym_vma = (relocs[i]->addend
804 + sym->value
805 + sym->section->vma
806 + sym->section->output_offset
807 + sym->section->output_section->vma);
808 reloc_data[total_relocs].vma = sec_vma + relocs[i]->address;
5cc18311 809
344a211f 810#define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift)
5cc18311 811
344a211f
NC
812 switch BITS_AND_SHIFT (relocs[i]->howto->bitsize,
813 relocs[i]->howto->rightshift)
252b5132 814 {
344a211f 815 case BITS_AND_SHIFT (32, 0):
c6c37250
DD
816 reloc_data[total_relocs].type = 3;
817 total_relocs++;
252b5132 818 break;
344a211f
NC
819 case BITS_AND_SHIFT (16, 0):
820 reloc_data[total_relocs].type = 2;
821 total_relocs++;
822 break;
823 case BITS_AND_SHIFT (16, 16):
824 reloc_data[total_relocs].type = 4;
825 /* FIXME: we can't know the symbol's right value yet,
826 but we probably can safely assume that CE will relocate
827 us in 64k blocks, so leaving it zero is safe. */
828 reloc_data[total_relocs].extra = 0;
829 total_relocs++;
830 break;
831 case BITS_AND_SHIFT (26, 2):
832 reloc_data[total_relocs].type = 5;
833 total_relocs++;
834 break;
252b5132
RH
835 default:
836 /* xgettext:c-format */
837 einfo (_("%XError: %d-bit reloc in dll\n"),
838 relocs[i]->howto->bitsize);
839 break;
840 }
841 }
842 }
843 free (relocs);
844 /* Warning: the allocated symbols are remembered in BFD and reused
845 later, so don't free them! */
846 /* free (symbols); */
847 }
848 }
849
850 /* At this point, we have total_relocs relocation addresses in
851 reloc_addresses, which are all suitable for the .reloc section.
5cc18311 852 We must now create the new sections. */
252b5132 853
c6c37250 854 qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort);
252b5132
RH
855
856 for (i = 0; i < total_relocs; i++)
857 {
c6c37250 858 unsigned long this_page = (reloc_data[i].vma >> 12);
5cc18311 859
252b5132
RH
860 if (this_page != sec_page)
861 {
862 reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */
863 reloc_sz += 8;
864 sec_page = this_page;
865 }
5cc18311 866
252b5132 867 reloc_sz += 2;
5cc18311 868
344a211f
NC
869 if (reloc_data[i].type == 4)
870 reloc_sz += 2;
252b5132
RH
871 }
872 reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */
873
874 reloc_d = (unsigned char *) xmalloc (reloc_sz);
875
876 sec_page = (unsigned long) (-1);
877 reloc_sz = 0;
878 page_ptr = (unsigned long) (-1);
879 page_count = 0;
880 for (i = 0; i < total_relocs; i++)
881 {
c6c37250 882 unsigned long rva = reloc_data[i].vma - image_base;
252b5132
RH
883 unsigned long this_page = (rva & ~0xfff);
884 if (this_page != sec_page)
885 {
886 while (reloc_sz & 3)
887 reloc_d[reloc_sz++] = 0;
888 if (page_ptr != (unsigned long) (-1))
889 bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
890 bfd_put_32 (abfd, this_page, reloc_d + reloc_sz);
891 page_ptr = reloc_sz;
892 reloc_sz += 8;
893 sec_page = this_page;
894 page_count = 0;
895 }
c6c37250
DD
896 bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type<<12),
897 reloc_d + reloc_sz);
252b5132 898 reloc_sz += 2;
c6c37250
DD
899 if (reloc_data[i].type == 4)
900 {
901 bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz);
902 reloc_sz += 2;
903 }
252b5132
RH
904 page_count++;
905 }
906 while (reloc_sz & 3)
907 reloc_d[reloc_sz++] = 0;
908 if (page_ptr != (unsigned long) (-1))
909 bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
910 while (reloc_sz < reloc_s->_raw_size)
911 reloc_d[reloc_sz++] = 0;
912}
913
914/************************************************************************
915
916 Given the exiting def_file structure, print out a .DEF file that
917 corresponds to it.
918
919 ************************************************************************/
920
921static void
922quoteput (s, f, needs_quotes)
923 char *s;
924 FILE * f;
925 int needs_quotes;
926{
927 char *cp;
928 for (cp = s; *cp; cp++)
929 if (*cp == '\''
930 || *cp == '"'
931 || *cp == '\\'
932 || isspace ((unsigned char) *cp)
933 || *cp == ','
934 || *cp == ';')
935 needs_quotes = 1;
936 if (needs_quotes)
937 {
938 putc ('"', f);
939 while (*s)
940 {
941 if (*s == '"' || *s == '\\')
942 putc ('\\', f);
943 putc (*s, f);
944 s++;
945 }
946 putc ('"', f);
947 }
948 else
949 fputs (s, f);
950}
951
952void
953pe_dll_generate_def_file (pe_out_def_filename)
1069dd8d 954 const char *pe_out_def_filename;
252b5132
RH
955{
956 int i;
957 FILE *out = fopen (pe_out_def_filename, "w");
958 if (out == NULL)
959 {
960 /* xgettext:c-format */
961 einfo (_("%s: Can't open output def file %s\n"),
962 program_name, pe_out_def_filename);
963 }
964
965 if (pe_def_file)
966 {
967 if (pe_def_file->name)
968 {
969 if (pe_def_file->is_dll)
970 fprintf (out, "LIBRARY ");
971 else
972 fprintf (out, "NAME ");
973 quoteput (pe_def_file->name, out, 1);
974 if (pe_data (output_bfd)->pe_opthdr.ImageBase)
975 fprintf (out, " BASE=0x%lx",
976 (unsigned long) pe_data (output_bfd)->pe_opthdr.ImageBase);
977 fprintf (out, "\n");
978 }
979
980 if (pe_def_file->description)
981 {
982 fprintf (out, "DESCRIPTION ");
983 quoteput (pe_def_file->description, out, 1);
984 fprintf (out, "\n");
985 }
986
987 if (pe_def_file->version_minor != -1)
988 fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major,
989 pe_def_file->version_minor);
990 else if (pe_def_file->version_major != -1)
991 fprintf (out, "VERSION %d\n", pe_def_file->version_major);
992
993 if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1)
994 fprintf (out, "\n");
995
996 if (pe_def_file->stack_commit != -1)
997 fprintf (out, "STACKSIZE 0x%x,0x%x\n",
998 pe_def_file->stack_reserve, pe_def_file->stack_commit);
999 else if (pe_def_file->stack_reserve != -1)
1000 fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve);
1001 if (pe_def_file->heap_commit != -1)
1002 fprintf (out, "HEAPSIZE 0x%x,0x%x\n",
1003 pe_def_file->heap_reserve, pe_def_file->heap_commit);
1004 else if (pe_def_file->heap_reserve != -1)
1005 fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve);
1006
1007 if (pe_def_file->num_section_defs > 0)
1008 {
1009 fprintf (out, "\nSECTIONS\n\n");
1010 for (i = 0; i < pe_def_file->num_section_defs; i++)
1011 {
1012 fprintf (out, " ");
1013 quoteput (pe_def_file->section_defs[i].name, out, 0);
1014 if (pe_def_file->section_defs[i].class)
1015 {
1016 fprintf (out, " CLASS ");
1017 quoteput (pe_def_file->section_defs[i].class, out, 0);
1018 }
1019 if (pe_def_file->section_defs[i].flag_read)
1020 fprintf (out, " READ");
1021 if (pe_def_file->section_defs[i].flag_write)
1022 fprintf (out, " WRITE");
1023 if (pe_def_file->section_defs[i].flag_execute)
1024 fprintf (out, " EXECUTE");
1025 if (pe_def_file->section_defs[i].flag_shared)
1026 fprintf (out, " SHARED");
1027 fprintf (out, "\n");
1028 }
1029 }
1030
1031 if (pe_def_file->num_exports > 0)
1032 {
1033 fprintf (out, "\nEXPORTS\n\n");
1034 for (i = 0; i < pe_def_file->num_exports; i++)
1035 {
1036 def_file_export *e = pe_def_file->exports + i;
1037 fprintf (out, " ");
1038 quoteput (e->name, out, 0);
1039 if (e->internal_name && strcmp (e->internal_name, e->name))
1040 {
1041 fprintf (out, " = ");
1042 quoteput (e->internal_name, out, 0);
1043 }
1044 if (e->ordinal != -1)
1045 fprintf (out, " @%d", e->ordinal);
1046 if (e->flag_private)
1047 fprintf (out, " PRIVATE");
1048 if (e->flag_constant)
1049 fprintf (out, " CONSTANT");
1050 if (e->flag_noname)
1051 fprintf (out, " NONAME");
1052 if (e->flag_data)
1053 fprintf (out, " DATA");
1054
1055 fprintf (out, "\n");
1056 }
1057 }
1058
1059 if (pe_def_file->num_imports > 0)
1060 {
1061 fprintf (out, "\nIMPORTS\n\n");
1062 for (i = 0; i < pe_def_file->num_imports; i++)
1063 {
1064 def_file_import *im = pe_def_file->imports + i;
1065 fprintf (out, " ");
1066 if (im->internal_name
1067 && (!im->name || strcmp (im->internal_name, im->name)))
1068 {
1069 quoteput (im->internal_name, out, 0);
1070 fprintf (out, " = ");
1071 }
1072 quoteput (im->module->name, out, 0);
1073 fprintf (out, ".");
1074 if (im->name)
1075 quoteput (im->name, out, 0);
1076 else
1077 fprintf (out, "%d", im->ordinal);
1078 fprintf (out, "\n");
1079 }
1080 }
1081 }
1082 else
1083 fprintf (out, _("; no contents available\n"));
1084
1085 if (fclose (out) == EOF)
1086 {
1087 /* xgettext:c-format */
1088 einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename);
1089 }
1090}
1091
1092/************************************************************************
1093
1094 Generate the import library
1095
1096 ************************************************************************/
1097
1098static asymbol **symtab;
1099static int symptr;
1100static int tmp_seq;
1101static const char *dll_filename;
1102static char *dll_symname;
1103
1104#define UNDSEC (asection *) &bfd_und_section
1105
1106static asection *
1107quick_section(abfd, name, flags, align)
1108 bfd *abfd;
1109 const char *name;
1110 int flags;
1111 int align;
1112{
1113 asection *sec;
1114 asymbol *sym;
1115
1116 sec = bfd_make_section_old_way (abfd, name);
1117 bfd_set_section_flags (abfd, sec, flags
1118 | SEC_ALLOC
1119 | SEC_LOAD
1120 | SEC_KEEP
1121 );
1122 bfd_set_section_alignment (abfd, sec, align);
1123 /* remember to undo this before trying to link internally! */
1124 sec->output_section = sec;
1125
1126 sym = bfd_make_empty_symbol (abfd);
1127 symtab[symptr++] = sym;
1128 sym->name = sec->name;
1129 sym->section = sec;
1130 sym->flags = BSF_LOCAL;
1131 sym->value = 0;
1132
1133 return sec;
1134}
1135
1136static void
1137quick_symbol (abfd, n1, n2, n3, sec, flags, addr)
1138 bfd *abfd;
1139 char *n1;
1140 char *n2;
1141 char *n3;
1142 asection *sec;
1143 int flags;
1144 int addr;
1145{
1146 asymbol *sym;
1147 char *name = (char *) xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
1148 strcpy (name, n1);
1149 strcat (name, n2);
1150 strcat (name, n3);
1151 sym = bfd_make_empty_symbol (abfd);
1152 sym->name = name;
1153 sym->section = sec;
1154 sym->flags = flags;
1155 sym->value = addr;
1156 symtab[symptr++] = sym;
1157}
1158
1159static arelent *reltab = 0;
1160static int relcount = 0, relsize = 0;
1161
1162static void
1163quick_reloc (abfd, address, which_howto, symidx)
1164 bfd *abfd;
1165 int address;
1166 int which_howto;
1167 int symidx;
1168{
1169 if (relcount >= (relsize-1))
1170 {
1171 relsize += 10;
1172 if (reltab)
1173 reltab = (arelent *) xrealloc (reltab, relsize * sizeof (arelent));
1174 else
1175 reltab = (arelent *) xmalloc (relsize * sizeof (arelent));
1176 }
1177 reltab[relcount].address = address;
1178 reltab[relcount].addend = 0;
1179 reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
1180 reltab[relcount].sym_ptr_ptr = symtab + symidx;
1181 relcount++;
1182}
1183
1184static void
1185save_relocs (asection *sec)
1186{
1187 int i;
1188 sec->relocation = reltab;
1189 sec->reloc_count = relcount;
1190 sec->orelocation = (arelent **) xmalloc ((relcount+1) * sizeof (arelent *));
1191 for (i=0; i<relcount; i++)
1192 sec->orelocation[i] = sec->relocation + i;
1193 sec->orelocation[relcount] = 0;
1194 sec->flags |= SEC_RELOC;
1195 reltab = 0;
1196 relcount = relsize = 0;
1197}
1198
1199/*
1200 * .section .idata$2
1201 * .global __head_my_dll
1202 * __head_my_dll:
1203 * .rva hname
1204 * .long 0
1205 * .long 0
1206 * .rva __my_dll_iname
1207 * .rva fthunk
1208 *
1209 * .section .idata$5
1210 * .long 0
1211 * fthunk:
1212 *
1213 * .section .idata$4
1214 * .long 0
1215 * hname:
1216 */
1217
1218static bfd *
1219make_head (parent)
1220 bfd *parent;
1221{
1222 asection *id2, *id5, *id4;
1223 unsigned char *d2, *d5, *d4;
1224 char *oname;
1225 bfd *abfd;
1226
1227 oname = (char *) xmalloc (20);
1228 sprintf (oname, "d%06d.o", tmp_seq);
1229 tmp_seq++;
1230
1231 abfd = bfd_create (oname, parent);
c6c37250 1232 bfd_find_target (pe_details->object_target, abfd);
252b5132
RH
1233 bfd_make_writable (abfd);
1234
1235 bfd_set_format (abfd, bfd_object);
c6c37250 1236 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
252b5132
RH
1237
1238 symptr = 0;
1239 symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *));
1240 id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
1241 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1242 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
c6c37250
DD
1243 quick_symbol (abfd, U("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
1244 quick_symbol (abfd, U(""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
1245
1246 /* OK, pay attention here. I got confused myself looking back at
1247 it. We create a four-byte section to mark the beginning of the
1248 list, and we include an offset of 4 in the section, so that the
1249 pointer to the list points to the *end* of this section, which is
5cc18311 1250 the start of the list of sections from other objects. */
252b5132
RH
1251
1252 bfd_set_section_size (abfd, id2, 20);
1253 d2 = (unsigned char *) xmalloc (20);
1254 id2->contents = d2;
1255 memset (d2, 0, 20);
1256 d2[0] = d2[16] = 4; /* reloc addend */
1257 quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
1258 quick_reloc (abfd, 12, BFD_RELOC_RVA, 4);
1259 quick_reloc (abfd, 16, BFD_RELOC_RVA, 1);
1260 save_relocs (id2);
1261
1262 bfd_set_section_size (abfd, id5, 4);
1263 d5 = (unsigned char *) xmalloc (4);
1264 id5->contents = d5;
1265 memset (d5, 0, 4);
1266
1267 bfd_set_section_size (abfd, id4, 4);
1268 d4 = (unsigned char *) xmalloc (4);
1269 id4->contents = d4;
1270 memset (d4, 0, 4);
1271
1272 bfd_set_symtab (abfd, symtab, symptr);
1273
1274 bfd_set_section_contents (abfd, id2, d2, 0, 20);
1275 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1276 bfd_set_section_contents (abfd, id4, d4, 0, 4);
5cc18311 1277
252b5132
RH
1278 bfd_make_readable (abfd);
1279 return abfd;
1280}
1281
1282/*
1283 * .section .idata$4
1284 * .long 0
1285 * .section .idata$5
1286 * .long 0
1287 * .section idata$7
1288 * .global __my_dll_iname
1289 *__my_dll_iname:
1290 * .asciz "my.dll"
1291 */
1292
1293static bfd *
1294make_tail (parent)
1295 bfd *parent;
1296{
1297 asection *id4, *id5, *id7;
1298 unsigned char *d4, *d5, *d7;
1299 int len;
1300 char *oname;
1301 bfd *abfd;
1302
1303 oname = (char *) xmalloc (20);
1304 sprintf (oname, "d%06d.o", tmp_seq);
1305 tmp_seq++;
1306
1307 abfd = bfd_create (oname, parent);
c6c37250 1308 bfd_find_target (pe_details->object_target, abfd);
252b5132
RH
1309 bfd_make_writable (abfd);
1310
1311 bfd_set_format (abfd, bfd_object);
c6c37250 1312 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
252b5132
RH
1313
1314 symptr = 0;
1315 symtab = (asymbol **) xmalloc (5 * sizeof (asymbol *));
1316 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1317 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1318 id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
c6c37250 1319 quick_symbol (abfd, U(""), dll_symname, "_iname", id7, BSF_GLOBAL, 0);
252b5132
RH
1320
1321 bfd_set_section_size (abfd, id4, 4);
1322 d4 = (unsigned char *) xmalloc (4);
1323 id4->contents = d4;
1324 memset (d4, 0, 4);
1325
1326 bfd_set_section_size (abfd, id5, 4);
1327 d5 = (unsigned char *) xmalloc (4);
1328 id5->contents = d5;
1329 memset (d5, 0, 4);
1330
1331 len = strlen (dll_filename)+1;
1332 if (len & 1)
1333 len ++;
1334 bfd_set_section_size (abfd, id7, len);
1335 d7 = (unsigned char *) xmalloc (len);
1336 id7->contents = d7;
1337 strcpy (d7, dll_filename);
1338
1339 bfd_set_symtab (abfd, symtab, symptr);
1340
1341 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1342 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1343 bfd_set_section_contents (abfd, id7, d7, 0, len);
1344
1345 bfd_make_readable (abfd);
1346 return abfd;
1347}
1348
1349/*
1350 * .text
1351 * .global _function
1352 * .global ___imp_function
1353 * .global __imp__function
1354 *_function:
1355 * jmp *__imp__function:
1356 *
1357 * .section idata$7
1358 * .long __head_my_dll
1359 *
1360 * .section .idata$5
1361 *___imp_function:
1362 *__imp__function:
1363 *iat?
1364 * .section .idata$4
1365 *iat?
1366 * .section .idata$6
1367 *ID<ordinal>:
1368 * .short <hint>
1369 * .asciz "function" xlate? (add underscore, kill at)
1370 */
1371
1372static unsigned char jmp_ix86_bytes[] = {
1373 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
1374};
1375
344a211f
NC
1376/*
1377 *_function:
1378 * mov.l ip+8,r0
1379 * mov.l @r0,r0
1380 * jmp @r0
1381 * nop
1382 * .dw __imp_function
1383 */
1384
1385static unsigned char jmp_sh_bytes[] = {
1386 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
1387};
1388
1389/*
1390 *_function:
1391 * lui $t0,<high:__imp_function>
1392 * lw $t0,<low:__imp_function>
1393 * jr $t0
1394 * nop
1395 */
1396
1397static unsigned char jmp_mips_bytes[] = {
1398 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
1399 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
1400};
252b5132
RH
1401
1402static bfd *
1403make_one (exp, parent)
1404 def_file_export *exp;
1405 bfd *parent;
1406{
1407 asection *tx, *id7, *id5, *id4, *id6;
f0c87f88 1408 unsigned char *td, *d7, *d5, *d4, *d6 = NULL;
252b5132
RH
1409 int len;
1410 char *oname;
1411 bfd *abfd;
f0c87f88
NC
1412 unsigned char *jmp_bytes = NULL;
1413 int jmp_byte_count = 0;
c6c37250
DD
1414
1415 switch (pe_details->pe_arch)
1416 {
1417 case PE_ARCH_i386:
1418 jmp_bytes = jmp_ix86_bytes;
1419 jmp_byte_count = sizeof (jmp_ix86_bytes);
1420 break;
344a211f
NC
1421 case PE_ARCH_sh:
1422 jmp_bytes = jmp_sh_bytes;
1423 jmp_byte_count = sizeof (jmp_sh_bytes);
1424 break;
1425 case PE_ARCH_mips:
1426 jmp_bytes = jmp_mips_bytes;
1427 jmp_byte_count = sizeof (jmp_mips_bytes);
1428 break;
c6c37250 1429 }
252b5132
RH
1430
1431 oname = (char *) xmalloc (20);
1432 sprintf (oname, "d%06d.o", tmp_seq);
1433 tmp_seq++;
1434
1435 abfd = bfd_create (oname, parent);
c6c37250 1436 bfd_find_target (pe_details->object_target, abfd);
252b5132
RH
1437 bfd_make_writable (abfd);
1438
1439 bfd_set_format (abfd, bfd_object);
c6c37250 1440 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
252b5132
RH
1441
1442 symptr = 0;
1443 symtab = (asymbol **) xmalloc (10 * sizeof (asymbol *));
1444 tx = quick_section (abfd, ".text", SEC_CODE|SEC_HAS_CONTENTS, 2);
1445 id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1446 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1447 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1448 id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
7c9e78f8
DD
1449 if (! exp->flag_data)
1450 quick_symbol (abfd, U(""), exp->internal_name, "", tx, BSF_GLOBAL, 0);
c6c37250 1451 quick_symbol (abfd, U("_head_"), dll_symname, "", UNDSEC, BSF_GLOBAL, 0);
5b4cf3f4 1452 quick_symbol (abfd, U("_imp__"), exp->internal_name, "", id5, BSF_GLOBAL, 0);
870df5dc 1453 if (pe_dll_compat_implib)
5cc18311 1454 quick_symbol (abfd, U("__imp_"), exp->internal_name, "",
870df5dc 1455 id5, BSF_GLOBAL, 0);
252b5132 1456
c6c37250
DD
1457 bfd_set_section_size (abfd, tx, jmp_byte_count);
1458 td = (unsigned char *) xmalloc (jmp_byte_count);
252b5132 1459 tx->contents = td;
c6c37250
DD
1460 memcpy (td, jmp_bytes, jmp_byte_count);
1461 switch (pe_details->pe_arch)
1462 {
1463 case PE_ARCH_i386:
1464 quick_reloc (abfd, 2, BFD_RELOC_32, 2);
1465 break;
344a211f
NC
1466 case PE_ARCH_sh:
1467 quick_reloc (abfd, 8, BFD_RELOC_32, 2);
1468 break;
1469 case PE_ARCH_mips:
1470 quick_reloc (abfd, 0, BFD_RELOC_HI16_S, 2);
1471 quick_reloc (abfd, 0, BFD_RELOC_LO16, 0); /* MIPS_R_PAIR */
1472 quick_reloc (abfd, 4, BFD_RELOC_LO16, 2);
1473 break;
c6c37250 1474 }
252b5132
RH
1475 save_relocs (tx);
1476
1477 bfd_set_section_size (abfd, id7, 4);
1478 d7 = (unsigned char *) xmalloc (4);
1479 id7->contents = d7;
1480 memset (d7, 0, 4);
1481 quick_reloc (abfd, 0, BFD_RELOC_RVA, 6);
1482 save_relocs (id7);
1483
1484 bfd_set_section_size (abfd, id5, 4);
1485 d5 = (unsigned char *) xmalloc (4);
1486 id5->contents = d5;
1487 memset (d5, 0, 4);
1488 if (exp->flag_noname)
1489 {
1490 d5[0] = exp->ordinal;
1491 d5[1] = exp->ordinal >> 8;
1492 d5[3] = 0x80;
1493 }
1494 else
1495 {
1496 quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1497 save_relocs (id5);
1498 }
1499
1500 bfd_set_section_size (abfd, id4, 4);
1501 d4 = (unsigned char *) xmalloc (4);
1502 id4->contents = d4;
1503 memset (d4, 0, 4);
1504 if (exp->flag_noname)
1505 {
c2a94a7a
DD
1506 d4[0] = exp->ordinal;
1507 d4[1] = exp->ordinal >> 8;
1508 d4[3] = 0x80;
252b5132
RH
1509 }
1510 else
1511 {
1512 quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1513 save_relocs (id4);
1514 }
1515
1516 if (exp->flag_noname)
1517 {
1518 len = 0;
1519 bfd_set_section_size (abfd, id6, 0);
1520 }
1521 else
1522 {
1523 len = strlen (exp->name) + 3;
1524 if (len & 1)
1525 len++;
1526 bfd_set_section_size (abfd, id6, len);
1527 d6 = (unsigned char *) xmalloc (len);
1528 id6->contents = d6;
1529 memset (d6, 0, len);
1530 d6[0] = exp->hint & 0xff;
1531 d6[1] = exp->hint >> 8;
1532 strcpy (d6+2, exp->name);
1533 }
1534
1535 bfd_set_symtab (abfd, symtab, symptr);
1536
c6c37250 1537 bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
252b5132
RH
1538 bfd_set_section_contents (abfd, id7, d7, 0, 4);
1539 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1540 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1541 if (!exp->flag_noname)
1542 bfd_set_section_contents (abfd, id6, d6, 0, len);
1543
1544 bfd_make_readable (abfd);
1545 return abfd;
1546}
1547
1548void
1549pe_dll_generate_implib (def, impfilename)
1550 def_file *def;
1069dd8d 1551 const char *impfilename;
252b5132
RH
1552{
1553 int i;
1554 bfd *ar_head;
1555 bfd *ar_tail;
1556 bfd *outarch;
1557 bfd *head = 0;
1558
5aaace27 1559 dll_filename = (def->name) ? def->name : dll_name;
252b5132
RH
1560 dll_symname = xstrdup (dll_filename);
1561 for (i=0; dll_symname[i]; i++)
1562 if (!isalnum ((unsigned char) dll_symname[i]))
1563 dll_symname[i] = '_';
1564
1565 unlink (impfilename);
1566
1567 outarch = bfd_openw (impfilename, 0);
1568
1569 if (!outarch)
1570 {
1571 /* xgettext:c-format */
1572 einfo (_("%XCan't open .lib file: %s\n"), impfilename);
1573 return;
1574 }
1575
1576 /* xgettext:c-format */
1577 einfo (_("Creating library file: %s\n"), impfilename);
5cc18311 1578
252b5132
RH
1579 bfd_set_format (outarch, bfd_archive);
1580 outarch->has_armap = 1;
1581
5cc18311 1582 /* Work out a reasonable size of things to put onto one line. */
252b5132
RH
1583
1584 ar_head = make_head (outarch);
252b5132
RH
1585
1586 for (i = 0; i<def->num_exports; i++)
1587 {
1588 /* The import library doesn't know about the internal name */
1589 char *internal = def->exports[i].internal_name;
1590 bfd *n;
1591 def->exports[i].internal_name = def->exports[i].name;
1592 n = make_one (def->exports+i, outarch);
1593 n->next = head;
1594 head = n;
1595 def->exports[i].internal_name = internal;
1596 }
1597
c6c37250
DD
1598 ar_tail = make_tail (outarch);
1599
1600 if (ar_head == NULL || ar_tail == NULL)
1601 return;
1602
252b5132
RH
1603 /* Now stick them all into the archive */
1604
1605 ar_head->next = head;
1606 ar_tail->next = ar_head;
1607 head = ar_tail;
1608
1609 if (! bfd_set_archive_head (outarch, head))
1610 einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ()));
5cc18311 1611
252b5132
RH
1612 if (! bfd_close (outarch))
1613 einfo ("%Xbfd_close %s: %s\n", impfilename, bfd_errmsg (bfd_get_error ()));
1614
1615 while (head != NULL)
1616 {
1617 bfd *n = head->next;
1618 bfd_close (head);
1619 head = n;
1620 }
1621}
1622
1623static void
1624add_bfd_to_link (abfd, name, link_info)
1625 bfd *abfd;
1626 char *name;
1627 struct bfd_link_info *link_info;
1628{
1629 lang_input_statement_type *fake_file;
1630 fake_file = lang_add_input_file (name,
1631 lang_input_file_is_fake_enum,
1632 NULL);
1633 fake_file->the_bfd = abfd;
1634 ldlang_add_file (fake_file);
1635 if (!bfd_link_add_symbols (abfd, link_info))
1636 einfo ("%Xaddsym %s: %s\n", name, bfd_errmsg (bfd_get_error ()));
1637}
1638
1639void
1640pe_process_import_defs (output_bfd, link_info)
1641 bfd *output_bfd;
1642 struct bfd_link_info *link_info;
1643{
1644 def_file_module *module;
c6c37250 1645 pe_dll_id_target(bfd_get_target (output_bfd));
252b5132
RH
1646
1647 if (!pe_def_file)
1648 return;
1649
1650 for (module = pe_def_file->modules; module; module = module->next)
1651 {
1652 int i, do_this_dll;
1653
1654 dll_filename = module->name;
1655 dll_symname = xstrdup (module->name);
1656 for (i=0; dll_symname[i]; i++)
1657 if (!isalnum (dll_symname[i]))
1658 dll_symname[i] = '_';
1659
1660 do_this_dll = 0;
1661
1662 for (i=0; i<pe_def_file->num_imports; i++)
1663 if (pe_def_file->imports[i].module == module)
1664 {
1665 def_file_export exp;
1666 struct bfd_link_hash_entry *blhe;
1667
1668 /* see if we need this import */
874c8c99 1669 char *name = (char *) xmalloc (strlen (pe_def_file->imports[i].internal_name) + 2 + 6);
c6c37250 1670 sprintf (name, "%s%s", U(""), pe_def_file->imports[i].internal_name);
252b5132
RH
1671 blhe = bfd_link_hash_lookup (link_info->hash, name,
1672 false, false, false);
874c8c99
DD
1673 if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
1674 {
1675 sprintf (name, "%s%s", U("_imp__"),
1676 pe_def_file->imports[i].internal_name);
1677 blhe = bfd_link_hash_lookup (link_info->hash, name,
1678 false, false, false);
1679 }
252b5132
RH
1680 free (name);
1681 if (blhe && blhe->type == bfd_link_hash_undefined)
1682 {
1683 bfd *one;
1684 /* we do */
1685 if (!do_this_dll)
1686 {
1687 bfd *ar_head = make_head (output_bfd);
1688 add_bfd_to_link (ar_head, ar_head->filename, link_info);
1689 do_this_dll = 1;
1690 }
1691 exp.internal_name = pe_def_file->imports[i].internal_name;
1692 exp.name = pe_def_file->imports[i].name;
1693 exp.ordinal = pe_def_file->imports[i].ordinal;
1694 exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
1695 exp.flag_private = 0;
1696 exp.flag_constant = 0;
1697 exp.flag_data = 0;
1698 exp.flag_noname = exp.name ? 0 : 1;
1699 one = make_one (&exp, output_bfd);
1700 add_bfd_to_link (one, one->filename, link_info);
1701 }
1702 }
1703 if (do_this_dll)
1704 {
1705 bfd *ar_tail = make_tail (output_bfd);
1706 add_bfd_to_link (ar_tail, ar_tail->filename, link_info);
1707 }
1708
1709 free (dll_symname);
1710 }
1711}
1712
1713/************************************************************************
1714
1715 We were handed a *.DLL file. Parse it and turn it into a set of
1716 IMPORTS directives in the def file. Return true if the file was
1717 handled, false if not.
1718
1719 ************************************************************************/
1720
1721static unsigned int
1722pe_get16 (abfd, where)
1723 bfd *abfd;
1724 int where;
1725{
1726 unsigned char b[2];
1727 bfd_seek (abfd, where, SEEK_SET);
1728 bfd_read (b, 1, 2, abfd);
1729 return b[0] + (b[1]<<8);
1730}
1731
1732static unsigned int
1733pe_get32 (abfd, where)
1734 bfd *abfd;
1735 int where;
1736{
1737 unsigned char b[4];
1738 bfd_seek (abfd, where, SEEK_SET);
1739 bfd_read (b, 1, 4, abfd);
1740 return b[0] + (b[1]<<8) + (b[2]<<16) + (b[3]<<24);
1741}
1742
1743#if 0 /* This is not currently used. */
1744
1745static unsigned int
1746pe_as16 (ptr)
1747 void *ptr;
1748{
1749 unsigned char *b = ptr;
1750 return b[0] + (b[1]<<8);
1751}
1752
1753#endif
1754
1755static unsigned int
1756pe_as32 (ptr)
1757 void *ptr;
1758{
1759 unsigned char *b = ptr;
1760 return b[0] + (b[1]<<8) + (b[2]<<16) + (b[3]<<24);
1761}
1762
1763boolean
1764pe_implied_import_dll (filename)
1069dd8d 1765 const char *filename;
252b5132
RH
1766{
1767 bfd *dll;
1768 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
1769 unsigned long export_rva, export_size, nsections, secptr, expptr;
1770 unsigned char *expdata, *erva;
1771 unsigned long name_rvas, ordinals, nexp, ordbase;
1069dd8d 1772 const char *dll_name;
252b5132
RH
1773
1774 /* No, I can't use bfd here. kernel32.dll puts its export table in
5cc18311 1775 the middle of the .rdata section. */
252b5132 1776
c6c37250 1777 dll = bfd_openr (filename, pe_details->target_name);
252b5132
RH
1778 if (!dll)
1779 {
1780 einfo ("%Xopen %s: %s\n", filename, bfd_errmsg (bfd_get_error ()));
1781 return false;
1782 }
1783 /* PEI dlls seem to be bfd_objects */
1784 if (!bfd_check_format (dll, bfd_object))
1785 {
1786 einfo ("%X%s: this doesn't appear to be a DLL\n", filename);
1787 return false;
1788 }
1789
1790 dll_name = filename;
1791 for (i=0; filename[i]; i++)
1792 if (filename[i] == '/' || filename[i] == '\\' || filename[i] == ':')
1793 dll_name = filename + i + 1;
1794
1795 pe_header_offset = pe_get32 (dll, 0x3c);
1796 opthdr_ofs = pe_header_offset + 4 + 20;
1797 num_entries = pe_get32 (dll, opthdr_ofs + 92);
1798 if (num_entries < 1) /* no exports */
1799 return false;
1800 export_rva = pe_get32 (dll, opthdr_ofs + 96);
1801 export_size = pe_get32 (dll, opthdr_ofs + 100);
1802 nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
1803 secptr = (pe_header_offset + 4 + 20 +
1804 pe_get16 (dll, pe_header_offset + 4 + 16));
1805 expptr = 0;
1806 for (i=0; i<nsections; i++)
1807 {
1808 char sname[8];
1809 unsigned long secptr1 = secptr + 40 * i;
1810 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
1811 unsigned long vsize = pe_get32 (dll, secptr1 + 16);
1812 unsigned long fptr = pe_get32 (dll, secptr1 + 20);
1813 bfd_seek(dll, secptr1, SEEK_SET);
1814 bfd_read(sname, 1, 8, dll);
1815 if (vaddr <= export_rva && vaddr+vsize > export_rva)
1816 {
1817 expptr = fptr + (export_rva - vaddr);
1818 if (export_rva + export_size > vaddr + vsize)
1819 export_size = vsize - (export_rva - vaddr);
1820 break;
1821 }
1822 }
1823
1824 expdata = (unsigned char *) xmalloc (export_size);
1825 bfd_seek (dll, expptr, SEEK_SET);
1826 bfd_read (expdata, 1, export_size, dll);
1827 erva = expdata - export_rva;
1828
1829 if (pe_def_file == 0)
1830 pe_def_file = def_file_empty();
1831
1832 nexp = pe_as32 (expdata+24);
1833 name_rvas = pe_as32 (expdata+32);
1834 ordinals = pe_as32 (expdata+36);
1835 ordbase = pe_as32 (expdata+16);
1836 for (i=0; i<nexp; i++)
1837 {
1838 unsigned long name_rva = pe_as32 (erva+name_rvas+i*4);
1839 def_file_import *imp;
1840 imp = def_file_add_import (pe_def_file, erva+name_rva, dll_name,
1841 i, 0);
1842 }
1843
1844 return true;
1845}
1846
1847/************************************************************************
1848
1849 These are the main functions, called from the emulation. The first
1850 is called after the bfds are read, so we can guess at how much space
1851 we need. The second is called after everything is placed, so we
1852 can put the right values in place.
1853
1854 ************************************************************************/
1855
1856void
1857pe_dll_build_sections (abfd, info)
1858 bfd *abfd;
1859 struct bfd_link_info *info;
1860{
c6c37250 1861 pe_dll_id_target (bfd_get_target (abfd));
252b5132
RH
1862 process_def_file (abfd, info);
1863
1864 generate_edata (abfd, info);
c6c37250
DD
1865 build_filler_bfd (1);
1866}
1867
1868void
1869pe_exe_build_sections (abfd, info)
1870 bfd *abfd;
1069dd8d 1871 struct bfd_link_info *info ATTRIBUTE_UNUSED;
c6c37250
DD
1872{
1873 pe_dll_id_target (bfd_get_target (abfd));
1874 build_filler_bfd (0);
252b5132
RH
1875}
1876
1877void
1878pe_dll_fill_sections (abfd, info)
1879 bfd *abfd;
1880 struct bfd_link_info *info;
1881{
c6c37250 1882 pe_dll_id_target (bfd_get_target (abfd));
252b5132
RH
1883 image_base = pe_data (abfd)->pe_opthdr.ImageBase;
1884
1885 generate_reloc (abfd, info);
1886 if (reloc_sz > 0)
1887 {
1888 bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
1889
1890 /* Resize the sections. */
1891 lang_size_sections (stat_ptr->head, abs_output_section,
1892 &stat_ptr->head, 0, (bfd_vma) 0, false);
1893
1894 /* Redo special stuff. */
1895 ldemul_after_allocation ();
1896
1897 /* Do the assignments again. */
1898 lang_do_assignments (stat_ptr->head,
1899 abs_output_section,
1900 (fill_type) 0, (bfd_vma) 0);
1901 }
1902
1903 fill_edata (abfd, info);
1904
1905 pe_data (abfd)->dll = 1;
1906
1907 edata_s->contents = edata_d;
1908 reloc_s->contents = reloc_d;
1909}
c6c37250
DD
1910
1911void
1912pe_exe_fill_sections (abfd, info)
1913 bfd *abfd;
1914 struct bfd_link_info *info;
1915{
1916 pe_dll_id_target (bfd_get_target (abfd));
1917 image_base = pe_data (abfd)->pe_opthdr.ImageBase;
1918
1919 generate_reloc (abfd, info);
1920 if (reloc_sz > 0)
1921 {
1922 bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
1923
1924 /* Resize the sections. */
1925 lang_size_sections (stat_ptr->head, abs_output_section,
1926 &stat_ptr->head, 0, (bfd_vma) 0, false);
1927
1928 /* Redo special stuff. */
1929 ldemul_after_allocation ();
1930
1931 /* Do the assignments again. */
1932 lang_do_assignments (stat_ptr->head,
1933 abs_output_section,
1934 (fill_type) 0, (bfd_vma) 0);
1935 }
1936 reloc_s->contents = reloc_d;
1937}