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
34 static inline USHORT
ushort_bswap(USHORT s
)
36 return (s
>> 8) | (s
<< 8);
39 static inline UINT
ulong_bswap(UINT l
)
41 return ((UINT
)ushort_bswap((USHORT
)l
) << 16) | ushort_bswap(l
>> 16);
44 static void dump_import_object(const IMPORT_OBJECT_HEADER
*ioh
)
46 if (ioh
->Version
== 0)
48 static const char * const obj_type
[] = { "code", "data", "const" };
49 static const char * const name_type
[] = { "ordinal", "name", "no prefix", "undecorate" };
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 printf(" DLL name : %s\n", name
+ strlen(name
) + 1);
58 printf(" Symbol name : %s\n", name
);
59 printf(" Type : %s\n", (ioh
->Type
< ARRAY_SIZE(obj_type
)) ? obj_type
[ioh
->Type
] : "unknown");
60 printf(" Name type : %s\n", (ioh
->NameType
< ARRAY_SIZE(name_type
)) ? name_type
[ioh
->NameType
] : "unknown");
61 printf(" %-13s: %u\n", (ioh
->NameType
== IMPORT_OBJECT_ORDINAL
) ? "Ordinal" : "Hint", ioh
->Ordinal
);
66 static void dump_long_import(const void *base
, const IMAGE_SECTION_HEADER
*ish
, unsigned num_sect
)
69 const DWORD
*imp_data5
= NULL
;
70 const WORD
*imp_data6
= NULL
;
72 if (globals
.do_dumpheader
)
73 printf("Section Table\n");
75 for (i
= 0; i
< num_sect
; i
++)
77 if (globals
.do_dumpheader
)
78 dump_section(&ish
[i
], NULL
);
80 if (globals
.do_dump_rawdata
)
82 dump_data((const unsigned char *)base
+ ish
[i
].PointerToRawData
, ish
[i
].SizeOfRawData
, " " );
86 if (!strcmp((const char *)ish
[i
].Name
, ".idata$5"))
88 imp_data5
= (const DWORD
*)((const char *)base
+ ish
[i
].PointerToRawData
);
90 else if (!strcmp((const char *)ish
[i
].Name
, ".idata$6"))
92 imp_data6
= (const WORD
*)((const char *)base
+ ish
[i
].PointerToRawData
);
94 else if (globals
.do_debug
&& !strcmp((const char *)ish
[i
].Name
, ".debug$S"))
96 const char *imp_debugS
= (const char *)base
+ ish
[i
].PointerToRawData
;
98 codeview_dump_symbols(imp_debugS
, 0, ish
[i
].SizeOfRawData
);
106 const char *name
= NULL
;
108 if (imp_data5
[0] & 0x80000000)
109 ordinal
= (WORD
)(imp_data5
[0] & ~0x80000000);
113 if (!ordinal
) ordinal
= imp_data6
[0];
114 name
= (const char *)(imp_data6
+ 1);
118 /* FIXME: find out a name in the section's data */
123 printf(" Symbol name : %s\n", name
? name
: "(ordinal import) /* FIXME */");
124 printf(" %-13s: %u\n", (imp_data5
[0] & 0x80000000) ? "Ordinal" : "Hint", ordinal
);
130 enum FileSig
get_kind_lib(void)
132 const char* arch
= PRD(0, IMAGE_ARCHIVE_START_SIZE
);
133 if (arch
&& !strncmp(arch
, IMAGE_ARCHIVE_START
, IMAGE_ARCHIVE_START_SIZE
))
140 BOOL first_linker_member
= TRUE
;
141 unsigned long cur_file_pos
, long_names_size
= 0;
142 const IMAGE_ARCHIVE_MEMBER_HEADER
*iamh
;
143 const char *long_names
= NULL
;
145 cur_file_pos
= IMAGE_ARCHIVE_START_SIZE
;
149 const IMPORT_OBJECT_HEADER
*ioh
;
152 if (!(iamh
= PRD(cur_file_pos
, sizeof(*iamh
)))) break;
154 if (globals
.do_dumpheader
)
156 printf("Archive member name at %08lx\n", Offset(iamh
));
158 printf("Name %.16s", iamh
->Name
);
159 if (!strncmp((const char *)iamh
->Name
, IMAGE_ARCHIVE_LINKER_MEMBER
, sizeof(iamh
->Name
)))
161 printf(" - %s archive linker member\n", first_linker_member
? "1st" : "2nd");
165 if (long_names
&& iamh
->Name
[0] == '/')
167 unsigned long long_names_offset
= atol((const char *)&iamh
->Name
[1]);
168 if (long_names_offset
< long_names_size
)
169 printf("%s\n", long_names
+ long_names_offset
);
173 printf("Date %.12s %s\n", iamh
->Date
, get_time_str(strtoul((const char *)iamh
->Date
, NULL
, 10)));
174 printf("UserID %.6s\n", iamh
->UserID
);
175 printf("GroupID %.6s\n", iamh
->GroupID
);
176 printf("Mode %.8s\n", iamh
->Mode
);
177 printf("Size %.10s\n\n", iamh
->Size
);
180 cur_file_pos
+= sizeof(IMAGE_ARCHIVE_MEMBER_HEADER
);
182 size
= strtoul((const char *)iamh
->Size
, NULL
, 10);
183 size
= (size
+ 1) & ~1; /* align to an even address */
185 if (!(ioh
= PRD(cur_file_pos
, sizeof(*ioh
)))) break;
187 if (ioh
->Sig1
== IMAGE_FILE_MACHINE_UNKNOWN
&& ioh
->Sig2
== IMPORT_OBJECT_HDR_SIG2
)
189 dump_import_object(ioh
);
191 else if (!strncmp((const char *)iamh
->Name
, IMAGE_ARCHIVE_LINKER_MEMBER
, sizeof(iamh
->Name
)))
193 const UINT
*offset
= (const UINT
*)ioh
;
197 if (first_linker_member
) /* 1st archive linker member, BE format */
199 count
= ulong_bswap(*offset
++);
200 name
= (const char *)(offset
+ count
);
201 printf("%u public symbols\n", count
);
202 for (i
= 0; i
< count
; i
++)
204 printf("%8x %s\n", ulong_bswap(offset
[i
]), name
);
205 name
+= strlen(name
) + 1;
209 else /* 2nd archive linker member, LE format */
214 printf("%u offsets\n", count
);
215 for (i
= 0; i
< count
; i
++)
217 printf("%8x %8x\n", i
, offset
[i
]);
223 idx
= (const WORD
*)offset
;
224 name
= (const char *)(idx
+ count
);
225 printf("%u public symbols\n", count
);
226 for (i
= 0; i
< count
; i
++)
228 printf("%8x %s\n", idx
[i
], name
);
229 name
+= strlen(name
) + 1;
234 else if (!strncmp((const char *)iamh
->Name
, "/<ECSYMBOLS>/ ", sizeof(iamh
->Name
)))
236 unsigned int i
, *count
= (unsigned int *)ioh
;
237 unsigned short *offsets
= (unsigned short *)(count
+ 1);
238 const char *name
= (const char *)(offsets
+ *count
);
240 printf("%u EC symbols\n", *count
);
241 for (i
= 0; i
< *count
; i
++)
243 printf("%8x %s\n", offsets
[i
], name
);
244 name
+= strlen(name
) + 1;
248 else if (!strncmp((const char *)iamh
->Name
, IMAGE_ARCHIVE_LONGNAMES_MEMBER
, sizeof(iamh
->Name
)))
250 long_names
= PRD(cur_file_pos
, size
);
251 long_names_size
= size
;
255 unsigned long expected_size
;
256 const IMAGE_FILE_HEADER
*fh
= (const IMAGE_FILE_HEADER
*)ioh
;
258 if (globals
.do_dumpheader
)
260 dump_file_header(fh
, FALSE
);
261 if (fh
->SizeOfOptionalHeader
)
263 const IMAGE_OPTIONAL_HEADER32
*oh
= (const IMAGE_OPTIONAL_HEADER32
*)((const char *)fh
+ sizeof(*fh
));
264 dump_optional_header(oh
, fh
->SizeOfOptionalHeader
);
268 expected_size
= sizeof(*fh
) + fh
->SizeOfOptionalHeader
+ fh
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
);
269 if (size
> expected_size
)
270 dump_long_import(fh
, (const IMAGE_SECTION_HEADER
*)((const char *)fh
+ sizeof(*fh
) + fh
->SizeOfOptionalHeader
), fh
->NumberOfSections
);
273 first_linker_member
= FALSE
;
274 cur_file_pos
+= size
;