[zz]mingw中的dlltool工具的使用

/ This program allows you to build the files necessary to create
   DLLs to run on a system which understands PE format image files.
   (eg, Windows NT)

   See "Peering Inside the PE: A Tour of the Win32 Portable Executable
   File Format", MSJ 1994, Volume 9 for more information.
   Also see "Microsoft Portable Executable and Common Object File Format,
   Specification 4.1" for more information.

   A DLL contains an export table which contains the information
   which the runtime loader needs to tie up references from a
   referencing program.

   The export table is generated by this program by reading
   in a .DEF file or scanning the .a and .o files which will be in the
   DLL. A .o file can contain information in special ".drectve" sections
   with export information.

   A DEF file contains any number of the following commands:


   NAME <name> [ , <base> ]
   The result is going to be <name>.EXE

   LIBRARY <name> [ , <base> ]
   The result is going to be <name>.DLL

   EXPORTS ( ( ( <name1> [ = <name2> ] )
               | ( <name1> = <module-name> . <external-name>))
            [ @ <integer> ] [ NONAME ] [CONSTANT] [DATA] [PRIVATE] )
   Declares name1 as an exported symbol from the
   DLL, with optional ordinal number <integer>.
   Or declares name1 as an alias (forward) of the function <external-name>
   in the DLL <module-name>.

   IMPORTS ( (   <internal-name> =   <module-name> . <integer> )
             | ( [ <internal-name> = ] <module-name> . <external-name> ))
   Declares that <external-name> or the exported function whoes ordinal number
   is <integer> is to be imported from the file <module-name>. If
   <internal-name> is specified then this is the name that the imported
   function will be refereed to in the body of the DLL.

   DESCRIPTION <string>
   Puts <string> into output .exp file in the .rdata section

   [STACKSIZE|HEAPSIZE] <number-reserve> [ , <number-commit> ]
   Generates –stack|–heap <number-reserve>,<number-commit>
   in the output .drectve section. The linker will
   see this and act upon it.

   [CODE|DATA] <attr>+
   SECTIONS ( <sectionname> <attr>+ )
   <attr> = READ | WRITE | EXECUTE | SHARED
   Generates –attr <sectionname> <attr> in the output
   .drectve section. The linker will see this and act
   upon it.


   A -export:<name> in a .drectve section in an input .o or .a
   file to this program is equivalent to a EXPORTS <name>
   in a .DEF file.

   The program generates output files with the prefix supplied
   on the command line, or in the def file, or taken from the first
   supplied argument.

   The .exp.s file contains the information necessary to export
   the routines in the DLL. The .lib.s file contains the information
   necessary to use the DLL’s routines from a referencing program.

   Example:

file1.c:
   asm (".section .drectve");
   asm (".ascii "-export:adef"");

   void adef (char s)
   {
     printf ("hello from the dll %sn", s);
   }

   void bdef (char s)
   {
     printf ("hello from the dll and the other entry point %sn", s);
   }

file2.c:
   asm (".section .drectve");
   asm (".ascii "-export:cdef"");
   asm (".ascii "-export:ddef"");

   void cdef (char s)
   {
     printf ("hello from the dll %sn", s);
   }

   void ddef (char s)
   {
     printf ("hello from the dll and the other entry point %sn", s);
   }

   int printf (void)
   {
     return 9;
   }

themain.c:
   int main (void)
   {
     cdef ();
     return 0;
   }

thedll.def

   LIBRARY thedll
   HEAPSIZE 0x40000, 0x2000
   EXPORTS bdef @ 20
           cdef @ 30 NONAME

   SECTIONS donkey READ WRITE
   aardvark EXECUTE

# Compile up the parts of the dll and the program

   gcc -c file1.c file2.c themain.c

# Optional: put the dll objects into a library
# (you don’t have to, you could name all the object
# files on the dlltool line)

   ar qcv thedll.in file1.o file2.o
   ranlib thedll.in

# Run this tool over the DLL’s .def file and generate an exports
# file (thedll.o) and an imports file (thedll.a).
# (You may have to use -S to tell dlltool where to find the assembler).

   dlltool –def thedll.def –output-exp thedll.exp –output-lib thedll.lib

# Build the dll with the library and the export table

   ld -o thedll.dll thedll.exp thedll.in

# Link the executable with the import library

   gcc -o themain.exe themain.o thedll.lib

This example can be extended if relocations are needed in the DLL:

# Compile up the parts of the dll and the program

   gcc -c file1.c file2.c themain.c

# Run this tool over the DLL’s .def file and generate an imports file.

   dlltool –def thedll.def –output-lib thedll.lib

# Link the executable with the import library and generate a base file
# at the same time

   gcc -o themain.exe themain.o thedll.lib -Wl,–base-file -Wl,themain.base

# Run this tool over the DLL’s .def file and generate an exports file
# which includes the relocations from the base file.

   dlltool –def thedll.def –base-file themain.base –output-exp thedll.exp

# Build the dll with file1.o, file2.o and the export table

   ld -o thedll.dll thedll.exp file1.o file2.o /

/ .idata section description

   The .idata section is the import table. It is a collection of several
   subsections used to keep the pieces for each dll together: .idata$[234567].
   IE: Each dll’s .idata$2’s are catenated together, each .idata$3’s, etc.

   .idata$2 = Import Directory Table
   = array of IMAGE_IMPORT_DESCRIPTOR’s.

DWORD   Import Lookup Table; - pointer to .idata$4
DWORD   TimeDateStamp;        - currently always 0
DWORD   ForwarderChain;       - currently always 0
DWORD   Name;                 - pointer to dll’s name
PIMAGE_THUNK_DATA FirstThunk; - pointer to .idata$5

   .idata$3 = null terminating entry for .idata$2.

   .idata$4 = Import Lookup Table
   = array of array of pointers to hint name table.
   There is one for each dll being imported from, and each dll’s set is
   terminated by a trailing NULL.

   .idata$5 = Import Address Table
   = array of array of pointers to hint name table.
   There is one for each dll being imported from, and each dll’s set is
   terminated by a trailing NULL.
   Initially, this table is identical to the Import Lookup Table. However,
   at load time, the loader overwrites the entries with the address of the
   function.

   .idata$6 = Hint Name Table
   = Array of { short, asciz } entries, one for each imported function.
   The `short’ is the function’s ordinal number.

   .idata$7 = dll name (eg: "kernel32.dll"). (.idata$6 for ppc). */