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