042a454d90c6d7ad20a654c9001d9c56374b40b3
1 /* dlltool.c -- tool to generate stuff for PE style DLLs
2 Copyright (C) 1995 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 This program allows you to build the files necessary to create
23 DLLs to run on a system which understands PE format image files.
26 A DLL contains an export table which contains the information
27 which the runtime loader needs to tie up references from a
30 The export table is generated by this program by reading
31 in a .DEF file or scanning the .a and .o files which will be in the
32 DLL. A .o file can contain information in special ".drective" sections
33 with export information.
35 A DEF file contains any number of the following commands:
38 NAME <name> [ , <base> ]
39 The result is going to be <name>.EXE
41 LIBRARY <name> [ , <base> ]
42 The result is going to be <name>.DLL
44 EXPORTS ( <name1> [ = <name2> ] [ @ <integer> ] [ NONAME ] [CONSTANT] ) *
45 Declares name1 as an exported symbol from the
46 DLL, with optional ordinal number <integer>
48 IMPORTS ( [ <name> = ] <name> . <name> ) *
49 Ignored for compatibility
52 Puts <string> into output .exp file in the .rdata section
54 [STACKSIZE|HEAPSIZE] <number-reserve> [ , <number-commit> ]
55 Generates --stack|--heap <number-reserve>,<number-commit>
56 in the output .drective section. The linker will
57 see this and act upon it.
60 SECTIONS ( <sectionname> <attr>+ )*
61 <attr> = READ | WRITE | EXECUTE | SHARED
62 Generates --attr <sectionname> <attr> in the output
63 .drective section. The linker will see this and act
67 A -export:<name> in a .drective section in an input .o or .a
68 file to this program is equivalent to a EXPORTS <name>
73 The program generates output files with the prefix supplied
74 on the command line, or in the def file, or taken from the first
77 The output files are <prefix>.exp.s and <prefix>.lib.s
79 The .exp.s file contains the information necessary to export
80 the routines in the DLL. The .lib.s file contains the information
81 necessary to use the DLL's routines from a referencing program.
88 asm (".section .drectve");
89 asm (".ascii \"-export:adef\"");
93 printf("hello from the dll %s\n",s);
98 printf("hello from the dll and the other entry point %s\n",s);
102 asm (".section .drectve");
103 asm (".ascii \"-export:cdef\"");
104 asm (".ascii \"-export:ddef\"");
107 printf("hello from the dll %s\n",s);
112 printf("hello from the dll and the other entry point %s\n",s);
130 HEAPSIZE 0x40000, 0x2000
134 SECTIONS donkey READ WRITE
138 # compile up the parts of the dll
143 # put them in a library (you don't have to, you
144 # could name all the .os on the dlltool line)
146 ar qcv thedll.in file1.o file2.o
149 # run this tool over the library and the def file
150 ./dlltool -o thedll -d thedll.def thedll.in
152 # build the export table for the dll
153 as -o thedll.exp thedll.exp.s
154 # build the dll with the library with file1.o, file2.o and the export table
155 ld -o thedll.dll thedll.exp thedll.in
157 # build the import table for the executable
158 as -o thedll.lib thedll.lib.s
163 # link the executable with the import library
164 ld -e main -Tthemain.ld -o themain.exe themain.o thedll.lib
178 char *outfile_prefix
;
185 static char *mname
= "arm";
189 static char *mname
= "i386";
204 = {{"arm",".byte",".short",".long",".asciz","@","bl",".global",".space"},
205 {"i386",".byte",".short",".long",".asciz",";","jmp",".global",".space"},
208 #define ASM_BYTE mtable[machine].how_byte
209 #define ASM_SHORT mtable[machine].how_short
210 #define ASM_LONG mtable[machine].how_long
211 #define ASM_TEXT mtable[machine].how_asciz
212 #define ASM_C mtable[machine].how_comment
213 #define ASM_JUMP mtable[machine].how_jump
214 #define ASM_GLOBAL mtable[machine].how_global
215 #define ASM_SPACE mtable[machine].how_space
217 #define PATHMAX 250 /* What's the right name for this ? */
222 FILE *yyin
; /* communications with flex */
223 extern int linenumber
;
225 process_def_file (name
)
228 FILE *f
= fopen (name
, "r");
231 fprintf (stderr
, "Can't open def file %s\n", name
);
240 /**********************************************************************/
242 /* Communications with the parser */
251 typedef struct export
262 static char *d_name
; /* Arg to NAME or LIBRARY */
263 static int d_nfuncs
; /* Number of functions exported */
264 static int d_ord
; /* Base ordinal index */
265 static export_type
*d_exports
; /*list of exported functions */
266 static char *d_suffix
= "dll";
267 static dlist_type
*d_list
; /* Descriptions */
268 static dlist_type
*a_list
; /* Stuff to go in directives */
275 fprintf (stderr
, "Syntax error in def file %s:%d\n",
276 def_file
, linenumber
);
280 def_exports (name
, internal_name
, ordinal
, noname
, constant
)
287 struct export
*p
= (struct export
*) xmalloc (sizeof (*p
));
290 p
->internal_name
= internal_name
? internal_name
: name
;
291 p
->ordinal
= ordinal
;
292 p
->constant
= constant
;
300 def_name (name
, base
)
304 printf ("NAME %s base %x\n", name
, base
);
307 fprintf (stderr
, "Can't have LIBRARY and NAME\n");
310 if (strchr (d_name
, '.'))
311 d_suffix
= strdup (strchr (d_name
, '.') + 1);
316 def_library (name
, base
)
320 printf ("LIBRARY %s base %x\n", name
, base
);
323 fprintf (stderr
, "Can't have LIBRARY and NAME\n");
326 if (strchr (d_name
, '.'))
327 d_suffix
= strdup (strchr (d_name
, '.') + 1);
332 def_description (desc
)
335 dlist_type
*d
= (dlist_type
*)xmalloc(sizeof(dlist_type
));
336 d
->text
= strdup (desc
);
341 void new_directive(dir
)
344 dlist_type
*d
= (dlist_type
*)xmalloc(sizeof(dlist_type
));
345 d
->text
= strdup (dir
);
351 def_stacksize (reserve
, commit
)
357 sprintf (b
,"-stack 0x%x,0x%x ", reserve
, commit
);
359 sprintf (b
,"-stack 0x%x ", reserve
);
360 new_directive (strdup(b
));
364 def_heapsize (reserve
, commit
)
370 sprintf (b
,"-heap 0x%x,0x%x ", reserve
, commit
);
372 sprintf (b
,"-heap 0x%x ", reserve
);
373 new_directive (strdup(b
));
378 def_import (internal
, module
, entry
)
383 fprintf (stderr
, "IMPORTS are ignored");
387 def_version (major
, minor
)
389 printf ("VERSION %d.%d\n", major
, minor
);
394 def_section (name
, attr
)
411 sprintf (buf
, "-attr %s %s", name
, atts
);
412 new_directive (strdup(buf
));
419 def_section ("CODE", attr
);
426 def_section ("DATA",attr
);
430 /**********************************************************************/
433 scan_open_obj_file (abfd
)
436 /* Look for .drectives */
437 asection
*s
= bfd_get_section_by_name (abfd
, ".drectve");
440 int size
= bfd_get_section_size_before_reloc (s
);
441 char *buf
= xmalloc (size
);
444 bfd_get_section_contents (abfd
, s
, buf
, 0, size
);
445 printf ("Sucking in info from %s\n",
446 bfd_get_filename (abfd
));
448 /* Search for -export: strings */
454 && strncmp (p
, "-export:", 8) == 0)
460 while (*p
!= ' ' && *p
!= '-' && p
< e
)
462 c
= xmalloc (p
- name
+ 1);
463 memcpy (c
, name
, p
- name
);
465 def_exports (c
, 0, -1, 0);
476 scan_obj_file (filename
)
479 bfd
*f
= bfd_openr (filename
, 0);
483 fprintf (stderr
, "Unable to open object file %s\n", filename
);
486 if (bfd_check_format (f
, bfd_archive
))
488 bfd
*arfile
= bfd_openr_next_archived_file (f
, 0);
491 if (bfd_check_format (arfile
, bfd_object
))
492 scan_open_obj_file (arfile
);
494 arfile
= bfd_openr_next_archived_file (f
, arfile
);
498 if (bfd_check_format (f
, bfd_object
))
500 scan_open_obj_file (f
);
506 /**********************************************************************/
509 /* return the bit of the name before the last . */
516 char *res
= strdup (name
);
517 char *p
= strrchr (res
, '.');
529 fprintf(f
,"%s ", ASM_C
);
530 for (i
= 0; oav
[i
]; i
++)
531 fprintf(f
,"%s ", oav
[i
]);
533 for (i
= 0, exp
= d_exports
; exp
; i
++, exp
= exp
->next
)
535 fprintf (f
, "%s %d = %s %s @ %d %s%s\n",
541 exp
->noname
? "NONAME " : "",
542 exp
->constant
? "CONSTANT" : "");
545 /* Generate the .exp file */
552 char outfile
[PATHMAX
];
556 sprintf (outfile
, "%s.exp.s", outfile_prefix
);
558 f
= fopen (outfile
, "w");
561 fprintf (stderr
, "Unable to open output file %s\n", outfile
);
565 fprintf (f
, "\t.section .edata\n\n");
566 fprintf (f
, "\t%s 0 %s Allways 0\n", ASM_LONG
, ASM_C
);
567 fprintf (f
, "\t%s %d %s Time and date\n", ASM_LONG
, time (0), ASM_C
);
568 fprintf (f
, "\t%s 0 %s Major and Minor version\n", ASM_LONG
, ASM_C
);
569 fprintf (f
, "\t%s name %s Ptr to name of dll\n", ASM_LONG
, ASM_C
);
570 fprintf (f
, "\t%s %d %s Starting ordinal of exports\n", ASM_LONG
, d_ord
, ASM_C
);
571 fprintf (f
, "\t%s The next field is documented as being the number of functions\n", ASM_C
);
572 fprintf (f
, "\t%s yet it doesn't look like that in real PE dlls\n", ASM_C
);
573 fprintf (f
, "\t%s But it shouldn't be a problem, causes there's\n", ASM_C
);
574 fprintf (f
, "\t%s always the number of names field\n", ASM_C
);
575 fprintf (f
, "\t%s %d %s Number of functions\n", ASM_LONG
, d_nfuncs
, ASM_C
);
576 fprintf (f
, "\t%s %d %s Number of names\n", ASM_LONG
, d_nfuncs
, ASM_C
);
577 fprintf (f
, "\t%s afuncs %s Address of functions\n", ASM_LONG
, ASM_C
);
578 fprintf (f
, "\t%s anames %s Address of names\n", ASM_LONG
, ASM_C
);
579 fprintf (f
, "\t%s anords %s Address of ordinals\n", ASM_LONG
, ASM_C
);
581 fprintf (f
, "name: %s \"%s.%s\"\n", ASM_TEXT
, outfile_prefix
, d_suffix
);
583 fprintf (f
, "afuncs:\n");
585 for (exp
= d_exports
; exp
; exp
= exp
->next
)
587 if (exp
->ordinal
!= i
)
589 fprintf (f
, "\t%s\t%d\t@ %d..%d missing\n", ASM_SPACE
,
590 (exp
->ordinal
- i
) * 4,
591 i
, exp
->ordinal
- 1);
594 fprintf (f
, "\t%s %s\t%s %d\n", ASM_LONG
, exp
->internal_name
, ASM_C
, exp
->ordinal
);
599 fprintf (f
, "anames:\n");
600 for (i
= 0, exp
= d_exports
; exp
; i
++, exp
= exp
->next
)
602 fprintf (f
, "\t%s 0\t%sNoname\n", ASM_LONG
, ASM_C
);
604 fprintf (f
, "\t%s n%d\n", ASM_LONG
, i
);
606 fprintf (f
, "anords:\n");
607 for (exp
= d_exports
; exp
; exp
= exp
->next
)
608 fprintf (f
, "\t%s %d\n", ASM_SHORT
, exp
->ordinal
- d_ord
);
610 for (i
= 0, exp
= d_exports
; exp
; i
++, exp
= exp
->next
)
612 fprintf (f
, "@n%d: %s \"%s\"\n", i
, ASM_TEXT
, exp
->name
);
614 fprintf (f
, "n%d: %s \"%s\"\n", i
, ASM_TEXT
, exp
->name
);
619 fprintf(f
,"\t.section .drectve\n");
620 for (dl
= a_list
; dl
; dl
= dl
->next
)
622 fprintf (f
,"\t%s\t\"%s\"\n", ASM_TEXT
, dl
->text
);
627 fprintf(f
,"\t.section .rdata\n");
628 for (dl
= d_list
; dl
; dl
= dl
->next
)
632 /* We dont output as ascii 'cause there can
633 be quote characters in the string */
636 for (p
= dl
->text
; *p
; p
++) {
638 fprintf(f
,"\t%s\t", ASM_BYTE
);
656 /**********************************************************************/
659 char outfile
[PATHMAX
];
664 sprintf (outfile
, "%s.lib.s", outfile_prefix
);
666 f
= fopen (outfile
, "w");
669 fprintf (stderr
, "Unable to open output file %s\n", outfile
);
675 fprintf (f
, "\t.text\n");
676 fprintf (f
, "%s Thunk table\n", ASM_C
);
677 for (i
= 0, exp
= d_exports
; exp
; i
++, exp
= exp
->next
)
679 fprintf (f
, "\t%s\t%s\n", ASM_GLOBAL
, exp
->name
);
680 fprintf (f
, "\t%s\t__imp__%s\n", ASM_GLOBAL
, exp
->name
);
683 for (i
= 0, exp
= d_exports
; exp
; i
++, exp
= exp
->next
)
685 fprintf (f
, "%s:\t%s\t__imp__%s\n", exp
->name
, ASM_JUMP
, exp
->name
);
689 fprintf (f
, "%s IMAGE_IMPORT_DESCRIPTOR\n", ASM_C
);
690 fprintf (f
, "\t.section .idata$2\n");
691 fprintf (f
, "\t%s\thname\t%s Ptr to image import by name list\n", ASM_LONG
, ASM_C
);
692 fprintf (f
, "\t%s\t%d\t%s time\n", ASM_LONG
, time (0), ASM_C
);
693 fprintf (f
, "\t%s\t0\t%s Forwarder chain\n", ASM_LONG
, ASM_C
);
694 fprintf (f
, "\t%s\tiname\t%s imported dll's name\n", ASM_LONG
, ASM_C
);
695 fprintf (f
, "\t%s\tfthunk\t%s pointer to firstthunk\n", ASM_LONG
, ASM_C
);
697 fprintf (f
, "\n%s Loader modifies this\n", ASM_C
);
698 fprintf (f
, "\t.section .idata$5\n");
699 fprintf (f
, "fthunk:\n");
700 for (i
= 0, exp
= d_exports
; exp
; i
++, exp
= exp
->next
)
702 fprintf (f
, "__imp__%s:\n", exp
->name
);
703 fprintf (f
, "\t%s\tID%d\n", ASM_LONG
, i
);
706 fprintf (f
, "\n%s Hint name array\n", ASM_C
);
707 fprintf (f
, "\t.section .idata$4\n");
708 fprintf (f
, "hname:\n");
709 for (i
= 0, exp
= d_exports
; exp
; i
++, exp
= exp
->next
)
711 fprintf (f
, "\t%s\tID%d\n", ASM_LONG
, i
);
714 fprintf (f
, "%s Hint/name array storage and import dll name\n", ASM_C
);
715 fprintf (f
, "\t.section .idata$6\n");
717 for (i
= 0, exp
= d_exports
; exp
; i
++, exp
= exp
->next
)
719 fprintf (f
, "ID%d:\t%s\t%d\n", i
, ASM_SHORT
, exp
->ordinal
);
720 fprintf (f
, "\t%s\t\"%s\"\n", ASM_TEXT
, exp
->name
);
723 fprintf (f
, "iname:\t%s\t\"%s.%s\"\n", ASM_TEXT
, outfile_prefix
, d_suffix
);
726 /**********************************************************************/
728 /* Run through the information gathered from the .o files and the
729 .def file and work out the best stuff */
735 export_type
*ap
= *(export_type
**) a
;
736 export_type
*bp
= *(export_type
**) b
;
737 if (ap
->ordinal
== bp
->ordinal
)
740 /* unset ordinals go to the bottom */
741 if (ap
->ordinal
== -1)
743 if (bp
->ordinal
== -1)
745 return (ap
->ordinal
- bp
->ordinal
);
754 export_type
*ap
= *(export_type
**) a
;
755 export_type
*bp
= *(export_type
**) b
;
757 return (strcmp (ap
->name
, bp
->name
));
762 remove_null_names (ptr
)
767 for (dst
= src
= 0; src
< d_nfuncs
; src
++)
784 for (i
= 0; i
< d_nfuncs
; i
++)
788 printf ("%d %s @ %d %s%s\n",
789 i
, ptr
[i
]->name
, ptr
[i
]->ordinal
,
790 ptr
[i
]->noname
? "NONAME " : "",
791 ptr
[i
]->constant
? "CONSTANT" : "");
800 process_duplicates (d_export_vec
)
801 export_type
**d_export_vec
;
808 /* Remove duplicates */
809 qsort (d_export_vec
, d_nfuncs
, sizeof (export_type
*), nfunc
);
812 for (i
= 0; i
< d_nfuncs
- 1; i
++)
814 if (strcmp (d_export_vec
[i
]->name
,
815 d_export_vec
[i
+ 1]->name
) == 0)
818 export_type
*a
= d_export_vec
[i
];
819 export_type
*b
= d_export_vec
[i
+ 1];
823 fprintf (stderr
, "warning, ignoring duplicate EXPORT %s\n",
829 fprintf (stderr
, "Error, duplicate EXPORT with oridinals %s\n",
833 /* Merge attributes */
834 b
->ordinal
= a
->ordinal
> 0 ? a
->ordinal
: b
->ordinal
;
835 b
->constant
|= a
->constant
;
836 b
->noname
|= a
->noname
;
841 remove_null_names (d_export_vec
);
848 fill_ordinals (d_export_vec
)
849 export_type
**d_export_vec
;
852 qsort (d_export_vec
, d_nfuncs
, sizeof (export_type
*), pfunc
);
854 /* fill in the unset ordinals with ones from the minimum */
855 for (i
= 0; i
< d_nfuncs
; i
++)
857 if (d_export_vec
[i
]->ordinal
== -1)
859 d_export_vec
[i
]->ordinal
= lowest
++;
863 if (lowest
== d_export_vec
[i
]->ordinal
)
865 fprintf (stderr
, "Warning, Duplicate ordinal %s @ %d\n",
866 d_export_vec
[i
]->name
,
867 d_export_vec
[i
]->ordinal
);
869 lowest
= d_export_vec
[i
]->ordinal
+ 1;
873 /* Work out the lowest ordinal number */
875 d_ord
= d_export_vec
[0]->ordinal
;
880 /* First work out the minimum ordinal chosen */
885 export_type
**d_export_vec
886 = (export_type
**) xmalloc (sizeof (export_type
*) * d_nfuncs
);
888 for (i
= 0, exp
= d_exports
; exp
; i
++, exp
= exp
->next
)
890 d_export_vec
[i
] = exp
;
893 process_duplicates (d_export_vec
);
894 fill_ordinals (d_export_vec
);
896 /* Put back the list in the new order */
898 for (i
= d_nfuncs
- 1; i
>= 0; i
--)
900 d_export_vec
[i
]->next
= d_exports
;
901 d_exports
= d_export_vec
[i
];
906 /**********************************************************************/
913 fprintf (file
, "Usage %s [-m|--machine machine] [-o outprefix] [-d|--def deffile] [--def deffile]\n", program_name
);
917 static struct option long_options
[] =
919 {"def", required_argument
, NULL
, 'd'},
920 {"help", no_argument
, NULL
, 'h'},
921 {"machine", required_argument
, NULL
, 'm'},
932 program_name
= av
[0];
935 while ((c
= getopt_long (ac
, av
, "h?m:o:Dd:", long_options
, 0)) != EOF
)
947 outfile_prefix
= optarg
;
961 for (i
= 0; mtable
[i
].type
; i
++)
963 if (strcmp (mtable
[i
].type
, mname
) == 0)
969 fprintf(stderr
,"Machine not supported\n");
978 process_def_file (def_file
);
983 firstarg
= av
[optind
];
984 scan_obj_file (av
[optind
]);
991 outfile_prefix
= d_name
;
993 outfile_prefix
= def_file
;
995 outfile_prefix
= firstarg
;
998 fprintf (stderr
, "No way to create an output filename\n");
1002 outfile_prefix
= prefix (outfile_prefix
);
This page took 0.051486 seconds and 4 git commands to generate.