Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winedump/lib.c
4389 views
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
23
#include <stdarg.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <fcntl.h>
27
28
#include "windef.h"
29
#include "winbase.h"
30
#include "winnt.h"
31
32
#include "winedump.h"
33
34
static inline USHORT ushort_bswap(USHORT s)
35
{
36
return (s >> 8) | (s << 8);
37
}
38
39
static inline UINT ulong_bswap(UINT l)
40
{
41
return ((UINT)ushort_bswap((USHORT)l) << 16) | ushort_bswap(l >> 16);
42
}
43
44
static void dump_import_object(const IMPORT_OBJECT_HEADER *ioh)
45
{
46
if (ioh->Version == 0)
47
{
48
static const char * const obj_type[] = { "code", "data", "const" };
49
static const char * const name_type[] = { "ordinal", "name", "no prefix", "undecorate", "export as" };
50
const char *name, *dll_name;
51
52
printf(" Version : %X\n", ioh->Version);
53
printf(" Machine : %X (%s)\n", ioh->Machine, get_machine_str(ioh->Machine));
54
printf(" TimeDateStamp: %08X %s\n", (UINT)ioh->TimeDateStamp, get_time_str(ioh->TimeDateStamp));
55
printf(" SizeOfData : %08X\n", (UINT)ioh->SizeOfData);
56
name = (const char *)ioh + sizeof(*ioh);
57
dll_name = name + strlen(name) + 1;
58
printf(" DLL name : %s\n", dll_name);
59
printf(" Symbol name : %s\n", name);
60
printf(" Type : %s\n", (ioh->Type < ARRAY_SIZE(obj_type)) ? obj_type[ioh->Type] : "unknown");
61
printf(" Name type : %s\n", (ioh->NameType < ARRAY_SIZE(name_type)) ? name_type[ioh->NameType] : "unknown");
62
if (ioh->NameType == IMPORT_OBJECT_NAME_EXPORTAS)
63
printf(" Export name : %s\n", dll_name + strlen(dll_name) + 1);
64
printf(" %-13s: %u\n", (ioh->NameType == IMPORT_OBJECT_ORDINAL) ? "Ordinal" : "Hint", ioh->Ordinal);
65
printf("\n");
66
}
67
}
68
69
static void dump_long_import(const void *base, const IMAGE_SECTION_HEADER *ish, unsigned num_sect)
70
{
71
unsigned i;
72
const DWORD *imp_data5 = NULL;
73
const WORD *imp_data6 = NULL;
74
75
if (globals.do_dumpheader)
76
printf("Section Table\n");
77
78
for (i = 0; i < num_sect; i++)
79
{
80
if (globals.do_dumpheader)
81
dump_section(&ish[i], NULL);
82
83
if (globals.do_dump_rawdata)
84
{
85
dump_data((const unsigned char *)base + ish[i].PointerToRawData, ish[i].SizeOfRawData, " " );
86
printf("\n");
87
}
88
89
if (!strcmp((const char *)ish[i].Name, ".idata$5"))
90
{
91
imp_data5 = (const DWORD *)((const char *)base + ish[i].PointerToRawData);
92
}
93
else if (!strcmp((const char *)ish[i].Name, ".idata$6"))
94
{
95
imp_data6 = (const WORD *)((const char *)base + ish[i].PointerToRawData);
96
}
97
else if (globals.do_debug && !strcmp((const char *)ish[i].Name, ".debug$S"))
98
{
99
const char *imp_debugS = (const char *)base + ish[i].PointerToRawData;
100
101
codeview_dump_symbols(imp_debugS, 0, ish[i].SizeOfRawData);
102
printf("\n");
103
}
104
}
105
106
if (imp_data5)
107
{
108
WORD ordinal = 0;
109
const char *name = NULL;
110
111
if (imp_data5[0] & 0x80000000)
112
ordinal = (WORD)(imp_data5[0] & ~0x80000000);
113
114
if (imp_data6)
115
{
116
if (!ordinal) ordinal = imp_data6[0];
117
name = (const char *)(imp_data6 + 1);
118
}
119
else
120
{
121
/* FIXME: find out a name in the section's data */
122
}
123
124
if (ordinal)
125
{
126
printf(" Symbol name : %s\n", name ? name : "(ordinal import) /* FIXME */");
127
printf(" %-13s: %u\n", (imp_data5[0] & 0x80000000) ? "Ordinal" : "Hint", ordinal);
128
printf("\n");
129
}
130
}
131
}
132
133
enum FileSig get_kind_lib(void)
134
{
135
const char* arch = PRD(0, IMAGE_ARCHIVE_START_SIZE);
136
if (arch && !strncmp(arch, IMAGE_ARCHIVE_START, IMAGE_ARCHIVE_START_SIZE))
137
return SIG_COFFLIB;
138
return SIG_UNKNOWN;
139
}
140
141
void lib_dump(void)
142
{
143
BOOL first_linker_member = TRUE;
144
unsigned long cur_file_pos, long_names_size = 0;
145
const IMAGE_ARCHIVE_MEMBER_HEADER *iamh;
146
const char *long_names = NULL;
147
148
cur_file_pos = IMAGE_ARCHIVE_START_SIZE;
149
150
for (;;)
151
{
152
const IMPORT_OBJECT_HEADER *ioh;
153
unsigned long size;
154
155
if (!(iamh = PRD(cur_file_pos, sizeof(*iamh)))) break;
156
157
if (globals.do_dumpheader)
158
{
159
printf("Archive member name at %08lx\n", Offset(iamh));
160
161
printf("Name %.16s", iamh->Name);
162
if (!strncmp((const char *)iamh->Name, IMAGE_ARCHIVE_LINKER_MEMBER, sizeof(iamh->Name)))
163
{
164
printf(" - %s archive linker member\n", first_linker_member ? "1st" : "2nd");
165
}
166
else
167
{
168
if (long_names && iamh->Name[0] == '/')
169
{
170
unsigned long long_names_offset = atol((const char *)&iamh->Name[1]);
171
if (long_names_offset < long_names_size)
172
printf("%s\n", long_names + long_names_offset);
173
}
174
printf("\n");
175
}
176
printf("Date %.12s %s\n", iamh->Date, get_time_str(strtoul((const char *)iamh->Date, NULL, 10)));
177
printf("UserID %.6s\n", iamh->UserID);
178
printf("GroupID %.6s\n", iamh->GroupID);
179
printf("Mode %.8s\n", iamh->Mode);
180
printf("Size %.10s\n\n", iamh->Size);
181
}
182
183
cur_file_pos += sizeof(IMAGE_ARCHIVE_MEMBER_HEADER);
184
185
size = strtoul((const char *)iamh->Size, NULL, 10);
186
size = (size + 1) & ~1; /* align to an even address */
187
188
if (!(ioh = PRD(cur_file_pos, sizeof(*ioh)))) break;
189
190
if (ioh->Sig1 == IMAGE_FILE_MACHINE_UNKNOWN && ioh->Sig2 == IMPORT_OBJECT_HDR_SIG2)
191
{
192
dump_import_object(ioh);
193
}
194
else if (!strncmp((const char *)iamh->Name, IMAGE_ARCHIVE_LINKER_MEMBER, sizeof(iamh->Name)))
195
{
196
const UINT *offset = (const UINT *)ioh;
197
const char *name;
198
UINT i, count;
199
200
if (first_linker_member) /* 1st archive linker member, BE format */
201
{
202
count = ulong_bswap(*offset++);
203
name = (const char *)(offset + count);
204
printf("%u public symbols\n", count);
205
for (i = 0; i < count; i++)
206
{
207
printf("%8x %s\n", ulong_bswap(offset[i]), name);
208
name += strlen(name) + 1;
209
}
210
printf("\n");
211
}
212
else /* 2nd archive linker member, LE format */
213
{
214
const WORD *idx;
215
216
count = *offset++;
217
printf("%u offsets\n", count);
218
for (i = 0; i < count; i++)
219
{
220
printf("%8x %8x\n", i, offset[i]);
221
}
222
printf("\n");
223
224
offset += count;
225
count = *offset++;
226
idx = (const WORD *)offset;
227
name = (const char *)(idx + count);
228
printf("%u public symbols\n", count);
229
for (i = 0; i < count; i++)
230
{
231
printf("%8x %s\n", idx[i], name);
232
name += strlen(name) + 1;
233
}
234
printf("\n");
235
}
236
}
237
else if (!strncmp((const char *)iamh->Name, "/<ECSYMBOLS>/ ", sizeof(iamh->Name)))
238
{
239
unsigned int i, *count = (unsigned int *)ioh;
240
unsigned short *offsets = (unsigned short *)(count + 1);
241
const char *name = (const char *)(offsets + *count);
242
243
printf("%u EC symbols\n", *count);
244
for (i = 0; i < *count; i++)
245
{
246
printf("%8x %s\n", offsets[i], name);
247
name += strlen(name) + 1;
248
}
249
printf("\n");
250
}
251
else if (!strncmp((const char *)iamh->Name, IMAGE_ARCHIVE_LONGNAMES_MEMBER, sizeof(iamh->Name)))
252
{
253
long_names = PRD(cur_file_pos, size);
254
long_names_size = size;
255
}
256
else
257
{
258
unsigned long expected_size;
259
const IMAGE_FILE_HEADER *fh = (const IMAGE_FILE_HEADER *)ioh;
260
261
if (globals.do_dumpheader)
262
{
263
dump_file_header(fh, FALSE);
264
if (fh->SizeOfOptionalHeader)
265
{
266
const IMAGE_OPTIONAL_HEADER32 *oh = (const IMAGE_OPTIONAL_HEADER32 *)((const char *)fh + sizeof(*fh));
267
dump_optional_header(oh);
268
}
269
}
270
/* Sanity check */
271
expected_size = sizeof(*fh) + fh->SizeOfOptionalHeader + fh->NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
272
if (size > expected_size)
273
dump_long_import(fh, (const IMAGE_SECTION_HEADER *)((const char *)fh + sizeof(*fh) + fh->SizeOfOptionalHeader), fh->NumberOfSections);
274
}
275
276
first_linker_member = FALSE;
277
cur_file_pos += size;
278
}
279
}
280
281