1 /* Routines to help build PEI-format DLLs (Win32 etc)
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3 Written by DJ Delorie <dj@cygnus.com>
5 This file is part of GLD, the Gnu Linker.
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)
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.
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
25 #include "libiberty.h"
38 #include "coff/internal.h"
39 #include "../bfd/libcoff.h"
43 /************************************************************************
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.
52 See also: ld/emultempl/pe.em
54 ************************************************************************/
56 /* for emultempl/pe.em */
58 def_file *pe_def_file = 0;
59 int pe_dll_export_everything = 0;
60 int pe_dll_do_default_excludes = 1;
61 int pe_dll_kill_ats = 0;
62 int pe_dll_stdcall_aliases = 0;
63 int pe_dll_warn_dup_exports = 0;
64 int pe_dll_compat_implib = 0;
66 /************************************************************************
68 static variables and types
70 ************************************************************************/
72 static bfd_vma image_base;
74 static bfd *filler_bfd;
75 static struct sec *edata_s, *reloc_s;
76 static unsigned char *edata_d, *reloc_d;
77 static size_t edata_sz, reloc_sz;
82 unsigned int imagebase_reloc;
88 #define PE_ARCH_i386 1
90 static pe_details_type pe_detail_list[] = {
99 { NULL, NULL, 0, 0, 0, 0 }
102 static pe_details_type *pe_details;
104 #define U(str) (pe_details->underscored ? "_" str : str)
107 pe_dll_id_target (target)
111 for (i=0; pe_detail_list[i].target_name; i++)
112 if (strcmp (pe_detail_list[i].target_name, target) == 0)
114 pe_details = pe_detail_list+i;
117 einfo (_("%XUnsupported PEI architecture: %s\n"), target);
121 /************************************************************************
123 Helper functions for qsort. Relocs must be sorted so that we can write
126 ************************************************************************/
138 bfd_vma a = ((reloc_data_type *) va)->vma;
139 bfd_vma b = ((reloc_data_type *) vb)->vma;
140 return (a > b) ? 1 : ((a < b) ? -1 : 0);
144 pe_export_sort (va, vb)
147 def_file_export *a = (def_file_export *) va;
148 def_file_export *b = (def_file_export *) vb;
149 return strcmp (a->name, b->name);
152 /************************************************************************
154 Read and process the .DEF file
156 ************************************************************************/
158 /* These correspond to the entries in pe_def_file->exports[]. I use
159 exported_symbol_sections[i] to tag whether or not the symbol was
160 defined, since we can't export symbols we don't have. */
162 static bfd_vma *exported_symbol_offsets;
163 static struct sec **exported_symbol_sections;
165 static int export_table_size;
166 static int count_exported;
167 static int count_exported_byname;
168 static int count_with_ordinals;
169 static const char *dll_name;
170 static int min_ordinal, max_ordinal;
171 static int *exported_symbols;
173 typedef struct exclude_list_struct
176 struct exclude_list_struct *next;
179 static struct exclude_list_struct *excludes = 0;
182 pe_dll_add_excludes (new_excludes)
183 const char *new_excludes;
186 char *exclude_string;
188 local_copy = xstrdup (new_excludes);
190 exclude_string = strtok (local_copy, ",:");
191 for (; exclude_string; exclude_string = strtok (NULL, ",:"))
193 struct exclude_list_struct *new_exclude;
195 new_exclude = ((struct exclude_list_struct *)
196 xmalloc (sizeof (struct exclude_list_struct)));
197 new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 1);
198 strcpy (new_exclude->string, exclude_string);
199 new_exclude->next = excludes;
200 excludes = new_exclude;
212 struct exclude_list_struct *ex;
213 for (i = 0; i < d->num_exports; i++)
214 if (strcmp (d->exports[i].name, n) == 0)
216 if (pe_dll_do_default_excludes)
218 if (strcmp (n, "DllMain@12") == 0)
220 if (strcmp (n, "DllEntryPoint@0") == 0)
222 if (strcmp (n, "impure_ptr") == 0)
225 for (ex = excludes; ex; ex = ex->next)
226 if (strcmp (n, ex->string) == 0)
232 process_def_file (abfd, info)
233 bfd *abfd ATTRIBUTE_UNUSED;
234 struct bfd_link_info *info;
237 struct bfd_link_hash_entry *blhe;
240 def_file_export *e=0;
243 pe_def_file = def_file_empty ();
245 /* First, run around to all the objects looking for the .drectve
246 sections, and push those into the def file too */
248 for (b = info->input_bfds; b; b = b->link_next)
250 s = bfd_get_section_by_name (b, ".drectve");
253 int size = bfd_get_section_size_before_reloc (s);
254 char *buf = xmalloc (size);
255 bfd_get_section_contents (b, s, buf, 0, size);
256 def_file_add_directive (pe_def_file, buf, size);
261 /* Now, maybe export everything else the default way */
263 if (pe_dll_export_everything || pe_def_file->num_exports == 0)
265 for (b = info->input_bfds; b; b = b->link_next)
270 symsize = bfd_get_symtab_upper_bound (b);
271 symbols = (asymbol **) xmalloc (symsize);
272 nsyms = bfd_canonicalize_symtab (b, symbols);
274 for (j = 0; j < nsyms; j++)
276 if ((symbols[j]->flags & (BSF_FUNCTION | BSF_GLOBAL))
277 == (BSF_FUNCTION | BSF_GLOBAL))
279 const char *sn = symbols[j]->name;
282 if (auto_export (pe_def_file, sn))
283 def_file_add_export (pe_def_file, sn, 0, -1);
290 #define NE pe_def_file->num_exports
292 /* Canonicalize the export list */
296 for (i = 0; i < NE; i++)
298 if (strchr (pe_def_file->exports[i].name, '@'))
300 /* This will preserve internal_name, which may have been pointing
301 to the same memory as name, or might not have */
302 char *tmp = xstrdup (pe_def_file->exports[i].name);
303 *(strchr (tmp, '@')) = 0;
304 pe_def_file->exports[i].name = tmp;
309 if (pe_dll_stdcall_aliases)
311 for (i = 0; i < NE; i++)
313 if (strchr (pe_def_file->exports[i].name, '@'))
315 char *tmp = xstrdup (pe_def_file->exports[i].name);
316 *(strchr (tmp, '@')) = 0;
317 if (auto_export (pe_def_file, tmp))
318 def_file_add_export (pe_def_file, tmp,
319 pe_def_file->exports[i].internal_name, -1);
326 e = pe_def_file->exports; /* convenience, but watch out for it changing */
328 exported_symbol_offsets = (bfd_vma *) xmalloc (NE * sizeof (bfd_vma));
329 exported_symbol_sections = (struct sec **) xmalloc (NE * sizeof (struct sec *));
331 memset (exported_symbol_sections, 0, NE * sizeof (struct sec *));
335 count_exported_byname = 0;
336 count_with_ordinals = 0;
338 qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]), pe_export_sort);
339 for (i = 0, j = 0; i < NE; i++)
341 if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0)
343 /* This is a duplicate. */
344 if (e[j - 1].ordinal != -1
345 && e[i].ordinal != -1
346 && e[j - 1].ordinal != e[i].ordinal)
348 if (pe_dll_warn_dup_exports)
349 /* xgettext:c-format */
350 einfo (_("%XError, duplicate EXPORT with oridinals: %s (%d vs %d)\n"),
351 e[j - 1].name, e[j - 1].ordinal, e[i].ordinal);
355 if (pe_dll_warn_dup_exports)
356 /* xgettext:c-format */
357 einfo (_("Warning, duplicate EXPORT: %s\n"),
361 e[j - 1].ordinal = e[i].ordinal;
362 e[j - 1].flag_private |= e[i].flag_private;
363 e[j - 1].flag_constant |= e[i].flag_constant;
364 e[j - 1].flag_noname |= e[i].flag_noname;
365 e[j - 1].flag_data |= e[i].flag_data;
374 pe_def_file->num_exports = j; /* == NE */
376 for (i = 0; i < NE; i++)
378 char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
379 if (pe_details->underscored)
382 strcpy (name + 1, pe_def_file->exports[i].internal_name);
385 strcpy (name, pe_def_file->exports[i].internal_name);
387 blhe = bfd_link_hash_lookup (info->hash,
392 && (blhe->type == bfd_link_hash_defined
393 || (blhe->type == bfd_link_hash_common)))
396 if (!pe_def_file->exports[i].flag_noname)
397 count_exported_byname++;
399 /* Only fill in the sections. The actual offsets are computed
400 in fill_exported_offsets() after common symbols are laid
402 if (blhe->type == bfd_link_hash_defined)
403 exported_symbol_sections[i] = blhe->u.def.section;
405 exported_symbol_sections[i] = blhe->u.c.p->section;
407 if (pe_def_file->exports[i].ordinal != -1)
409 if (max_ordinal < pe_def_file->exports[i].ordinal)
410 max_ordinal = pe_def_file->exports[i].ordinal;
411 if (min_ordinal > pe_def_file->exports[i].ordinal)
412 min_ordinal = pe_def_file->exports[i].ordinal;
413 count_with_ordinals++;
416 else if (blhe && blhe->type == bfd_link_hash_undefined)
418 /* xgettext:c-format */
419 einfo (_("%XCannot export %s: symbol not defined\n"),
420 pe_def_file->exports[i].internal_name);
424 /* xgettext:c-format */
425 einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"),
426 pe_def_file->exports[i].internal_name,
427 blhe->type, bfd_link_hash_defined);
431 /* xgettext:c-format */
432 einfo (_("%XCannot export %s: symbol not found\n"),
433 pe_def_file->exports[i].internal_name);
439 /************************************************************************
441 Build the bfd that will contain .edata and .reloc sections
443 ************************************************************************/
446 build_filler_bfd (include_edata)
449 lang_input_statement_type *filler_file;
450 filler_file = lang_add_input_file ("dll stuff",
451 lang_input_file_is_fake_enum,
453 filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd);
454 if (filler_bfd == NULL
455 || !bfd_set_arch_mach (filler_bfd,
456 bfd_get_arch (output_bfd),
457 bfd_get_mach (output_bfd)))
459 einfo ("%X%P: can not create BFD %E\n");
465 edata_s = bfd_make_section_old_way (filler_bfd, ".edata");
467 || !bfd_set_section_flags (filler_bfd, edata_s,
474 einfo ("%X%P: can not create .edata section: %E\n");
477 bfd_set_section_size (filler_bfd, edata_s, edata_sz);
480 reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc");
482 || !bfd_set_section_flags (filler_bfd, reloc_s,
489 einfo ("%X%P: can not create .reloc section: %E\n");
492 bfd_set_section_size (filler_bfd, reloc_s, 0);
494 ldlang_add_file (filler_file);
497 /************************************************************************
499 Gather all the exported symbols and build the .edata section
501 ************************************************************************/
504 generate_edata (abfd, info)
506 struct bfd_link_info *info ATTRIBUTE_UNUSED;
509 int name_table_size = 0;
512 /* First, we need to know how many exported symbols there are,
513 and what the range of ordinals is. */
515 if (pe_def_file->name)
517 dll_name = pe_def_file->name;
521 dll_name = abfd->filename;
522 for (dlnp = dll_name; *dlnp; dlnp++)
524 if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':')
529 if (count_with_ordinals && max_ordinal > count_exported)
531 if (min_ordinal > max_ordinal - count_exported + 1)
532 min_ordinal = max_ordinal - count_exported + 1;
537 max_ordinal = count_exported;
539 export_table_size = max_ordinal - min_ordinal + 1;
541 exported_symbols = (int *) xmalloc (export_table_size * sizeof (int));
542 for (i = 0; i < export_table_size; i++)
543 exported_symbols[i] = -1;
545 /* Now we need to assign ordinals to those that don't have them */
546 for (i = 0; i < NE; i++)
548 if (exported_symbol_sections[i])
550 if (pe_def_file->exports[i].ordinal != -1)
552 int ei = pe_def_file->exports[i].ordinal - min_ordinal;
553 int pi = exported_symbols[ei];
556 /* xgettext:c-format */
557 einfo (_("%XError, oridinal used twice: %d (%s vs %s)\n"),
558 pe_def_file->exports[i].ordinal,
559 pe_def_file->exports[i].name,
560 pe_def_file->exports[pi].name);
562 exported_symbols[ei] = i;
564 name_table_size += strlen (pe_def_file->exports[i].name) + 1;
568 next_ordinal = min_ordinal;
569 for (i = 0; i < NE; i++)
570 if (exported_symbol_sections[i])
571 if (pe_def_file->exports[i].ordinal == -1)
573 while (exported_symbols[next_ordinal - min_ordinal] != -1)
575 exported_symbols[next_ordinal - min_ordinal] = i;
576 pe_def_file->exports[i].ordinal = next_ordinal;
579 /* OK, now we can allocate some memory */
581 edata_sz = (40 /* directory */
582 + 4 * export_table_size /* addresses */
583 + 4 * count_exported_byname /* name ptrs */
584 + 2 * count_exported_byname /* ordinals */
585 + name_table_size + strlen (dll_name) + 1);
588 /* Fill the exported symbol offsets. The preliminary work has already
589 been done in process_def_file(). */
592 fill_exported_offsets (abfd, info)
594 struct bfd_link_info *info;
597 struct bfd_link_hash_entry *blhe;
600 def_file_export *e=0;
602 for (i = 0; i < pe_def_file->num_exports; i++)
604 char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
605 if (pe_details->underscored)
608 strcpy (name + 1, pe_def_file->exports[i].internal_name);
611 strcpy (name, pe_def_file->exports[i].internal_name);
613 blhe = bfd_link_hash_lookup (info->hash,
617 if (blhe && (blhe->type == bfd_link_hash_defined))
619 exported_symbol_offsets[i] = blhe->u.def.value;
626 fill_edata (abfd, info)
628 struct bfd_link_info *info ATTRIBUTE_UNUSED;
631 unsigned char *edirectory;
632 unsigned long *eaddresses;
633 unsigned long *enameptrs;
634 unsigned short *eordinals;
635 unsigned char *enamestr;
640 edata_d = (unsigned char *) xmalloc (edata_sz);
642 /* Note use of array pointer math here */
643 edirectory = edata_d;
644 eaddresses = (unsigned long *) (edata_d + 40);
645 enameptrs = eaddresses + export_table_size;
646 eordinals = (unsigned short *) (enameptrs + count_exported_byname);
647 enamestr = (char *) (eordinals + count_exported_byname);
649 #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base)
651 memset (edata_d, 0, 40);
652 bfd_put_32 (abfd, now, edata_d + 4);
653 if (pe_def_file->version_major != -1)
655 bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8);
656 bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10);
658 bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12);
659 strcpy (enamestr, dll_name);
660 enamestr += strlen (enamestr) + 1;
661 bfd_put_32 (abfd, min_ordinal, edata_d + 16);
662 bfd_put_32 (abfd, export_table_size, edata_d + 20);
663 bfd_put_32 (abfd, count_exported_byname, edata_d + 24);
664 bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28);
665 bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32);
666 bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36);
668 fill_exported_offsets (abfd, info);
670 /* Ok, now for the filling in part */
672 for (i = 0; i < export_table_size; i++)
674 int s = exported_symbols[i];
677 struct sec *ssec = exported_symbol_sections[s];
678 unsigned long srva = (exported_symbol_offsets[s]
679 + ssec->output_section->vma
680 + ssec->output_offset);
682 bfd_put_32 (abfd, srva - image_base, (void *) (eaddresses + i));
683 if (!pe_def_file->exports[s].flag_noname)
685 char *ename = pe_def_file->exports[s].name;
686 bfd_put_32 (abfd, ERVA (enamestr), (void *) enameptrs);
687 strcpy (enamestr, ename);
688 enamestr += strlen (enamestr) + 1;
689 bfd_put_16 (abfd, i, (void *) eordinals);
691 pe_def_file->exports[s].hint = hint++;
698 /************************************************************************
700 Gather all the relocations and build the .reloc section
702 ************************************************************************/
705 generate_reloc (abfd, info)
707 struct bfd_link_info *info;
710 /* for .reloc stuff */
711 reloc_data_type *reloc_data;
712 int total_relocs = 0;
714 unsigned long sec_page = (unsigned long) (-1);
715 unsigned long page_ptr, page_count;
721 for (b = info->input_bfds; b; b = b->link_next)
722 for (s = b->sections; s; s = s->next)
723 total_relocs += s->reloc_count;
725 reloc_data = (reloc_data_type *) xmalloc (total_relocs * sizeof (reloc_data_type));
729 for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next)
732 int relsize, nrelocs, i;
734 for (s = b->sections; s; s = s->next)
736 unsigned long sec_vma = s->output_section->vma + s->output_offset;
740 /* if it's not loaded, we don't need to relocate it this way */
741 if (!(s->output_section->flags & SEC_LOAD))
744 /* I don't know why there would be a reloc for these, but I've
745 seen it happen - DJ */
746 if (s->output_section == &bfd_abs_section)
749 if (s->output_section->vma == 0)
751 /* Huh? Shouldn't happen, but punt if it does */
752 einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n",
753 s->output_section->name, s->output_section->index,
754 s->output_section->flags);
758 symsize = bfd_get_symtab_upper_bound (b);
759 symbols = (asymbol **) xmalloc (symsize);
760 nsyms = bfd_canonicalize_symtab (b, symbols);
762 relsize = bfd_get_reloc_upper_bound (b, s);
763 relocs = (arelent **) xmalloc ((size_t) relsize);
764 nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
766 for (i = 0; i < nrelocs; i++)
768 if (!relocs[i]->howto->pc_relative
769 && relocs[i]->howto->type != pe_details->imagebase_reloc)
772 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
773 sym_vma = (relocs[i]->addend
776 + sym->section->output_offset
777 + sym->section->output_section->vma);
778 reloc_data[total_relocs].vma = sec_vma + relocs[i]->address;
779 switch (relocs[i]->howto->bitsize*1000
780 + relocs[i]->howto->rightshift)
783 reloc_data[total_relocs].type = 3;
787 /* xgettext:c-format */
788 einfo (_("%XError: %d-bit reloc in dll\n"),
789 relocs[i]->howto->bitsize);
795 /* Warning: the allocated symbols are remembered in BFD and reused
796 later, so don't free them! */
797 /* free (symbols); */
801 /* At this point, we have total_relocs relocation addresses in
802 reloc_addresses, which are all suitable for the .reloc section.
803 We must now create the new sections. */
805 qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort);
807 for (i = 0; i < total_relocs; i++)
809 unsigned long this_page = (reloc_data[i].vma >> 12);
810 if (this_page != sec_page)
812 reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */
814 sec_page = this_page;
818 reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */
820 reloc_d = (unsigned char *) xmalloc (reloc_sz);
822 sec_page = (unsigned long) (-1);
824 page_ptr = (unsigned long) (-1);
826 for (i = 0; i < total_relocs; i++)
828 unsigned long rva = reloc_data[i].vma - image_base;
829 unsigned long this_page = (rva & ~0xfff);
830 if (this_page != sec_page)
833 reloc_d[reloc_sz++] = 0;
834 if (page_ptr != (unsigned long) (-1))
835 bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
836 bfd_put_32 (abfd, this_page, reloc_d + reloc_sz);
839 sec_page = this_page;
842 bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type<<12),
845 if (reloc_data[i].type == 4)
847 bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz);
853 reloc_d[reloc_sz++] = 0;
854 if (page_ptr != (unsigned long) (-1))
855 bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
856 while (reloc_sz < reloc_s->_raw_size)
857 reloc_d[reloc_sz++] = 0;
860 /************************************************************************
862 Given the exiting def_file structure, print out a .DEF file that
865 ************************************************************************/
868 quoteput (s, f, needs_quotes)
874 for (cp = s; *cp; cp++)
878 || isspace ((unsigned char) *cp)
887 if (*s == '"' || *s == '\\')
899 pe_dll_generate_def_file (pe_out_def_filename)
900 const char *pe_out_def_filename;
903 FILE *out = fopen (pe_out_def_filename, "w");
906 /* xgettext:c-format */
907 einfo (_("%s: Can't open output def file %s\n"),
908 program_name, pe_out_def_filename);
913 if (pe_def_file->name)
915 if (pe_def_file->is_dll)
916 fprintf (out, "LIBRARY ");
918 fprintf (out, "NAME ");
919 quoteput (pe_def_file->name, out, 1);
920 if (pe_data (output_bfd)->pe_opthdr.ImageBase)
921 fprintf (out, " BASE=0x%lx",
922 (unsigned long) pe_data (output_bfd)->pe_opthdr.ImageBase);
926 if (pe_def_file->description)
928 fprintf (out, "DESCRIPTION ");
929 quoteput (pe_def_file->description, out, 1);
933 if (pe_def_file->version_minor != -1)
934 fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major,
935 pe_def_file->version_minor);
936 else if (pe_def_file->version_major != -1)
937 fprintf (out, "VERSION %d\n", pe_def_file->version_major);
939 if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1)
942 if (pe_def_file->stack_commit != -1)
943 fprintf (out, "STACKSIZE 0x%x,0x%x\n",
944 pe_def_file->stack_reserve, pe_def_file->stack_commit);
945 else if (pe_def_file->stack_reserve != -1)
946 fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve);
947 if (pe_def_file->heap_commit != -1)
948 fprintf (out, "HEAPSIZE 0x%x,0x%x\n",
949 pe_def_file->heap_reserve, pe_def_file->heap_commit);
950 else if (pe_def_file->heap_reserve != -1)
951 fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve);
953 if (pe_def_file->num_section_defs > 0)
955 fprintf (out, "\nSECTIONS\n\n");
956 for (i = 0; i < pe_def_file->num_section_defs; i++)
959 quoteput (pe_def_file->section_defs[i].name, out, 0);
960 if (pe_def_file->section_defs[i].class)
962 fprintf (out, " CLASS ");
963 quoteput (pe_def_file->section_defs[i].class, out, 0);
965 if (pe_def_file->section_defs[i].flag_read)
966 fprintf (out, " READ");
967 if (pe_def_file->section_defs[i].flag_write)
968 fprintf (out, " WRITE");
969 if (pe_def_file->section_defs[i].flag_execute)
970 fprintf (out, " EXECUTE");
971 if (pe_def_file->section_defs[i].flag_shared)
972 fprintf (out, " SHARED");
977 if (pe_def_file->num_exports > 0)
979 fprintf (out, "\nEXPORTS\n\n");
980 for (i = 0; i < pe_def_file->num_exports; i++)
982 def_file_export *e = pe_def_file->exports + i;
984 quoteput (e->name, out, 0);
985 if (e->internal_name && strcmp (e->internal_name, e->name))
987 fprintf (out, " = ");
988 quoteput (e->internal_name, out, 0);
990 if (e->ordinal != -1)
991 fprintf (out, " @%d", e->ordinal);
993 fprintf (out, " PRIVATE");
994 if (e->flag_constant)
995 fprintf (out, " CONSTANT");
997 fprintf (out, " NONAME");
999 fprintf (out, " DATA");
1001 fprintf (out, "\n");
1005 if (pe_def_file->num_imports > 0)
1007 fprintf (out, "\nIMPORTS\n\n");
1008 for (i = 0; i < pe_def_file->num_imports; i++)
1010 def_file_import *im = pe_def_file->imports + i;
1012 if (im->internal_name
1013 && (!im->name || strcmp (im->internal_name, im->name)))
1015 quoteput (im->internal_name, out, 0);
1016 fprintf (out, " = ");
1018 quoteput (im->module->name, out, 0);
1021 quoteput (im->name, out, 0);
1023 fprintf (out, "%d", im->ordinal);
1024 fprintf (out, "\n");
1029 fprintf (out, _("; no contents available\n"));
1031 if (fclose (out) == EOF)
1033 /* xgettext:c-format */
1034 einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename);
1038 /************************************************************************
1040 Generate the import library
1042 ************************************************************************/
1044 static asymbol **symtab;
1047 static const char *dll_filename;
1048 static char *dll_symname;
1050 #define UNDSEC (asection *) &bfd_und_section
1053 quick_section(abfd, name, flags, align)
1062 sec = bfd_make_section_old_way (abfd, name);
1063 bfd_set_section_flags (abfd, sec, flags
1068 bfd_set_section_alignment (abfd, sec, align);
1069 /* remember to undo this before trying to link internally! */
1070 sec->output_section = sec;
1072 sym = bfd_make_empty_symbol (abfd);
1073 symtab[symptr++] = sym;
1074 sym->name = sec->name;
1076 sym->flags = BSF_LOCAL;
1083 quick_symbol (abfd, n1, n2, n3, sec, flags, addr)
1093 char *name = (char *) xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
1097 sym = bfd_make_empty_symbol (abfd);
1102 symtab[symptr++] = sym;
1105 static arelent *reltab = 0;
1106 static int relcount = 0, relsize = 0;
1109 quick_reloc (abfd, address, which_howto, symidx)
1115 if (relcount >= (relsize-1))
1119 reltab = (arelent *) xrealloc (reltab, relsize * sizeof (arelent));
1121 reltab = (arelent *) xmalloc (relsize * sizeof (arelent));
1123 reltab[relcount].address = address;
1124 reltab[relcount].addend = 0;
1125 reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
1126 reltab[relcount].sym_ptr_ptr = symtab + symidx;
1131 save_relocs (asection *sec)
1134 sec->relocation = reltab;
1135 sec->reloc_count = relcount;
1136 sec->orelocation = (arelent **) xmalloc ((relcount+1) * sizeof (arelent *));
1137 for (i=0; i<relcount; i++)
1138 sec->orelocation[i] = sec->relocation + i;
1139 sec->orelocation[relcount] = 0;
1140 sec->flags |= SEC_RELOC;
1142 relcount = relsize = 0;
1147 * .global __head_my_dll
1152 * .rva __my_dll_iname
1168 asection *id2, *id5, *id4;
1169 unsigned char *d2, *d5, *d4;
1173 oname = (char *) xmalloc (20);
1174 sprintf (oname, "d%06d.o", tmp_seq);
1177 abfd = bfd_create (oname, parent);
1178 bfd_find_target (pe_details->object_target, abfd);
1179 bfd_make_writable (abfd);
1181 bfd_set_format (abfd, bfd_object);
1182 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1185 symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *));
1186 id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
1187 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1188 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1189 quick_symbol (abfd, U("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
1190 quick_symbol (abfd, U(""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
1192 /* OK, pay attention here. I got confused myself looking back at
1193 it. We create a four-byte section to mark the beginning of the
1194 list, and we include an offset of 4 in the section, so that the
1195 pointer to the list points to the *end* of this section, which is
1196 the start of the list of sections from other objects. */
1198 bfd_set_section_size (abfd, id2, 20);
1199 d2 = (unsigned char *) xmalloc (20);
1202 d2[0] = d2[16] = 4; /* reloc addend */
1203 quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
1204 quick_reloc (abfd, 12, BFD_RELOC_RVA, 4);
1205 quick_reloc (abfd, 16, BFD_RELOC_RVA, 1);
1208 bfd_set_section_size (abfd, id5, 4);
1209 d5 = (unsigned char *) xmalloc (4);
1213 bfd_set_section_size (abfd, id4, 4);
1214 d4 = (unsigned char *) xmalloc (4);
1218 bfd_set_symtab (abfd, symtab, symptr);
1220 bfd_set_section_contents (abfd, id2, d2, 0, 20);
1221 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1222 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1224 bfd_make_readable (abfd);
1234 * .global __my_dll_iname
1243 asection *id4, *id5, *id7;
1244 unsigned char *d4, *d5, *d7;
1249 oname = (char *) xmalloc (20);
1250 sprintf (oname, "d%06d.o", tmp_seq);
1253 abfd = bfd_create (oname, parent);
1254 bfd_find_target (pe_details->object_target, abfd);
1255 bfd_make_writable (abfd);
1257 bfd_set_format (abfd, bfd_object);
1258 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1261 symtab = (asymbol **) xmalloc (5 * sizeof (asymbol *));
1262 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1263 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1264 id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1265 quick_symbol (abfd, U(""), dll_symname, "_iname", id7, BSF_GLOBAL, 0);
1267 bfd_set_section_size (abfd, id4, 4);
1268 d4 = (unsigned char *) xmalloc (4);
1272 bfd_set_section_size (abfd, id5, 4);
1273 d5 = (unsigned char *) xmalloc (4);
1277 len = strlen (dll_filename)+1;
1280 bfd_set_section_size (abfd, id7, len);
1281 d7 = (unsigned char *) xmalloc (len);
1283 strcpy (d7, dll_filename);
1285 bfd_set_symtab (abfd, symtab, symptr);
1287 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1288 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1289 bfd_set_section_contents (abfd, id7, d7, 0, len);
1291 bfd_make_readable (abfd);
1298 * .global ___imp_function
1299 * .global __imp__function
1301 * jmp *__imp__function:
1304 * .long __head_my_dll
1315 * .asciz "function" xlate? (add underscore, kill at)
1318 static unsigned char jmp_ix86_bytes[] = {
1319 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
1324 make_one (exp, parent)
1325 def_file_export *exp;
1328 asection *tx, *id7, *id5, *id4, *id6;
1329 unsigned char *td, *d7, *d5, *d4, *d6;
1333 unsigned char *jmp_bytes;
1336 switch (pe_details->pe_arch)
1339 jmp_bytes = jmp_ix86_bytes;
1340 jmp_byte_count = sizeof (jmp_ix86_bytes);
1344 oname = (char *) xmalloc (20);
1345 sprintf (oname, "d%06d.o", tmp_seq);
1348 abfd = bfd_create (oname, parent);
1349 bfd_find_target (pe_details->object_target, abfd);
1350 bfd_make_writable (abfd);
1352 bfd_set_format (abfd, bfd_object);
1353 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1356 symtab = (asymbol **) xmalloc (10 * sizeof (asymbol *));
1357 tx = quick_section (abfd, ".text", SEC_CODE|SEC_HAS_CONTENTS, 2);
1358 id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1359 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1360 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1361 id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
1362 if (! exp->flag_data)
1363 quick_symbol (abfd, U(""), exp->internal_name, "", tx, BSF_GLOBAL, 0);
1364 quick_symbol (abfd, U("_head_"), dll_symname, "", UNDSEC, BSF_GLOBAL, 0);
1365 quick_symbol (abfd, U("__imp_"), exp->internal_name, "", id5, BSF_GLOBAL, 0);
1366 if (pe_dll_compat_implib)
1367 quick_symbol (abfd, U("__imp_"), exp->internal_name, "",
1368 id5, BSF_GLOBAL, 0);
1370 bfd_set_section_size (abfd, tx, jmp_byte_count);
1371 td = (unsigned char *) xmalloc (jmp_byte_count);
1373 memcpy (td, jmp_bytes, jmp_byte_count);
1374 switch (pe_details->pe_arch)
1377 quick_reloc (abfd, 2, BFD_RELOC_32, 2);
1382 bfd_set_section_size (abfd, id7, 4);
1383 d7 = (unsigned char *) xmalloc (4);
1386 quick_reloc (abfd, 0, BFD_RELOC_RVA, 6);
1389 bfd_set_section_size (abfd, id5, 4);
1390 d5 = (unsigned char *) xmalloc (4);
1393 if (exp->flag_noname)
1395 d5[0] = exp->ordinal;
1396 d5[1] = exp->ordinal >> 8;
1401 quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1405 bfd_set_section_size (abfd, id4, 4);
1406 d4 = (unsigned char *) xmalloc (4);
1409 if (exp->flag_noname)
1411 d5[0] = exp->ordinal;
1412 d5[1] = exp->ordinal >> 8;
1417 quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1421 if (exp->flag_noname)
1424 bfd_set_section_size (abfd, id6, 0);
1428 len = strlen (exp->name) + 3;
1431 bfd_set_section_size (abfd, id6, len);
1432 d6 = (unsigned char *) xmalloc (len);
1434 memset (d6, 0, len);
1435 d6[0] = exp->hint & 0xff;
1436 d6[1] = exp->hint >> 8;
1437 strcpy (d6+2, exp->name);
1440 bfd_set_symtab (abfd, symtab, symptr);
1442 bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
1443 bfd_set_section_contents (abfd, id7, d7, 0, 4);
1444 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1445 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1446 if (!exp->flag_noname)
1447 bfd_set_section_contents (abfd, id6, d6, 0, len);
1449 bfd_make_readable (abfd);
1454 pe_dll_generate_implib (def, impfilename)
1456 const char *impfilename;
1464 dll_filename = (def->name) ? def->name : dll_name;
1465 dll_symname = xstrdup (dll_filename);
1466 for (i=0; dll_symname[i]; i++)
1467 if (!isalnum ((unsigned char) dll_symname[i]))
1468 dll_symname[i] = '_';
1470 unlink (impfilename);
1472 outarch = bfd_openw (impfilename, 0);
1476 /* xgettext:c-format */
1477 einfo (_("%XCan't open .lib file: %s\n"), impfilename);
1481 /* xgettext:c-format */
1482 einfo (_("Creating library file: %s\n"), impfilename);
1484 bfd_set_format (outarch, bfd_archive);
1485 outarch->has_armap = 1;
1487 /* Work out a reasonable size of things to put onto one line. */
1489 ar_head = make_head (outarch);
1491 for (i = 0; i<def->num_exports; i++)
1493 /* The import library doesn't know about the internal name */
1494 char *internal = def->exports[i].internal_name;
1496 def->exports[i].internal_name = def->exports[i].name;
1497 n = make_one (def->exports+i, outarch);
1500 def->exports[i].internal_name = internal;
1503 ar_tail = make_tail (outarch);
1505 if (ar_head == NULL || ar_tail == NULL)
1508 /* Now stick them all into the archive */
1510 ar_head->next = head;
1511 ar_tail->next = ar_head;
1514 if (! bfd_set_archive_head (outarch, head))
1515 einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ()));
1517 if (! bfd_close (outarch))
1518 einfo ("%Xbfd_close %s: %s\n", impfilename, bfd_errmsg (bfd_get_error ()));
1520 while (head != NULL)
1522 bfd *n = head->next;
1529 add_bfd_to_link (abfd, name, link_info)
1532 struct bfd_link_info *link_info;
1534 lang_input_statement_type *fake_file;
1535 fake_file = lang_add_input_file (name,
1536 lang_input_file_is_fake_enum,
1538 fake_file->the_bfd = abfd;
1539 ldlang_add_file (fake_file);
1540 if (!bfd_link_add_symbols (abfd, link_info))
1541 einfo ("%Xaddsym %s: %s\n", name, bfd_errmsg (bfd_get_error ()));
1545 pe_process_import_defs (output_bfd, link_info)
1547 struct bfd_link_info *link_info;
1549 def_file_module *module;
1550 pe_dll_id_target(bfd_get_target (output_bfd));
1555 for (module = pe_def_file->modules; module; module = module->next)
1559 dll_filename = module->name;
1560 dll_symname = xstrdup (module->name);
1561 for (i=0; dll_symname[i]; i++)
1562 if (!isalnum (dll_symname[i]))
1563 dll_symname[i] = '_';
1567 for (i=0; i<pe_def_file->num_imports; i++)
1568 if (pe_def_file->imports[i].module == module)
1570 def_file_export exp;
1571 struct bfd_link_hash_entry *blhe;
1573 /* see if we need this import */
1574 char *name = (char *) xmalloc (strlen (pe_def_file->imports[i].internal_name) + 2);
1575 sprintf (name, "%s%s", U(""), pe_def_file->imports[i].internal_name);
1576 blhe = bfd_link_hash_lookup (link_info->hash, name,
1577 false, false, false);
1579 if (blhe && blhe->type == bfd_link_hash_undefined)
1585 bfd *ar_head = make_head (output_bfd);
1586 add_bfd_to_link (ar_head, ar_head->filename, link_info);
1589 exp.internal_name = pe_def_file->imports[i].internal_name;
1590 exp.name = pe_def_file->imports[i].name;
1591 exp.ordinal = pe_def_file->imports[i].ordinal;
1592 exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
1593 exp.flag_private = 0;
1594 exp.flag_constant = 0;
1596 exp.flag_noname = exp.name ? 0 : 1;
1597 one = make_one (&exp, output_bfd);
1598 add_bfd_to_link (one, one->filename, link_info);
1603 bfd *ar_tail = make_tail (output_bfd);
1604 add_bfd_to_link (ar_tail, ar_tail->filename, link_info);
1611 /************************************************************************
1613 We were handed a *.DLL file. Parse it and turn it into a set of
1614 IMPORTS directives in the def file. Return true if the file was
1615 handled, false if not.
1617 ************************************************************************/
1620 pe_get16 (abfd, where)
1625 bfd_seek (abfd, where, SEEK_SET);
1626 bfd_read (b, 1, 2, abfd);
1627 return b[0] + (b[1]<<8);
1631 pe_get32 (abfd, where)
1636 bfd_seek (abfd, where, SEEK_SET);
1637 bfd_read (b, 1, 4, abfd);
1638 return b[0] + (b[1]<<8) + (b[2]<<16) + (b[3]<<24);
1641 #if 0 /* This is not currently used. */
1647 unsigned char *b = ptr;
1648 return b[0] + (b[1]<<8);
1657 unsigned char *b = ptr;
1658 return b[0] + (b[1]<<8) + (b[2]<<16) + (b[3]<<24);
1662 pe_implied_import_dll (filename)
1663 const char *filename;
1666 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
1667 unsigned long export_rva, export_size, nsections, secptr, expptr;
1668 unsigned char *expdata, *erva;
1669 unsigned long name_rvas, ordinals, nexp, ordbase;
1670 const char *dll_name;
1672 /* No, I can't use bfd here. kernel32.dll puts its export table in
1673 the middle of the .rdata section. */
1675 dll = bfd_openr (filename, pe_details->target_name);
1678 einfo ("%Xopen %s: %s\n", filename, bfd_errmsg (bfd_get_error ()));
1681 /* PEI dlls seem to be bfd_objects */
1682 if (!bfd_check_format (dll, bfd_object))
1684 einfo ("%X%s: this doesn't appear to be a DLL\n", filename);
1688 dll_name = filename;
1689 for (i=0; filename[i]; i++)
1690 if (filename[i] == '/' || filename[i] == '\\' || filename[i] == ':')
1691 dll_name = filename + i + 1;
1693 pe_header_offset = pe_get32 (dll, 0x3c);
1694 opthdr_ofs = pe_header_offset + 4 + 20;
1695 num_entries = pe_get32 (dll, opthdr_ofs + 92);
1696 if (num_entries < 1) /* no exports */
1698 export_rva = pe_get32 (dll, opthdr_ofs + 96);
1699 export_size = pe_get32 (dll, opthdr_ofs + 100);
1700 nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
1701 secptr = (pe_header_offset + 4 + 20 +
1702 pe_get16 (dll, pe_header_offset + 4 + 16));
1704 for (i=0; i<nsections; i++)
1707 unsigned long secptr1 = secptr + 40 * i;
1708 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
1709 unsigned long vsize = pe_get32 (dll, secptr1 + 16);
1710 unsigned long fptr = pe_get32 (dll, secptr1 + 20);
1711 bfd_seek(dll, secptr1, SEEK_SET);
1712 bfd_read(sname, 1, 8, dll);
1713 if (vaddr <= export_rva && vaddr+vsize > export_rva)
1715 expptr = fptr + (export_rva - vaddr);
1716 if (export_rva + export_size > vaddr + vsize)
1717 export_size = vsize - (export_rva - vaddr);
1722 expdata = (unsigned char *) xmalloc (export_size);
1723 bfd_seek (dll, expptr, SEEK_SET);
1724 bfd_read (expdata, 1, export_size, dll);
1725 erva = expdata - export_rva;
1727 if (pe_def_file == 0)
1728 pe_def_file = def_file_empty();
1730 nexp = pe_as32 (expdata+24);
1731 name_rvas = pe_as32 (expdata+32);
1732 ordinals = pe_as32 (expdata+36);
1733 ordbase = pe_as32 (expdata+16);
1734 for (i=0; i<nexp; i++)
1736 unsigned long name_rva = pe_as32 (erva+name_rvas+i*4);
1737 def_file_import *imp;
1738 imp = def_file_add_import (pe_def_file, erva+name_rva, dll_name,
1745 /************************************************************************
1747 These are the main functions, called from the emulation. The first
1748 is called after the bfds are read, so we can guess at how much space
1749 we need. The second is called after everything is placed, so we
1750 can put the right values in place.
1752 ************************************************************************/
1755 pe_dll_build_sections (abfd, info)
1757 struct bfd_link_info *info;
1759 pe_dll_id_target (bfd_get_target (abfd));
1760 process_def_file (abfd, info);
1762 generate_edata (abfd, info);
1763 build_filler_bfd (1);
1767 pe_exe_build_sections (abfd, info)
1769 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1771 pe_dll_id_target (bfd_get_target (abfd));
1772 build_filler_bfd (0);
1776 pe_dll_fill_sections (abfd, info)
1778 struct bfd_link_info *info;
1780 pe_dll_id_target (bfd_get_target (abfd));
1781 image_base = pe_data (abfd)->pe_opthdr.ImageBase;
1783 generate_reloc (abfd, info);
1786 bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
1788 /* Resize the sections. */
1789 lang_size_sections (stat_ptr->head, abs_output_section,
1790 &stat_ptr->head, 0, (bfd_vma) 0, false);
1792 /* Redo special stuff. */
1793 ldemul_after_allocation ();
1795 /* Do the assignments again. */
1796 lang_do_assignments (stat_ptr->head,
1798 (fill_type) 0, (bfd_vma) 0);
1801 fill_edata (abfd, info);
1803 pe_data (abfd)->dll = 1;
1805 edata_s->contents = edata_d;
1806 reloc_s->contents = reloc_d;
1810 pe_exe_fill_sections (abfd, info)
1812 struct bfd_link_info *info;
1814 pe_dll_id_target (bfd_get_target (abfd));
1815 image_base = pe_data (abfd)->pe_opthdr.ImageBase;
1817 generate_reloc (abfd, info);
1820 bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
1822 /* Resize the sections. */
1823 lang_size_sections (stat_ptr->head, abs_output_section,
1824 &stat_ptr->head, 0, (bfd_vma) 0, false);
1826 /* Redo special stuff. */
1827 ldemul_after_allocation ();
1829 /* Do the assignments again. */
1830 lang_do_assignments (stat_ptr->head,
1832 (fill_type) 0, (bfd_vma) 0);
1834 reloc_s->contents = reloc_d;