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