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
28 #define NONAMELESSUNION
36 static inline USHORT
ushort_bswap(USHORT s
)
38 return (s
>> 8) | (s
<< 8);
41 static inline ULONG
ulong_bswap(ULONG l
)
43 return ((ULONG
)ushort_bswap((USHORT
)l
) << 16) | ushort_bswap((USHORT
)(l
>> 16));
46 static void dump_import_object(const IMPORT_OBJECT_HEADER
*ioh
)
48 if (ioh
->Version
== 0)
50 static const char * const obj_type
[] = { "code", "data", "const" };
51 static const char * const name_type
[] = { "ordinal", "name", "no prefix", "undecorate" };
54 printf(" Version : %X\n", ioh
->Version
);
55 printf(" Machine : %X (%s)\n", ioh
->Machine
, get_machine_str(ioh
->Machine
));
56 printf(" TimeDateStamp: %08X %s\n", ioh
->TimeDateStamp
, get_time_str(ioh
->TimeDateStamp
));
57 printf(" SizeOfData : %08X\n", ioh
->SizeOfData
);
58 name
= (const char *)ioh
+ sizeof(*ioh
);
59 printf(" DLL name : %s\n", name
+ strlen(name
) + 1);
60 printf(" Symbol name : %s\n", name
);
61 printf(" Type : %s\n", (ioh
->Type
< ARRAY_SIZE(obj_type
)) ? obj_type
[ioh
->Type
] : "unknown");
62 printf(" Name type : %s\n", (ioh
->NameType
< ARRAY_SIZE(name_type
)) ? name_type
[ioh
->NameType
] : "unknown");
63 printf(" %-13s: %u\n", (ioh
->NameType
== IMPORT_OBJECT_ORDINAL
) ? "Ordinal" : "Hint", ioh
->u
.Ordinal
);
68 static void dump_long_import(const void *base
, const IMAGE_SECTION_HEADER
*ish
, unsigned num_sect
)
71 const DWORD
*imp_data5
= NULL
;
72 const WORD
*imp_data6
= NULL
;
74 if (globals
.do_dumpheader
)
75 printf("Section Table\n");
77 for (i
= 0; i
< num_sect
; i
++)
79 if (globals
.do_dumpheader
)
80 dump_section(&ish
[i
], NULL
);
82 if (globals
.do_dump_rawdata
)
84 dump_data((const unsigned char *)base
+ ish
[i
].PointerToRawData
, ish
[i
].SizeOfRawData
, " " );
88 if (!strcmp((const char *)ish
[i
].Name
, ".idata$5"))
90 imp_data5
= (const DWORD
*)((const char *)base
+ ish
[i
].PointerToRawData
);
92 else if (!strcmp((const char *)ish
[i
].Name
, ".idata$6"))
94 imp_data6
= (const WORD
*)((const char *)base
+ ish
[i
].PointerToRawData
);
96 else if (globals
.do_debug
&& !strcmp((const char *)ish
[i
].Name
, ".debug$S"))
98 const char *imp_debugS
= (const char *)base
+ ish
[i
].PointerToRawData
;
100 codeview_dump_symbols(imp_debugS
, 0, ish
[i
].SizeOfRawData
);
108 const char *name
= NULL
;
110 if (imp_data5
[0] & 0x80000000)
111 ordinal
= (WORD
)(imp_data5
[0] & ~0x80000000);
115 if (!ordinal
) ordinal
= imp_data6
[0];
116 name
= (const char *)(imp_data6
+ 1);
120 /* FIXME: find out a name in the section's data */
125 printf(" Symbol name : %s\n", name
? name
: "(ordinal import) /* FIXME */");
126 printf(" %-13s: %u\n", (imp_data5
[0] & 0x80000000) ? "Ordinal" : "Hint", ordinal
);
132 enum FileSig
get_kind_lib(void)
134 const char* arch
= PRD(0, IMAGE_ARCHIVE_START_SIZE
);
135 if (arch
&& !strncmp(arch
, IMAGE_ARCHIVE_START
, IMAGE_ARCHIVE_START_SIZE
))
142 BOOL first_linker_member
= TRUE
;
143 unsigned long cur_file_pos
, long_names_size
= 0;
144 const IMAGE_ARCHIVE_MEMBER_HEADER
*iamh
;
145 const char *long_names
= NULL
;
147 cur_file_pos
= IMAGE_ARCHIVE_START_SIZE
;
151 const IMPORT_OBJECT_HEADER
*ioh
;
154 if (!(iamh
= PRD(cur_file_pos
, sizeof(*iamh
)))) break;
156 if (globals
.do_dumpheader
)
158 printf("Archive member name at %08lx\n", Offset(iamh
));
160 printf("Name %.16s", iamh
->Name
);
161 if (!strncmp((const char *)iamh
->Name
, IMAGE_ARCHIVE_LINKER_MEMBER
, sizeof(iamh
->Name
)))
163 printf(" - %s archive linker member\n", first_linker_member
? "1st" : "2nd");
167 if (long_names
&& iamh
->Name
[0] == '/')
169 unsigned long long_names_offset
= atol((const char *)&iamh
->Name
[1]);
170 if (long_names_offset
< long_names_size
)
171 printf("%s\n", long_names
+ long_names_offset
);
175 printf("Date %.12s %s\n", iamh
->Date
, get_time_str(strtoul((const char *)iamh
->Date
, NULL
, 10)));
176 printf("UserID %.6s\n", iamh
->UserID
);
177 printf("GroupID %.6s\n", iamh
->GroupID
);
178 printf("Mode %.8s\n", iamh
->Mode
);
179 printf("Size %.10s\n\n", iamh
->Size
);
182 cur_file_pos
+= sizeof(IMAGE_ARCHIVE_MEMBER_HEADER
);
184 size
= strtoul((const char *)iamh
->Size
, NULL
, 10);
185 size
= (size
+ 1) & ~1; /* align to an even address */
187 if (!(ioh
= PRD(cur_file_pos
, sizeof(*ioh
)))) break;
189 if (ioh
->Sig1
== IMAGE_FILE_MACHINE_UNKNOWN
&& ioh
->Sig2
== IMPORT_OBJECT_HDR_SIG2
)
191 dump_import_object(ioh
);
193 else if (!strncmp((const char *)iamh
->Name
, IMAGE_ARCHIVE_LINKER_MEMBER
, sizeof(iamh
->Name
)))
195 const DWORD
*offset
= (const DWORD
*)ioh
;
199 if (first_linker_member
) /* 1st archive linker member, BE format */
201 count
= ulong_bswap(*offset
++);
202 name
= (const char *)(offset
+ count
);
203 printf("%u public symbols\n", count
);
204 for (i
= 0; i
< count
; i
++)
206 printf("%8x %s\n", ulong_bswap(offset
[i
]), name
);
207 name
+= strlen(name
) + 1;
211 else /* 2nd archive linker member, LE format */
216 printf("%u offsets\n", count
);
217 for (i
= 0; i
< count
; i
++)
219 printf("%8x %8x\n", i
, offset
[i
]);
225 idx
= (const WORD
*)offset
;
226 name
= (const char *)(idx
+ count
);
227 printf("%u public symbols\n", count
);
228 for (i
= 0; i
< count
; i
++)
230 printf("%8x %s\n", idx
[i
], name
);
231 name
+= strlen(name
) + 1;
236 else if (!strncmp((const char *)iamh
->Name
, IMAGE_ARCHIVE_LONGNAMES_MEMBER
, sizeof(iamh
->Name
)))
238 long_names
= PRD(cur_file_pos
, size
);
239 long_names_size
= size
;
243 unsigned long expected_size
;
244 const IMAGE_FILE_HEADER
*fh
= (const IMAGE_FILE_HEADER
*)ioh
;
246 if (globals
.do_dumpheader
)
248 dump_file_header(fh
);
249 if (fh
->SizeOfOptionalHeader
)
251 const IMAGE_OPTIONAL_HEADER32
*oh
= (const IMAGE_OPTIONAL_HEADER32
*)((const char *)fh
+ sizeof(*fh
));
252 dump_optional_header(oh
, fh
->SizeOfOptionalHeader
);
256 expected_size
= sizeof(*fh
) + fh
->SizeOfOptionalHeader
+ fh
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
);
257 if (size
> expected_size
)
258 dump_long_import(fh
, (const IMAGE_SECTION_HEADER
*)((const char *)fh
+ sizeof(*fh
) + fh
->SizeOfOptionalHeader
), fh
->NumberOfSections
);
261 first_linker_member
= FALSE
;
262 cur_file_pos
+= size
;