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