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