gdi32: Add a test for outline text metrics.
[wine] / tools / winedump / lib.c
1 /*
2  *  Dump a COFF library (lib) file
3  *
4  * Copyright 2006 Dmitry Timoshkov
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33 #ifdef HAVE_SYS_MMAN_H
34 #include <sys/mman.h>
35 #endif
36 #include <fcntl.h>
37
38 #define NONAMELESSUNION
39 #define NONAMELESSSTRUCT
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winnt.h"
43
44 #include "winedump.h"
45
46 static void dump_import_object(const IMPORT_OBJECT_HEADER *ioh)
47 {
48     if (ioh->Version >= 1)
49     {
50 #if 0 /* FIXME: supposed to handle uuid.lib but it doesn't */
51         const ANON_OBJECT_HEADER *aoh = (const ANON_OBJECT_HEADER *)ioh;
52
53         printf("CLSID {%08x-%04x-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X}",
54                aoh->ClassID.Data1, aoh->ClassID.Data2, aoh->ClassID.Data3,
55                aoh->ClassID.Data4[0], aoh->ClassID.Data4[1], aoh->ClassID.Data4[2], aoh->ClassID.Data4[3],
56                aoh->ClassID.Data4[4], aoh->ClassID.Data4[5], aoh->ClassID.Data4[6], aoh->ClassID.Data4[7]);
57 #endif
58     }
59     else
60     {
61         static const char * const obj_type[] = { "code", "data", "const" };
62         static const char * const name_type[] = { "ordinal", "name", "no prefix", "undecorate" };
63         const char *name;
64
65         printf("  Version      : %X\n", ioh->Version);
66         printf("  Machine      : %X (%s)\n", ioh->Machine, get_machine_str(ioh->Machine));
67         printf("  TimeDateStamp: %08X %s\n", ioh->TimeDateStamp, get_time_str(ioh->TimeDateStamp));
68         printf("  SizeOfData   : %08X\n", ioh->SizeOfData);
69         name = (const char *)ioh + sizeof(*ioh);
70         printf("  DLL name     : %s\n", name + strlen(name) + 1);
71         printf("  Symbol name  : %s\n", name);
72         printf("  Type         : %s\n", (ioh->Type < sizeof(obj_type)/sizeof(obj_type[0])) ? obj_type[ioh->Type] : "unknown");
73         printf("  Name type    : %s\n", (ioh->NameType < sizeof(name_type)/sizeof(name_type[0])) ? name_type[ioh->NameType] : "unknown");
74         printf("  %-13s: %u\n", (ioh->NameType == IMPORT_OBJECT_ORDINAL) ? "Ordinal" : "Hint", ioh->u.Ordinal);
75         printf("\n");
76     }
77 }
78
79 static void dump_long_import(const void *base, const IMAGE_SECTION_HEADER *ish, unsigned num_sect)
80 {
81     unsigned i;
82     const DWORD *imp_data5 = NULL;
83     const WORD *imp_data6 = NULL;
84
85     if (globals.do_dumpheader)
86         printf("Section Table\n");
87
88     for (i = 0; i < num_sect; i++)
89     {
90         if (globals.do_dumpheader)
91             dump_section(&ish[i]);
92
93         if (globals.do_dump_rawdata)
94         {
95             dump_data((const unsigned char *)base + ish[i].PointerToRawData, ish[i].SizeOfRawData, "    " );
96             printf("\n");
97         }
98
99         if (!strcmp((const char *)ish[i].Name, ".idata$5"))
100         {
101             imp_data5 = (const DWORD *)((const char *)base + ish[i].PointerToRawData);
102         }
103         else if (!strcmp((const char *)ish[i].Name, ".idata$6"))
104         {
105             imp_data6 = (const WORD *)((const char *)base + ish[i].PointerToRawData);
106         }
107         else if (globals.do_debug && !strcmp((const char *)ish[i].Name, ".debug$S"))
108         {
109             const char *imp_debugS = (const char *)base + ish[i].PointerToRawData;
110
111             codeview_dump_symbols(imp_debugS, ish[i].SizeOfRawData);
112             printf("\n");
113         }
114     }
115
116     if (imp_data5)
117     {
118         WORD ordinal = 0;
119         const char *name = NULL;
120
121         if (imp_data5[0] & 0x80000000)
122             ordinal = (WORD)(imp_data5[0] & ~0x80000000);
123
124         if (imp_data6)
125         {
126             if (!ordinal) ordinal = imp_data6[0];
127             name = (const char *)(imp_data6 + 1);
128         }
129         else
130         {
131             /* FIXME: find out a name in the section's data */
132         }
133
134         if (ordinal)
135         {
136             printf("  Symbol name  : %s\n", name ? name : "(ordinal import) /* FIXME */");
137             printf("  %-13s: %u\n", (imp_data5[0] & 0x80000000) ? "Ordinal" : "Hint", ordinal);
138             printf("\n");
139         }
140     }
141 }
142
143 enum FileSig get_kind_lib(void)
144 {
145     const char*         arch = PRD(0, IMAGE_ARCHIVE_START_SIZE);
146     if (arch && !strncmp(arch, IMAGE_ARCHIVE_START, IMAGE_ARCHIVE_START_SIZE))
147         return SIG_COFFLIB;
148     return SIG_UNKNOWN;
149 }
150
151 void lib_dump(void)
152 {
153     long cur_file_pos;
154     const IMAGE_ARCHIVE_MEMBER_HEADER *iamh;
155
156     cur_file_pos = IMAGE_ARCHIVE_START_SIZE;
157
158     for (;;)
159     {
160         const IMPORT_OBJECT_HEADER *ioh;
161         long size;
162
163         if (!(iamh = PRD(cur_file_pos, sizeof(*iamh)))) break;
164
165         if (globals.do_dumpheader)
166         {
167             printf("Archive member name at %08lx\n", Offset(iamh));
168
169             printf("Name %.16s", iamh->Name);
170             if (!strncmp((const char *)iamh->Name, IMAGE_ARCHIVE_LINKER_MEMBER, sizeof(iamh->Name)))
171             {
172                 printf(" - %s archive linker member\n",
173                        cur_file_pos == IMAGE_ARCHIVE_START_SIZE ? "1st" : "2nd");
174             }
175             else
176                 printf("\n");
177             printf("Date %.12s %s\n", iamh->Date, get_time_str(strtoul((const char *)iamh->Date, NULL, 10)));
178             printf("UserID %.6s\n", iamh->UserID);
179             printf("GroupID %.6s\n", iamh->GroupID);
180             printf("Mode %.8s\n", iamh->Mode);
181             printf("Size %.10s\n\n", iamh->Size);
182         }
183
184         cur_file_pos += sizeof(IMAGE_ARCHIVE_MEMBER_HEADER);
185
186         size = strtoul((const char *)iamh->Size, NULL, 10);
187         size = (size + 1) & ~1; /* align to an even address */
188
189         /* FIXME: only import library contents with the short format are
190          * recognized.
191          */
192         if (!(ioh = PRD(cur_file_pos, sizeof(*ioh)))) break;
193         if (ioh->Sig1 == IMAGE_FILE_MACHINE_UNKNOWN && ioh->Sig2 == IMPORT_OBJECT_HDR_SIG2)
194         {
195             dump_import_object(ioh);
196         }
197         else if (strncmp((const char *)iamh->Name, IMAGE_ARCHIVE_LINKER_MEMBER, sizeof(iamh->Name)))
198         {
199             long expected_size;
200             const IMAGE_FILE_HEADER *fh = (const IMAGE_FILE_HEADER *)ioh;
201
202             if (globals.do_dumpheader)
203             {
204                 dump_file_header(fh);
205                 if (fh->SizeOfOptionalHeader)
206                 {
207                     const IMAGE_OPTIONAL_HEADER32 *oh = (const IMAGE_OPTIONAL_HEADER32 *)((const char *)fh + sizeof(*fh));
208                     dump_optional_header(oh, fh->SizeOfOptionalHeader);
209                 }
210             }
211             /* Sanity check */
212             expected_size = sizeof(*fh) + fh->SizeOfOptionalHeader + fh->NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
213             if (size > expected_size)
214                 dump_long_import(fh, (const IMAGE_SECTION_HEADER *)((const char *)fh + sizeof(*fh) + fh->SizeOfOptionalHeader), fh->NumberOfSections);
215         }
216
217         cur_file_pos += size;
218     }
219 }