msvcp: Fixed complex division.
[wine.git] / tools / winedump / lib.c
blob6afef25ea1ddf15a43dcca9ff3f0ff8d7d0edd7b
1 /*
2 * Dump a COFF library (lib) file
4 * Copyright 2006 Dmitry Timoshkov
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
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>
38 #define NONAMELESSUNION
39 #define NONAMELESSSTRUCT
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winnt.h"
44 #include "winedump.h"
46 static inline USHORT ushort_bswap(USHORT s)
48 return (s >> 8) | (s << 8);
51 static inline ULONG ulong_bswap(ULONG l)
53 return ((ULONG)ushort_bswap((USHORT)l) << 16) | ushort_bswap((USHORT)(l >> 16));
56 static void dump_import_object(const IMPORT_OBJECT_HEADER *ioh)
58 if (ioh->Version == 0)
60 static const char * const obj_type[] = { "code", "data", "const" };
61 static const char * const name_type[] = { "ordinal", "name", "no prefix", "undecorate" };
62 const char *name;
64 printf(" Version : %X\n", ioh->Version);
65 printf(" Machine : %X (%s)\n", ioh->Machine, get_machine_str(ioh->Machine));
66 printf(" TimeDateStamp: %08X %s\n", ioh->TimeDateStamp, get_time_str(ioh->TimeDateStamp));
67 printf(" SizeOfData : %08X\n", ioh->SizeOfData);
68 name = (const char *)ioh + sizeof(*ioh);
69 printf(" DLL name : %s\n", name + strlen(name) + 1);
70 printf(" Symbol name : %s\n", name);
71 printf(" Type : %s\n", (ioh->Type < sizeof(obj_type)/sizeof(obj_type[0])) ? obj_type[ioh->Type] : "unknown");
72 printf(" Name type : %s\n", (ioh->NameType < sizeof(name_type)/sizeof(name_type[0])) ? name_type[ioh->NameType] : "unknown");
73 printf(" %-13s: %u\n", (ioh->NameType == IMPORT_OBJECT_ORDINAL) ? "Ordinal" : "Hint", ioh->u.Ordinal);
74 printf("\n");
78 static void dump_long_import(const void *base, const IMAGE_SECTION_HEADER *ish, unsigned num_sect)
80 unsigned i;
81 const DWORD *imp_data5 = NULL;
82 const WORD *imp_data6 = NULL;
84 if (globals.do_dumpheader)
85 printf("Section Table\n");
87 for (i = 0; i < num_sect; i++)
89 if (globals.do_dumpheader)
90 dump_section(&ish[i], NULL);
92 if (globals.do_dump_rawdata)
94 dump_data((const unsigned char *)base + ish[i].PointerToRawData, ish[i].SizeOfRawData, " " );
95 printf("\n");
98 if (!strcmp((const char *)ish[i].Name, ".idata$5"))
100 imp_data5 = (const DWORD *)((const char *)base + ish[i].PointerToRawData);
102 else if (!strcmp((const char *)ish[i].Name, ".idata$6"))
104 imp_data6 = (const WORD *)((const char *)base + ish[i].PointerToRawData);
106 else if (globals.do_debug && !strcmp((const char *)ish[i].Name, ".debug$S"))
108 const char *imp_debugS = (const char *)base + ish[i].PointerToRawData;
110 codeview_dump_symbols(imp_debugS, ish[i].SizeOfRawData);
111 printf("\n");
115 if (imp_data5)
117 WORD ordinal = 0;
118 const char *name = NULL;
120 if (imp_data5[0] & 0x80000000)
121 ordinal = (WORD)(imp_data5[0] & ~0x80000000);
123 if (imp_data6)
125 if (!ordinal) ordinal = imp_data6[0];
126 name = (const char *)(imp_data6 + 1);
128 else
130 /* FIXME: find out a name in the section's data */
133 if (ordinal)
135 printf(" Symbol name : %s\n", name ? name : "(ordinal import) /* FIXME */");
136 printf(" %-13s: %u\n", (imp_data5[0] & 0x80000000) ? "Ordinal" : "Hint", ordinal);
137 printf("\n");
142 enum FileSig get_kind_lib(void)
144 const char* arch = PRD(0, IMAGE_ARCHIVE_START_SIZE);
145 if (arch && !strncmp(arch, IMAGE_ARCHIVE_START, IMAGE_ARCHIVE_START_SIZE))
146 return SIG_COFFLIB;
147 return SIG_UNKNOWN;
150 void lib_dump(void)
152 int first_linker_member = 1;
153 unsigned long cur_file_pos, long_names_size = 0;
154 const IMAGE_ARCHIVE_MEMBER_HEADER *iamh;
155 const char *long_names = NULL;
157 cur_file_pos = IMAGE_ARCHIVE_START_SIZE;
159 for (;;)
161 const IMPORT_OBJECT_HEADER *ioh;
162 unsigned long size;
164 if (!(iamh = PRD(cur_file_pos, sizeof(*iamh)))) break;
166 if (globals.do_dumpheader)
168 printf("Archive member name at %08lx\n", Offset(iamh));
170 printf("Name %.16s", iamh->Name);
171 if (!strncmp((const char *)iamh->Name, IMAGE_ARCHIVE_LINKER_MEMBER, sizeof(iamh->Name)))
173 printf(" - %s archive linker member\n", first_linker_member ? "1st" : "2nd");
175 else
177 if (long_names && iamh->Name[0] == '/')
179 unsigned long long_names_offset = atol((const char *)&iamh->Name[1]);
180 if (long_names_offset < long_names_size)
181 printf("%s\n", long_names + long_names_offset);
183 printf("\n");
185 printf("Date %.12s %s\n", iamh->Date, get_time_str(strtoul((const char *)iamh->Date, NULL, 10)));
186 printf("UserID %.6s\n", iamh->UserID);
187 printf("GroupID %.6s\n", iamh->GroupID);
188 printf("Mode %.8s\n", iamh->Mode);
189 printf("Size %.10s\n\n", iamh->Size);
192 cur_file_pos += sizeof(IMAGE_ARCHIVE_MEMBER_HEADER);
194 size = strtoul((const char *)iamh->Size, NULL, 10);
195 size = (size + 1) & ~1; /* align to an even address */
197 if (!(ioh = PRD(cur_file_pos, sizeof(*ioh)))) break;
199 if (ioh->Sig1 == IMAGE_FILE_MACHINE_UNKNOWN && ioh->Sig2 == IMPORT_OBJECT_HDR_SIG2)
201 dump_import_object(ioh);
203 else if (!strncmp((const char *)iamh->Name, IMAGE_ARCHIVE_LINKER_MEMBER, sizeof(iamh->Name)))
205 const DWORD *offset = (const DWORD *)ioh;
206 const char *name;
207 DWORD i, count;
209 if (first_linker_member) /* 1st archive linker member, BE format */
211 count = ulong_bswap(*offset++);
212 name = (const char *)(offset + count);
213 printf("%u public symbols\n", count);
214 for (i = 0; i < count; i++)
216 printf("%8x %s\n", ulong_bswap(offset[i]), name);
217 name += strlen(name) + 1;
219 printf("\n");
221 else /* 2nd archive linker member, LE format */
223 const WORD *idx;
225 count = *offset++;
226 printf("%u offsets\n", count);
227 for (i = 0; i < count; i++)
229 printf("%8x %8x\n", i, offset[i]);
231 printf("\n");
233 offset += count;
234 count = *offset++;
235 idx = (const WORD *)offset;
236 name = (const char *)(idx + count);
237 printf("%u public symbols\n", count);
238 for (i = 0; i < count; i++)
240 printf("%8x %s\n", idx[i], name);
241 name += strlen(name) + 1;
243 printf("\n");
246 else if (!strncmp((const char *)iamh->Name, IMAGE_ARCHIVE_LONGNAMES_MEMBER, sizeof(iamh->Name)))
248 long_names = PRD(cur_file_pos, size);
249 long_names_size = size;
251 else
253 unsigned long expected_size;
254 const IMAGE_FILE_HEADER *fh = (const IMAGE_FILE_HEADER *)ioh;
256 if (globals.do_dumpheader)
258 dump_file_header(fh);
259 if (fh->SizeOfOptionalHeader)
261 const IMAGE_OPTIONAL_HEADER32 *oh = (const IMAGE_OPTIONAL_HEADER32 *)((const char *)fh + sizeof(*fh));
262 dump_optional_header(oh, fh->SizeOfOptionalHeader);
265 /* Sanity check */
266 expected_size = sizeof(*fh) + fh->SizeOfOptionalHeader + fh->NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
267 if (size > expected_size)
268 dump_long_import(fh, (const IMAGE_SECTION_HEADER *)((const char *)fh + sizeof(*fh) + fh->SizeOfOptionalHeader), fh->NumberOfSections);
271 first_linker_member = 0;
272 cur_file_pos += size;