pe.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472
  1. /*
  2. * PE dumping utility
  3. *
  4. * Copyright 2001 Eric Pouech
  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. #include "config.h"
  21. #include <stdlib.h>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <time.h>
  25. #include <fcntl.h>
  26. #define NONAMELESSUNION
  27. #define NONAMELESSSTRUCT
  28. #include "windef.h"
  29. #include "winbase.h"
  30. #include "winedump.h"
  31. #define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
  32. static const IMAGE_NT_HEADERS32* PE_nt_headers;
  33. static const char builtin_signature[] = "Wine builtin DLL";
  34. static const char fakedll_signature[] = "Wine placeholder DLL";
  35. static int is_builtin;
  36. const char *get_machine_str(int mach)
  37. {
  38. switch (mach)
  39. {
  40. case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
  41. case IMAGE_FILE_MACHINE_I860: return "i860";
  42. case IMAGE_FILE_MACHINE_I386: return "i386";
  43. case IMAGE_FILE_MACHINE_R3000: return "R3000";
  44. case IMAGE_FILE_MACHINE_R4000: return "R4000";
  45. case IMAGE_FILE_MACHINE_R10000: return "R10000";
  46. case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
  47. case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
  48. case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
  49. case IMAGE_FILE_MACHINE_IA64: return "IA64";
  50. case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
  51. case IMAGE_FILE_MACHINE_ARM: return "ARM";
  52. case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
  53. case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
  54. }
  55. return "???";
  56. }
  57. static const void* RVA(unsigned long rva, unsigned long len)
  58. {
  59. IMAGE_SECTION_HEADER* sectHead;
  60. int i;
  61. if (rva == 0) return NULL;
  62. sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
  63. for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
  64. {
  65. if (sectHead[i].VirtualAddress <= rva &&
  66. rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
  67. {
  68. /* return image import directory offset */
  69. return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
  70. }
  71. }
  72. return NULL;
  73. }
  74. static const IMAGE_NT_HEADERS32 *get_nt_header( void )
  75. {
  76. const IMAGE_DOS_HEADER *dos;
  77. dos = PRD(0, sizeof(*dos));
  78. if (!dos) return NULL;
  79. is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
  80. !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
  81. return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
  82. }
  83. void print_fake_dll( void )
  84. {
  85. const IMAGE_DOS_HEADER *dos;
  86. dos = PRD(0, sizeof(*dos) + 32);
  87. if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
  88. {
  89. if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
  90. printf( "*** This is a Wine builtin DLL ***\n\n" );
  91. else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
  92. printf( "*** This is a Wine fake DLL ***\n\n" );
  93. }
  94. }
  95. static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
  96. {
  97. if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
  98. {
  99. const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
  100. if (idx >= opt->NumberOfRvaAndSizes)
  101. return NULL;
  102. if(size)
  103. *size = opt->DataDirectory[idx].Size;
  104. return RVA(opt->DataDirectory[idx].VirtualAddress,
  105. opt->DataDirectory[idx].Size);
  106. }
  107. else
  108. {
  109. const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
  110. if (idx >= opt->NumberOfRvaAndSizes)
  111. return NULL;
  112. if(size)
  113. *size = opt->DataDirectory[idx].Size;
  114. return RVA(opt->DataDirectory[idx].VirtualAddress,
  115. opt->DataDirectory[idx].Size);
  116. }
  117. }
  118. static const void* get_dir(unsigned idx)
  119. {
  120. return get_dir_and_size(idx, 0);
  121. }
  122. static const char * const DirectoryNames[16] = {
  123. "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
  124. "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
  125. "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
  126. "IAT", "Delay IAT", "CLR Header", ""
  127. };
  128. static const char *get_magic_type(WORD magic)
  129. {
  130. switch(magic) {
  131. case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
  132. return "32bit";
  133. case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
  134. return "64bit";
  135. case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
  136. return "ROM";
  137. }
  138. return "???";
  139. }
  140. static inline void print_word(const char *title, WORD value)
  141. {
  142. printf(" %-34s 0x%-4X %u\n", title, value, value);
  143. }
  144. static inline void print_dword(const char *title, DWORD value)
  145. {
  146. printf(" %-34s 0x%-8x %u\n", title, value, value);
  147. }
  148. static inline void print_longlong(const char *title, ULONGLONG value)
  149. {
  150. printf(" %-34s 0x", title);
  151. if (sizeof(value) > sizeof(unsigned long) && value >> 32)
  152. printf("%lx%08lx\n", (unsigned long)(value >> 32), (unsigned long)value);
  153. else
  154. printf("%lx\n", (unsigned long)value);
  155. }
  156. static inline void print_ver(const char *title, BYTE major, BYTE minor)
  157. {
  158. printf(" %-34s %u.%02u\n", title, major, minor);
  159. }
  160. static inline void print_subsys(const char *title, WORD value)
  161. {
  162. const char *str;
  163. switch (value)
  164. {
  165. default:
  166. case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
  167. case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
  168. case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
  169. case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
  170. case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
  171. case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
  172. case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
  173. case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
  174. case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
  175. case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
  176. case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
  177. case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
  178. case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
  179. case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
  180. }
  181. printf(" %-34s 0x%X (%s)\n", title, value, str);
  182. }
  183. static inline void print_dllflags(const char *title, WORD value)
  184. {
  185. printf(" %-34s 0x%04X\n", title, value);
  186. #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
  187. if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
  188. X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
  189. X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
  190. X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
  191. X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
  192. X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
  193. X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
  194. X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
  195. X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
  196. X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
  197. X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
  198. X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
  199. #undef X
  200. }
  201. static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
  202. {
  203. unsigned i;
  204. printf("Data Directory\n");
  205. for (i = 0; i < n && i < 16; i++)
  206. {
  207. printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
  208. DirectoryNames[i], directory[i].VirtualAddress,
  209. directory[i].Size);
  210. }
  211. }
  212. static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
  213. {
  214. IMAGE_OPTIONAL_HEADER32 oh;
  215. const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
  216. /* in case optional header is missing or partial */
  217. memset(&oh, 0, sizeof(oh));
  218. memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
  219. optionalHeader = &oh;
  220. print_word("Magic", optionalHeader->Magic);
  221. print_ver("linker version",
  222. optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
  223. print_dword("size of code", optionalHeader->SizeOfCode);
  224. print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
  225. print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
  226. print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
  227. print_dword("base of code", optionalHeader->BaseOfCode);
  228. print_dword("base of data", optionalHeader->BaseOfData);
  229. print_dword("image base", optionalHeader->ImageBase);
  230. print_dword("section align", optionalHeader->SectionAlignment);
  231. print_dword("file align", optionalHeader->FileAlignment);
  232. print_ver("required OS version",
  233. optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
  234. print_ver("image version",
  235. optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
  236. print_ver("subsystem version",
  237. optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
  238. print_dword("Win32 Version", optionalHeader->Win32VersionValue);
  239. print_dword("size of image", optionalHeader->SizeOfImage);
  240. print_dword("size of headers", optionalHeader->SizeOfHeaders);
  241. print_dword("checksum", optionalHeader->CheckSum);
  242. print_subsys("Subsystem", optionalHeader->Subsystem);
  243. print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
  244. print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
  245. print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
  246. print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
  247. print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
  248. print_dword("loader flags", optionalHeader->LoaderFlags);
  249. print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
  250. printf("\n");
  251. print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
  252. printf("\n");
  253. }
  254. static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
  255. {
  256. IMAGE_OPTIONAL_HEADER64 oh;
  257. const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
  258. /* in case optional header is missing or partial */
  259. memset(&oh, 0, sizeof(oh));
  260. memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
  261. optionalHeader = &oh;
  262. print_word("Magic", optionalHeader->Magic);
  263. print_ver("linker version",
  264. optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
  265. print_dword("size of code", optionalHeader->SizeOfCode);
  266. print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
  267. print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
  268. print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
  269. print_dword("base of code", optionalHeader->BaseOfCode);
  270. print_longlong("image base", optionalHeader->ImageBase);
  271. print_dword("section align", optionalHeader->SectionAlignment);
  272. print_dword("file align", optionalHeader->FileAlignment);
  273. print_ver("required OS version",
  274. optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
  275. print_ver("image version",
  276. optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
  277. print_ver("subsystem version",
  278. optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
  279. print_dword("Win32 Version", optionalHeader->Win32VersionValue);
  280. print_dword("size of image", optionalHeader->SizeOfImage);
  281. print_dword("size of headers", optionalHeader->SizeOfHeaders);
  282. print_dword("checksum", optionalHeader->CheckSum);
  283. print_subsys("Subsystem", optionalHeader->Subsystem);
  284. print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
  285. print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
  286. print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
  287. print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
  288. print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
  289. print_dword("loader flags", optionalHeader->LoaderFlags);
  290. print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
  291. printf("\n");
  292. print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
  293. printf("\n");
  294. }
  295. void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
  296. {
  297. printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
  298. switch(optionalHeader->Magic) {
  299. case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
  300. dump_optional_header32(optionalHeader, header_size);
  301. break;
  302. case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
  303. dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
  304. break;
  305. default:
  306. printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
  307. break;
  308. }
  309. }
  310. void dump_file_header(const IMAGE_FILE_HEADER *fileHeader)
  311. {
  312. printf("File Header\n");
  313. printf(" Machine: %04X (%s)\n",
  314. fileHeader->Machine, get_machine_str(fileHeader->Machine));
  315. printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
  316. printf(" TimeDateStamp: %08X (%s) offset %lu\n",
  317. fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp),
  318. Offset(&(fileHeader->TimeDateStamp)));
  319. printf(" PointerToSymbolTable: %08X\n", fileHeader->PointerToSymbolTable);
  320. printf(" NumberOfSymbols: %08X\n", fileHeader->NumberOfSymbols);
  321. printf(" SizeOfOptionalHeader: %04X\n", fileHeader->SizeOfOptionalHeader);
  322. printf(" Characteristics: %04X\n", fileHeader->Characteristics);
  323. #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
  324. X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
  325. X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
  326. X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
  327. X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
  328. X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
  329. X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
  330. X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
  331. X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
  332. X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
  333. X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
  334. X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
  335. X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
  336. X(IMAGE_FILE_SYSTEM, "SYSTEM");
  337. X(IMAGE_FILE_DLL, "DLL");
  338. X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
  339. X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
  340. #undef X
  341. printf("\n");
  342. }
  343. static void dump_pe_header(void)
  344. {
  345. dump_file_header(&PE_nt_headers->FileHeader);
  346. dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader, PE_nt_headers->FileHeader.SizeOfOptionalHeader);
  347. }
  348. void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
  349. {
  350. unsigned offset;
  351. /* long section name ? */
  352. if (strtable && sectHead->Name[0] == '/' &&
  353. ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
  354. printf(" %.8s (%s)", sectHead->Name, strtable + offset);
  355. else
  356. printf(" %-8.8s", sectHead->Name);
  357. printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
  358. sectHead->Misc.VirtualSize, sectHead->VirtualAddress);
  359. printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
  360. sectHead->PointerToRawData, sectHead->SizeOfRawData);
  361. printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
  362. sectHead->PointerToRelocations, sectHead->NumberOfRelocations);
  363. printf(" line # offs: %-8u line #'s: %-8u\n",
  364. sectHead->PointerToLinenumbers, sectHead->NumberOfLinenumbers);
  365. printf(" characteristics: 0x%08x\n", sectHead->Characteristics);
  366. printf(" ");
  367. #define X(b,s) if (sectHead->Characteristics & b) printf(" " s)
  368. /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
  369. /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
  370. /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
  371. /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
  372. /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
  373. /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
  374. X(IMAGE_SCN_CNT_CODE, "CODE");
  375. X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
  376. X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
  377. X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
  378. X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
  379. /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
  380. X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
  381. X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
  382. /* 0x00002000 - Reserved */
  383. /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
  384. X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
  385. /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
  386. X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
  387. X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
  388. X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
  389. X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
  390. switch (sectHead->Characteristics & IMAGE_SCN_ALIGN_MASK)
  391. {
  392. #define X2(b,s) case b: printf(" " s); break
  393. X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
  394. X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
  395. X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
  396. X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
  397. X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
  398. X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
  399. X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
  400. X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
  401. X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
  402. X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
  403. X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
  404. X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
  405. X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
  406. X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
  407. #undef X2
  408. }
  409. X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
  410. X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
  411. X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
  412. X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
  413. X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
  414. X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
  415. X(IMAGE_SCN_MEM_READ, "MEM_READ");
  416. X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
  417. #undef X
  418. printf("\n\n");
  419. }
  420. static void dump_sections(const void *base, const void* addr, unsigned num_sect)
  421. {
  422. const IMAGE_SECTION_HEADER* sectHead = addr;
  423. unsigned i;
  424. const char* strtable;
  425. if (PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
  426. {
  427. strtable = (const char*)base +
  428. PE_nt_headers->FileHeader.PointerToSymbolTable +
  429. PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
  430. }
  431. else strtable = NULL;
  432. printf("Section Table\n");
  433. for (i = 0; i < num_sect; i++, sectHead++)
  434. {
  435. dump_section(sectHead, strtable);
  436. if (globals.do_dump_rawdata)
  437. {
  438. dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
  439. printf("\n");
  440. }
  441. }
  442. }
  443. static void dump_dir_exported_functions(void)
  444. {
  445. unsigned int size = 0;
  446. const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
  447. unsigned int i;
  448. const DWORD* pFunc;
  449. const DWORD* pName;
  450. const WORD* pOrdl;
  451. DWORD* funcs;
  452. if (!exportDir) return;
  453. printf("Exports table:\n");
  454. printf("\n");
  455. printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
  456. printf(" Characteristics: %08x\n", exportDir->Characteristics);
  457. printf(" TimeDateStamp: %08X %s\n",
  458. exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
  459. printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
  460. printf(" Ordinal base: %u\n", exportDir->Base);
  461. printf(" # of functions: %u\n", exportDir->NumberOfFunctions);
  462. printf(" # of Names: %u\n", exportDir->NumberOfNames);
  463. printf("Addresses of functions: %08X\n", exportDir->AddressOfFunctions);
  464. printf("Addresses of name ordinals: %08X\n", exportDir->AddressOfNameOrdinals);
  465. printf("Addresses of names: %08X\n", exportDir->AddressOfNames);
  466. printf("\n");
  467. printf(" Entry Pt Ordn Name\n");
  468. pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
  469. if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
  470. pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
  471. pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
  472. funcs = calloc( exportDir->NumberOfFunctions, sizeof(*funcs) );
  473. if (!funcs) fatal("no memory");
  474. for (i = 0; i < exportDir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
  475. for (i = 0; i < exportDir->NumberOfFunctions; i++)
  476. {
  477. if (!pFunc[i]) continue;
  478. printf(" %08X %5u ", pFunc[i], exportDir->Base + i);
  479. if (funcs[i])
  480. printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
  481. else
  482. printf("<by ordinal>");
  483. /* check for forwarded function */
  484. if ((const char *)RVA(pFunc[i],1) >= (const char *)exportDir &&
  485. (const char *)RVA(pFunc[i],1) < (const char *)exportDir + size)
  486. printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
  487. printf("\n");
  488. }
  489. free(funcs);
  490. printf("\n");
  491. }
  492. struct runtime_function_x86_64
  493. {
  494. DWORD BeginAddress;
  495. DWORD EndAddress;
  496. DWORD UnwindData;
  497. };
  498. struct runtime_function_armnt
  499. {
  500. DWORD BeginAddress;
  501. union {
  502. DWORD UnwindData;
  503. struct {
  504. DWORD Flag : 2;
  505. DWORD FunctionLength : 11;
  506. DWORD Ret : 2;
  507. DWORD H : 1;
  508. DWORD Reg : 3;
  509. DWORD R : 1;
  510. DWORD L : 1;
  511. DWORD C : 1;
  512. DWORD StackAdjust : 10;
  513. } DUMMYSTRUCTNAME;
  514. } DUMMYUNIONNAME;
  515. };
  516. struct runtime_function_arm64
  517. {
  518. DWORD BeginAddress;
  519. union
  520. {
  521. DWORD UnwindData;
  522. struct
  523. {
  524. DWORD Flag : 2;
  525. DWORD FunctionLength : 11;
  526. DWORD RegF : 3;
  527. DWORD RegI : 4;
  528. DWORD H : 1;
  529. DWORD CR : 2;
  530. DWORD FrameSize : 9;
  531. } DUMMYSTRUCTNAME;
  532. } DUMMYUNIONNAME;
  533. };
  534. union handler_data
  535. {
  536. struct runtime_function_x86_64 chain;
  537. DWORD handler;
  538. };
  539. struct opcode
  540. {
  541. BYTE offset;
  542. BYTE code : 4;
  543. BYTE info : 4;
  544. };
  545. struct unwind_info_x86_64
  546. {
  547. BYTE version : 3;
  548. BYTE flags : 5;
  549. BYTE prolog;
  550. BYTE count;
  551. BYTE frame_reg : 4;
  552. BYTE frame_offset : 4;
  553. struct opcode opcodes[1]; /* count entries */
  554. /* followed by union handler_data */
  555. };
  556. struct unwind_info_armnt
  557. {
  558. DWORD function_length : 18;
  559. DWORD version : 2;
  560. DWORD x : 1;
  561. DWORD e : 1;
  562. DWORD f : 1;
  563. DWORD count : 5;
  564. DWORD words : 4;
  565. };
  566. struct unwind_info_ext_armnt
  567. {
  568. WORD excount;
  569. BYTE exwords;
  570. BYTE reserved;
  571. };
  572. struct unwind_info_epilogue_armnt
  573. {
  574. DWORD offset : 18;
  575. DWORD res : 2;
  576. DWORD cond : 4;
  577. DWORD index : 8;
  578. };
  579. #define UWOP_PUSH_NONVOL 0
  580. #define UWOP_ALLOC_LARGE 1
  581. #define UWOP_ALLOC_SMALL 2
  582. #define UWOP_SET_FPREG 3
  583. #define UWOP_SAVE_NONVOL 4
  584. #define UWOP_SAVE_NONVOL_FAR 5
  585. #define UWOP_SAVE_XMM128 8
  586. #define UWOP_SAVE_XMM128_FAR 9
  587. #define UWOP_PUSH_MACHFRAME 10
  588. #define UNW_FLAG_EHANDLER 1
  589. #define UNW_FLAG_UHANDLER 2
  590. #define UNW_FLAG_CHAININFO 4
  591. static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
  592. {
  593. static const char * const reg_names[16] =
  594. { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
  595. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
  596. const union handler_data *handler_data;
  597. const struct unwind_info_x86_64 *info;
  598. unsigned int i, count;
  599. printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
  600. if (function->UnwindData & 1)
  601. {
  602. const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
  603. printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
  604. return;
  605. }
  606. info = RVA( function->UnwindData, sizeof(*info) );
  607. printf( " unwind info at %08x\n", function->UnwindData );
  608. if (info->version != 1)
  609. {
  610. printf( " *** unknown version %u\n", info->version );
  611. return;
  612. }
  613. printf( " flags %x", info->flags );
  614. if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
  615. if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
  616. if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
  617. printf( "\n prolog 0x%x bytes\n", info->prolog );
  618. if (info->frame_reg)
  619. printf( " frame register %s offset 0x%x(%%rsp)\n",
  620. reg_names[info->frame_reg], info->frame_offset * 16 );
  621. for (i = 0; i < info->count; i++)
  622. {
  623. printf( " 0x%02x: ", info->opcodes[i].offset );
  624. switch (info->opcodes[i].code)
  625. {
  626. case UWOP_PUSH_NONVOL:
  627. printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
  628. break;
  629. case UWOP_ALLOC_LARGE:
  630. if (info->opcodes[i].info)
  631. {
  632. count = *(const DWORD *)&info->opcodes[i+1];
  633. i += 2;
  634. }
  635. else
  636. {
  637. count = *(const USHORT *)&info->opcodes[i+1] * 8;
  638. i++;
  639. }
  640. printf( "sub $0x%x,%%rsp\n", count );
  641. break;
  642. case UWOP_ALLOC_SMALL:
  643. count = (info->opcodes[i].info + 1) * 8;
  644. printf( "sub $0x%x,%%rsp\n", count );
  645. break;
  646. case UWOP_SET_FPREG:
  647. printf( "lea 0x%x(%%rsp),%s\n",
  648. info->frame_offset * 16, reg_names[info->frame_reg] );
  649. break;
  650. case UWOP_SAVE_NONVOL:
  651. count = *(const USHORT *)&info->opcodes[i+1] * 8;
  652. printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
  653. i++;
  654. break;
  655. case UWOP_SAVE_NONVOL_FAR:
  656. count = *(const DWORD *)&info->opcodes[i+1];
  657. printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
  658. i += 2;
  659. break;
  660. case UWOP_SAVE_XMM128:
  661. count = *(const USHORT *)&info->opcodes[i+1] * 16;
  662. printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
  663. i++;
  664. break;
  665. case UWOP_SAVE_XMM128_FAR:
  666. count = *(const DWORD *)&info->opcodes[i+1];
  667. printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
  668. i += 2;
  669. break;
  670. case UWOP_PUSH_MACHFRAME:
  671. printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
  672. break;
  673. default:
  674. printf( "*** unknown code %u\n", info->opcodes[i].code );
  675. break;
  676. }
  677. }
  678. handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
  679. if (info->flags & UNW_FLAG_CHAININFO)
  680. {
  681. printf( " -> function %08x-%08x\n",
  682. handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
  683. return;
  684. }
  685. if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
  686. printf( " handler %08x data at %08x\n", handler_data->handler,
  687. (ULONG)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
  688. }
  689. static const BYTE armnt_code_lengths[256] =
  690. {
  691. /* 00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  692. /* 20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  693. /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  694. /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  695. /* 80 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  696. /* a0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  697. /* c0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  698. /* e0 */ 1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,4,3,4,1,1,1,1,1
  699. };
  700. static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
  701. {
  702. const struct unwind_info_armnt *info;
  703. const struct unwind_info_ext_armnt *infoex;
  704. const struct unwind_info_epilogue_armnt *infoepi;
  705. unsigned int rva;
  706. WORD i, count = 0, words = 0;
  707. if (fnc->u.s.Flag)
  708. {
  709. char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
  710. WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->u.s.StackAdjust;
  711. printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
  712. (fnc->BeginAddress & ~1) + fnc->u.s.FunctionLength * 2 );
  713. printf( " Flag %x\n", fnc->u.s.Flag );
  714. printf( " FunctionLength %x\n", fnc->u.s.FunctionLength );
  715. printf( " Ret %x\n", fnc->u.s.Ret );
  716. printf( " H %x\n", fnc->u.s.H );
  717. printf( " Reg %x\n", fnc->u.s.Reg );
  718. printf( " R %x\n", fnc->u.s.R );
  719. printf( " L %x\n", fnc->u.s.L );
  720. printf( " C %x\n", fnc->u.s.C );
  721. printf( " StackAdjust %x\n", fnc->u.s.StackAdjust );
  722. if (fnc->u.s.StackAdjust >= 0x03f4)
  723. {
  724. pf = fnc->u.s.StackAdjust & 0x04;
  725. ef = fnc->u.s.StackAdjust & 0x08;
  726. stack = (fnc->u.s.StackAdjust & 3) + 1;
  727. }
  728. if (!fnc->u.s.R || pf)
  729. {
  730. int first = 4, last = fnc->u.s.Reg + 4;
  731. if (pf)
  732. {
  733. first = (~fnc->u.s.StackAdjust) & 3;
  734. if (fnc->u.s.R)
  735. last = 3;
  736. }
  737. if (first == last)
  738. sprintf(intregs, "r%u", first);
  739. else
  740. sprintf(intregs, "r%u-r%u", first, last);
  741. fpoffset = last + 1 - first;
  742. }
  743. if (!fnc->u.s.R || ef)
  744. {
  745. int first = 4, last = fnc->u.s.Reg + 4;
  746. if (ef)
  747. {
  748. first = (~fnc->u.s.StackAdjust) & 3;
  749. if (fnc->u.s.R)
  750. last = 3;
  751. }
  752. if (first == last)
  753. sprintf(intregspop, "r%u", first);
  754. else
  755. sprintf(intregspop, "r%u-r%u", first, last);
  756. }
  757. if (fnc->u.s.C)
  758. {
  759. if (intregs[0])
  760. strcat(intregs, ", ");
  761. if (intregspop[0])
  762. strcat(intregspop, ", ");
  763. strcat(intregs, "r11");
  764. strcat(intregspop, "r11");
  765. }
  766. if (fnc->u.s.L)
  767. {
  768. if (intregs[0])
  769. strcat(intregs, ", ");
  770. strcat(intregs, "lr");
  771. if (intregspop[0] && (fnc->u.s.Ret != 0 || !fnc->u.s.H))
  772. strcat(intregspop, ", ");
  773. if (fnc->u.s.Ret != 0)
  774. strcat(intregspop, "lr");
  775. else if (!fnc->u.s.H)
  776. strcat(intregspop, "pc");
  777. }
  778. if (fnc->u.s.R)
  779. {
  780. if (fnc->u.s.Reg)
  781. sprintf(vfpregs, "d8-d%u", fnc->u.s.Reg + 8);
  782. else
  783. strcpy(vfpregs, "d8");
  784. }
  785. if (fnc->u.s.Flag == 1) {
  786. if (fnc->u.s.H)
  787. printf( " Unwind Code\tpush {r0-r3}\n" );
  788. if (intregs[0])
  789. printf( " Unwind Code\tpush {%s}\n", intregs );
  790. if (fnc->u.s.C && fpoffset == 0)
  791. printf( " Unwind Code\tmov r11, sp\n" );
  792. else if (fnc->u.s.C)
  793. printf( " Unwind Code\tadd r11, sp, #%d\n", fpoffset * 4 );
  794. if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
  795. printf( " Unwind Code\tvpush {%s}\n", vfpregs );
  796. if (stack && !pf)
  797. printf( " Unwind Code\tsub sp, sp, #%d\n", stack * 4 );
  798. }
  799. if (fnc->u.s.Ret == 3)
  800. return;
  801. printf( "Epilogue:\n" );
  802. if (stack && !ef)
  803. printf( " Unwind Code\tadd sp, sp, #%d\n", stack * 4 );
  804. if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
  805. printf( " Unwind Code\tvpop {%s}\n", vfpregs );
  806. if (intregspop[0])
  807. printf( " Unwind Code\tpop {%s}\n", intregspop );
  808. if (fnc->u.s.H && !(fnc->u.s.L && fnc->u.s.Ret == 0))
  809. printf( " Unwind Code\tadd sp, sp, #16\n" );
  810. else if (fnc->u.s.H && (fnc->u.s.L && fnc->u.s.Ret == 0))
  811. printf( " Unwind Code\tldr pc, [sp], #20\n" );
  812. if (fnc->u.s.Ret == 1)
  813. printf( " Unwind Code\tbx <reg>\n" );
  814. else if (fnc->u.s.Ret == 2)
  815. printf( " Unwind Code\tb <address>\n" );
  816. return;
  817. }
  818. info = RVA( fnc->u.UnwindData, sizeof(*info) );
  819. rva = fnc->u.UnwindData + sizeof(*info);
  820. count = info->count;
  821. words = info->words;
  822. printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
  823. (fnc->BeginAddress & ~1) + info->function_length * 2 );
  824. printf( " unwind info at %08x\n", fnc->u.UnwindData );
  825. printf( " Flag %x\n", fnc->u.s.Flag );
  826. printf( " FunctionLength %x\n", info->function_length );
  827. printf( " Version %x\n", info->version );
  828. printf( " X %x\n", info->x );
  829. printf( " E %x\n", info->e );
  830. printf( " F %x\n", info->f );
  831. printf( " Count %x\n", count );
  832. printf( " Words %x\n", words );
  833. if (!info->count && !info->words)
  834. {
  835. infoex = RVA( rva, sizeof(*infoex) );
  836. rva = rva + sizeof(*infoex);
  837. count = infoex->excount;
  838. words = infoex->exwords;
  839. printf( " ExtCount %x\n", count );
  840. printf( " ExtWords %x\n", words );
  841. }
  842. if (!info->e)
  843. {
  844. infoepi = RVA( rva, count * sizeof(*infoepi) );
  845. rva = rva + count * sizeof(*infoepi);
  846. for (i = 0; i < count; i++)
  847. {
  848. printf( " Epilogue Scope %x\n", i );
  849. printf( " Offset %x\n", infoepi[i].offset );
  850. printf( " Reserved %x\n", infoepi[i].res );
  851. printf( " Condition %x\n", infoepi[i].cond );
  852. printf( " Index %x\n", infoepi[i].index );
  853. }
  854. }
  855. else
  856. infoepi = NULL;
  857. if (words)
  858. {
  859. const unsigned int *codes;
  860. BYTE b, *bytes;
  861. BOOL inepilogue = FALSE;
  862. codes = RVA( rva, words * sizeof(*codes) );
  863. rva = rva + words * sizeof(*codes);
  864. bytes = (BYTE*)codes;
  865. for (b = 0; b < words * sizeof(*codes); b++)
  866. {
  867. BYTE code = bytes[b];
  868. BYTE len = armnt_code_lengths[code];
  869. if (info->e && b == count)
  870. {
  871. printf( "Epilogue:\n" );
  872. inepilogue = TRUE;
  873. }
  874. else if (!info->e && infoepi)
  875. {
  876. for (i = 0; i < count; i++)
  877. if (b == infoepi[i].index)
  878. {
  879. printf( "Epilogue from Scope %x at %08x:\n", i,
  880. (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
  881. inepilogue = TRUE;
  882. }
  883. }
  884. printf( " Unwind Code");
  885. for (i = 0; i < len; i++)
  886. printf( " %02x", bytes[b+i] );
  887. printf( "\t" );
  888. if (code == 0x00)
  889. printf( "\n" );
  890. else if (code <= 0x7f)
  891. printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
  892. else if (code <= 0xbf)
  893. {
  894. WORD excode, f;
  895. BOOL first = TRUE;
  896. BYTE excodes = bytes[++b];
  897. excode = (code << 8) | excodes;
  898. printf( "%s {", inepilogue ? "pop" : "push" );
  899. for (f = 0; f <= 12; f++)
  900. {
  901. if ((excode >> f) & 1)
  902. {
  903. printf( "%sr%u", first ? "" : ", ", f );
  904. first = FALSE;
  905. }
  906. }
  907. if (excode & 0x2000)
  908. printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
  909. printf( "}\n" );
  910. }
  911. else if (code <= 0xcf)
  912. if (inepilogue)
  913. printf( "mov sp, r%u\n", code & 0x0f );
  914. else
  915. printf( "mov r%u, sp\n", code & 0x0f );
  916. else if (code <= 0xd7)
  917. if (inepilogue)
  918. printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
  919. else
  920. printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
  921. else if (code <= 0xdf)
  922. if (inepilogue)
  923. printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
  924. else
  925. printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
  926. else if (code <= 0xe7)
  927. printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
  928. else if (code <= 0xeb)
  929. {
  930. WORD excode;
  931. BYTE excodes = bytes[++b];
  932. excode = (code << 8) | excodes;
  933. printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
  934. }
  935. else if (code <= 0xed)
  936. {
  937. WORD excode, f;
  938. BOOL first = TRUE;
  939. BYTE excodes = bytes[++b];
  940. excode = (code << 8) | excodes;
  941. printf( "%s {", inepilogue ? "pop" : "push" );
  942. for (f = 0; f < 8; f++)
  943. {
  944. if ((excode >> f) & 1)
  945. {
  946. printf( "%sr%u", first ? "" : ", ", f );
  947. first = FALSE;
  948. }
  949. }
  950. if (excode & 0x0100)
  951. printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
  952. printf( "}\n" );
  953. }
  954. else if (code == 0xee)
  955. printf( "unknown 16\n" );
  956. else if (code == 0xef)
  957. {
  958. WORD excode;
  959. BYTE excodes = bytes[++b];
  960. if (excodes <= 0x0f)
  961. {
  962. excode = (code << 8) | excodes;
  963. if (inepilogue)
  964. printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
  965. else
  966. printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
  967. }
  968. else
  969. printf( "unknown 32\n" );
  970. }
  971. else if (code <= 0xf4)
  972. printf( "unknown\n" );
  973. else if (code <= 0xf6)
  974. {
  975. WORD excode, offset = (code == 0xf6) ? 16 : 0;
  976. BYTE excodes = bytes[++b];
  977. excode = (code << 8) | excodes;
  978. printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
  979. ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
  980. }
  981. else if (code <= 0xf7)
  982. {
  983. unsigned int excode;
  984. BYTE excodes[2];
  985. excodes[0] = bytes[++b];
  986. excodes[1] = bytes[++b];
  987. excode = (code << 16) | (excodes[0] << 8) | excodes[1];
  988. printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
  989. }
  990. else if (code <= 0xf8)
  991. {
  992. unsigned int excode;
  993. BYTE excodes[3];
  994. excodes[0] = bytes[++b];
  995. excodes[1] = bytes[++b];
  996. excodes[2] = bytes[++b];
  997. excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
  998. printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
  999. }
  1000. else if (code <= 0xf9)
  1001. {
  1002. unsigned int excode;
  1003. BYTE excodes[2];
  1004. excodes[0] = bytes[++b];
  1005. excodes[1] = bytes[++b];
  1006. excode = (code << 16) | (excodes[0] << 8) | excodes[1];
  1007. printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
  1008. }
  1009. else if (code <= 0xfa)
  1010. {
  1011. unsigned int excode;
  1012. BYTE excodes[3];
  1013. excodes[0] = bytes[++b];
  1014. excodes[1] = bytes[++b];
  1015. excodes[2] = bytes[++b];
  1016. excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
  1017. printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
  1018. }
  1019. else if (code <= 0xfb)
  1020. printf( "nop\n" );
  1021. else if (code <= 0xfc)
  1022. printf( "nop.w\n" );
  1023. else if (code <= 0xfd)
  1024. {
  1025. printf( "(end) nop\n" );
  1026. inepilogue = TRUE;
  1027. }
  1028. else if (code <= 0xfe)
  1029. {
  1030. printf( "(end) nop.w\n" );
  1031. inepilogue = TRUE;
  1032. }
  1033. else
  1034. {
  1035. printf( "end\n" );
  1036. inepilogue = TRUE;
  1037. }
  1038. }
  1039. }
  1040. if (info->x)
  1041. {
  1042. const unsigned int *handler;
  1043. handler = RVA( rva, sizeof(*handler) );
  1044. rva = rva + sizeof(*handler);
  1045. printf( " handler %08x data at %08x\n", *handler, rva);
  1046. }
  1047. }
  1048. struct unwind_info_arm64
  1049. {
  1050. DWORD function_length : 18;
  1051. DWORD version : 2;
  1052. DWORD x : 1;
  1053. DWORD e : 1;
  1054. DWORD epilog : 5;
  1055. DWORD codes : 5;
  1056. };
  1057. struct unwind_info_ext_arm64
  1058. {
  1059. WORD epilog;
  1060. BYTE codes;
  1061. BYTE reserved;
  1062. };
  1063. struct unwind_info_epilog_arm64
  1064. {
  1065. DWORD offset : 18;
  1066. DWORD res : 4;
  1067. DWORD index : 10;
  1068. };
  1069. static const BYTE code_lengths[256] =
  1070. {
  1071. /* 00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1072. /* 20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1073. /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1074. /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1075. /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1076. /* a0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1077. /* c0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  1078. /* e0 */ 4,1,2,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  1079. };
  1080. static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
  1081. {
  1082. unsigned int i, j;
  1083. for (i = 0; i < count; i += code_lengths[ptr[i]])
  1084. {
  1085. BYTE len = code_lengths[ptr[i]];
  1086. unsigned int val = ptr[i];
  1087. if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
  1088. else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
  1089. printf( " %04x: ", i );
  1090. for (j = 0; j < 4; j++)
  1091. if (j < len) printf( "%02x ", ptr[i+j] );
  1092. else printf( " " );
  1093. if (ptr[i] < 0x20) /* alloc_s */
  1094. {
  1095. printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
  1096. }
  1097. else if (ptr[i] < 0x40) /* save_r19r20_x */
  1098. {
  1099. printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
  1100. }
  1101. else if (ptr[i] < 0x80) /* save_fplr */
  1102. {
  1103. printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
  1104. }
  1105. else if (ptr[i] < 0xc0) /* save_fplr_x */
  1106. {
  1107. printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
  1108. }
  1109. else if (ptr[i] < 0xc8) /* alloc_m */
  1110. {
  1111. printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
  1112. }
  1113. else if (ptr[i] < 0xcc) /* save_regp */
  1114. {
  1115. int reg = 19 + ((val >> 6) & 0xf);
  1116. printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
  1117. }
  1118. else if (ptr[i] < 0xd0) /* save_regp_x */
  1119. {
  1120. int reg = 19 + ((val >> 6) & 0xf);
  1121. printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
  1122. }
  1123. else if (ptr[i] < 0xd4) /* save_reg */
  1124. {
  1125. int reg = 19 + ((val >> 6) & 0xf);
  1126. printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
  1127. }
  1128. else if (ptr[i] < 0xd6) /* save_reg_x */
  1129. {
  1130. int reg = 19 + ((val >> 5) & 0xf);
  1131. printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
  1132. }
  1133. else if (ptr[i] < 0xd8) /* save_lrpair */
  1134. {
  1135. int reg = 19 + 2 * ((val >> 6) & 0x7);
  1136. printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
  1137. }
  1138. else if (ptr[i] < 0xda) /* save_fregp */
  1139. {
  1140. int reg = 8 + ((val >> 6) & 0x7);
  1141. printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
  1142. }
  1143. else if (ptr[i] < 0xdc) /* save_fregp_x */
  1144. {
  1145. int reg = 8 + ((val >> 6) & 0x7);
  1146. printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
  1147. }
  1148. else if (ptr[i] < 0xde) /* save_freg */
  1149. {
  1150. int reg = 8 + ((val >> 6) & 0x7);
  1151. printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
  1152. }
  1153. else if (ptr[i] == 0xde) /* save_freg_x */
  1154. {
  1155. int reg = 8 + ((val >> 5) & 0x7);
  1156. printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
  1157. }
  1158. else if (ptr[i] == 0xe0) /* alloc_l */
  1159. {
  1160. printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
  1161. }
  1162. else if (ptr[i] == 0xe1) /* set_fp */
  1163. {
  1164. printf( "mov x29,sp\n" );
  1165. }
  1166. else if (ptr[i] == 0xe2) /* add_fp */
  1167. {
  1168. printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
  1169. }
  1170. else if (ptr[i] == 0xe3) /* nop */
  1171. {
  1172. printf( "nop\n" );
  1173. }
  1174. else if (ptr[i] == 0xe4) /* end */
  1175. {
  1176. printf( "end\n" );
  1177. }
  1178. else if (ptr[i] == 0xe5) /* end_c */
  1179. {
  1180. printf( "end_c\n" );
  1181. }
  1182. else if (ptr[i] == 0xe6) /* save_next */
  1183. {
  1184. printf( "save_next\n" );
  1185. }
  1186. else if (ptr[i] == 0xe7) /* arithmetic */
  1187. {
  1188. switch ((val >> 4) & 0x0f)
  1189. {
  1190. case 0: printf( "add lr,lr,x28\n" ); break;
  1191. case 1: printf( "add lr,lr,sp\n" ); break;
  1192. case 2: printf( "sub lr,lr,x28\n" ); break;
  1193. case 3: printf( "sub lr,lr,sp\n" ); break;
  1194. case 4: printf( "eor lr,lr,x28\n" ); break;
  1195. case 5: printf( "eor lr,lr,sp\n" ); break;
  1196. case 6: printf( "rol lr,lr,neg x28\n" ); break;
  1197. case 8: printf( "ror lr,lr,x28\n" ); break;
  1198. case 9: printf( "ror lr,lr,sp\n" ); break;
  1199. default:printf( "unknown op\n" ); break;
  1200. }
  1201. }
  1202. else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
  1203. {
  1204. printf( "MSFT_OP_TRAP_FRAME\n" );
  1205. }
  1206. else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
  1207. {
  1208. printf( "MSFT_OP_MACHINE_FRAME\n" );
  1209. }
  1210. else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
  1211. {
  1212. printf( "MSFT_OP_CONTEXT\n" );
  1213. }
  1214. else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
  1215. {
  1216. printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
  1217. }
  1218. else printf( "??\n");
  1219. }
  1220. }
  1221. static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
  1222. {
  1223. int i, pos = 0, intsz = func->u.s.RegI * 8, fpsz = func->u.s.RegF * 8, savesz, locsz;
  1224. if (func->u.s.CR == 1) intsz += 8;
  1225. if (func->u.s.RegF) fpsz += 8;
  1226. savesz = ((intsz + fpsz + 8 * 8 * func->u.s.H) + 0xf) & ~0xf;
  1227. locsz = func->u.s.FrameSize * 16 - savesz;
  1228. switch (func->u.s.CR)
  1229. {
  1230. case 3:
  1231. printf( " %04x: mov x29,sp\n", pos++ );
  1232. if (locsz <= 512)
  1233. {
  1234. printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
  1235. break;
  1236. }
  1237. printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
  1238. /* fall through */
  1239. case 0:
  1240. case 1:
  1241. if (locsz <= 4080)
  1242. {
  1243. printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
  1244. }
  1245. else
  1246. {
  1247. printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
  1248. printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
  1249. }
  1250. break;
  1251. }
  1252. if (func->u.s.H)
  1253. {
  1254. printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
  1255. printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
  1256. printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
  1257. printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
  1258. }
  1259. if (func->u.s.RegF)
  1260. {
  1261. if (func->u.s.RegF % 2 == 0)
  1262. printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->u.s.RegF, intsz + fpsz - 8 );
  1263. for (i = (func->u.s.RegF - 1)/ 2; i >= 0; i--)
  1264. {
  1265. if (!i && !intsz)
  1266. printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
  1267. else
  1268. printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
  1269. }
  1270. }
  1271. switch (func->u.s.RegI)
  1272. {
  1273. case 0:
  1274. if (func->u.s.CR == 1)
  1275. printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
  1276. break;
  1277. case 1:
  1278. if (func->u.s.CR == 1)
  1279. printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
  1280. else
  1281. printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
  1282. break;
  1283. default:
  1284. if (func->u.s.RegI % 2)
  1285. {
  1286. if (func->u.s.CR == 1)
  1287. printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->u.s.RegI, 8 * func->u.s.RegI - 8 );
  1288. else
  1289. printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->u.s.RegI, 8 * func->u.s.RegI - 8 );
  1290. }
  1291. else if (func->u.s.CR == 1)
  1292. printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
  1293. for (i = func->u.s.RegI / 2 - 1; i >= 0; i--)
  1294. if (i)
  1295. printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
  1296. else
  1297. printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
  1298. break;
  1299. }
  1300. printf( " %04x: end\n", pos );
  1301. }
  1302. static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
  1303. {
  1304. const struct unwind_info_arm64 *info;
  1305. const struct unwind_info_ext_arm64 *infoex;
  1306. const struct unwind_info_epilog_arm64 *infoepi;
  1307. const BYTE *ptr;
  1308. unsigned int i, rva, codes, epilogs;
  1309. if (func->u.s.Flag)
  1310. {
  1311. printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
  1312. func->BeginAddress + func->u.s.FunctionLength * 4 );
  1313. printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
  1314. func->u.s.FunctionLength, func->u.s.Flag, func->u.s.RegF, func->u.s.RegI,
  1315. func->u.s.H, func->u.s.CR, func->u.s.FrameSize );
  1316. dump_arm64_packed_info( func );
  1317. return;
  1318. }
  1319. rva = func->u.UnwindData;
  1320. info = RVA( rva, sizeof(*info) );
  1321. rva += sizeof(*info);
  1322. epilogs = info->epilog;
  1323. codes = info->codes;
  1324. if (!codes)
  1325. {
  1326. infoex = RVA( rva, sizeof(*infoex) );
  1327. rva = rva + sizeof(*infoex);
  1328. codes = infoex->codes;
  1329. epilogs = infoex->epilog;
  1330. }
  1331. printf( "\nFunction %08x-%08x:\n",
  1332. func->BeginAddress, func->BeginAddress + info->function_length * 4 );
  1333. printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
  1334. info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
  1335. if (info->e)
  1336. {
  1337. printf( " epilog 0: code=%04x\n", info->epilog );
  1338. }
  1339. else
  1340. {
  1341. infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
  1342. rva += sizeof(*infoepi) * epilogs;
  1343. for (i = 0; i < epilogs; i++)
  1344. printf( " epilog %u: pc=%08x code=%04x\n", i,
  1345. func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
  1346. }
  1347. ptr = RVA( rva, codes * 4);
  1348. rva += codes * 4;
  1349. if (info->x)
  1350. {
  1351. const DWORD *handler = RVA( rva, sizeof(*handler) );
  1352. rva += sizeof(*handler);
  1353. printf( " handler: %08x data %08x\n", *handler, rva );
  1354. }
  1355. dump_arm64_codes( ptr, codes * 4 );
  1356. }
  1357. static void dump_dir_exceptions(void)
  1358. {
  1359. unsigned int i, size = 0;
  1360. const void *funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
  1361. const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
  1362. if (!funcs) return;
  1363. switch (file_header->Machine)
  1364. {
  1365. case IMAGE_FILE_MACHINE_AMD64:
  1366. size /= sizeof(struct runtime_function_x86_64);
  1367. printf( "Exception info (%u functions):\n", size );
  1368. for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
  1369. break;
  1370. case IMAGE_FILE_MACHINE_ARMNT:
  1371. size /= sizeof(struct runtime_function_armnt);
  1372. printf( "Exception info (%u functions):\n", size );
  1373. for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
  1374. break;
  1375. case IMAGE_FILE_MACHINE_ARM64:
  1376. size /= sizeof(struct runtime_function_arm64);
  1377. printf( "Exception info (%u functions):\n", size );
  1378. for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
  1379. break;
  1380. default:
  1381. printf( "Exception information not supported for %s binaries\n",
  1382. get_machine_str(file_header->Machine));
  1383. break;
  1384. }
  1385. }
  1386. static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, DWORD thunk_rva)
  1387. {
  1388. /* FIXME: This does not properly handle large images */
  1389. const IMAGE_IMPORT_BY_NAME* iibn;
  1390. for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
  1391. {
  1392. if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
  1393. printf(" %08x %4u <by ordinal>\n", thunk_rva, (DWORD)IMAGE_ORDINAL64(il->u1.Ordinal));
  1394. else
  1395. {
  1396. iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
  1397. if (!iibn)
  1398. printf("Can't grab import by name info, skipping to next ordinal\n");
  1399. else
  1400. printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
  1401. }
  1402. }
  1403. }
  1404. static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, DWORD thunk_rva)
  1405. {
  1406. const IMAGE_IMPORT_BY_NAME* iibn;
  1407. for (; il->u1.Ordinal; il++, thunk_rva += sizeof(DWORD))
  1408. {
  1409. if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
  1410. printf(" %08x %4u <by ordinal>\n", thunk_rva, IMAGE_ORDINAL32(il->u1.Ordinal));
  1411. else
  1412. {
  1413. iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
  1414. if (!iibn)
  1415. printf("Can't grab import by name info, skipping to next ordinal\n");
  1416. else
  1417. printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
  1418. }
  1419. }
  1420. }
  1421. static void dump_dir_imported_functions(void)
  1422. {
  1423. unsigned directorySize;
  1424. const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
  1425. if (!importDesc) return;
  1426. printf("Import Table size: %08x\n", directorySize);/* FIXME */
  1427. for (;;)
  1428. {
  1429. const IMAGE_THUNK_DATA32* il;
  1430. if (!importDesc->Name || !importDesc->FirstThunk) break;
  1431. printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
  1432. printf(" Hint/Name Table: %08X\n", (DWORD)importDesc->u.OriginalFirstThunk);
  1433. printf(" TimeDateStamp: %08X (%s)\n",
  1434. importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
  1435. printf(" ForwarderChain: %08X\n", importDesc->ForwarderChain);
  1436. printf(" First thunk RVA: %08X\n", (DWORD)importDesc->FirstThunk);
  1437. printf(" Thunk Ordn Name\n");
  1438. il = (importDesc->u.OriginalFirstThunk != 0) ?
  1439. RVA((DWORD)importDesc->u.OriginalFirstThunk, sizeof(DWORD)) :
  1440. RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
  1441. if (!il)
  1442. printf("Can't grab thunk data, going to next imported DLL\n");
  1443. else
  1444. {
  1445. if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
  1446. dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
  1447. else
  1448. dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
  1449. printf("\n");
  1450. }
  1451. importDesc++;
  1452. }
  1453. printf("\n");
  1454. }
  1455. static void dump_dir_loadconfig(void)
  1456. {
  1457. const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32 = get_dir(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG);
  1458. const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void*)loadcfg32;
  1459. if (!loadcfg32) return;
  1460. printf( "Loadconfig\n" );
  1461. print_dword( "Size", loadcfg32->Size );
  1462. print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
  1463. print_word( "MajorVersion", loadcfg32->MajorVersion );
  1464. print_word( "MinorVersion", loadcfg32->MinorVersion );
  1465. print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
  1466. print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
  1467. print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
  1468. if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
  1469. {
  1470. print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
  1471. print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
  1472. print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
  1473. print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
  1474. print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
  1475. print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
  1476. print_word( "CSDVersion", loadcfg64->CSDVersion );
  1477. print_word( "Reserved", loadcfg64->Reserved1 );
  1478. print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
  1479. print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
  1480. print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
  1481. }
  1482. else
  1483. {
  1484. print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
  1485. print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
  1486. print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
  1487. print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
  1488. print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
  1489. print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
  1490. print_word( "CSDVersion", loadcfg32->CSDVersion );
  1491. print_word( "Reserved", loadcfg32->Reserved1 );
  1492. print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
  1493. print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
  1494. print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
  1495. }
  1496. }
  1497. static void dump_dir_delay_imported_functions(void)
  1498. {
  1499. unsigned directorySize;
  1500. const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
  1501. if (!importDesc) return;
  1502. printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
  1503. for (;;)
  1504. {
  1505. const IMAGE_THUNK_DATA32* il;
  1506. int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
  1507. if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
  1508. printf(" grAttrs %08x offset %08lx %s\n", importDesc->Attributes.AllAttributes, Offset(importDesc),
  1509. (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
  1510. printf(" Hint/Name Table: %08x\n", importDesc->ImportNameTableRVA);
  1511. printf(" Address Table: %08x\n", importDesc->ImportAddressTableRVA);
  1512. printf(" TimeDateStamp: %08X (%s)\n",
  1513. importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
  1514. printf(" Thunk Ordn Name\n");
  1515. il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
  1516. if (!il)
  1517. printf("Can't grab thunk data, going to next imported DLL\n");
  1518. else
  1519. {
  1520. if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
  1521. dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
  1522. else
  1523. dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
  1524. printf("\n");
  1525. }
  1526. importDesc++;
  1527. }
  1528. printf("\n");
  1529. }
  1530. static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
  1531. {
  1532. const char* str;
  1533. printf("Directory %02u\n", idx + 1);
  1534. printf(" Characteristics: %08X\n", idd->Characteristics);
  1535. printf(" TimeDateStamp: %08X %s\n",
  1536. idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
  1537. printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
  1538. switch (idd->Type)
  1539. {
  1540. default:
  1541. case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
  1542. case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
  1543. case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
  1544. case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
  1545. case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
  1546. case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
  1547. case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
  1548. case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
  1549. case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
  1550. case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
  1551. case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
  1552. case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
  1553. case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
  1554. case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
  1555. case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
  1556. case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
  1557. case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
  1558. }
  1559. printf(" Type: %u (%s)\n", idd->Type, str);
  1560. printf(" SizeOfData: %u\n", idd->SizeOfData);
  1561. printf(" AddressOfRawData: %08X\n", idd->AddressOfRawData);
  1562. printf(" PointerToRawData: %08X\n", idd->PointerToRawData);
  1563. switch (idd->Type)
  1564. {
  1565. case IMAGE_DEBUG_TYPE_UNKNOWN:
  1566. break;
  1567. case IMAGE_DEBUG_TYPE_COFF:
  1568. dump_coff(idd->PointerToRawData, idd->SizeOfData,
  1569. IMAGE_FIRST_SECTION(PE_nt_headers));
  1570. break;
  1571. case IMAGE_DEBUG_TYPE_CODEVIEW:
  1572. dump_codeview(idd->PointerToRawData, idd->SizeOfData);
  1573. break;
  1574. case IMAGE_DEBUG_TYPE_FPO:
  1575. dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
  1576. break;
  1577. case IMAGE_DEBUG_TYPE_MISC:
  1578. {
  1579. const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
  1580. if (!misc) {printf("Can't get misc debug information\n"); break;}
  1581. printf(" DataType: %u (%s)\n",
  1582. misc->DataType,
  1583. (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
  1584. printf(" Length: %u\n", misc->Length);
  1585. printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
  1586. printf(" Data: %s\n", misc->Data);
  1587. }
  1588. break;
  1589. default: break;
  1590. }
  1591. printf("\n");
  1592. }
  1593. static void dump_dir_debug(void)
  1594. {
  1595. unsigned nb_dbg, i;
  1596. const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
  1597. nb_dbg /= sizeof(*debugDir);
  1598. if (!debugDir || !nb_dbg) return;
  1599. printf("Debug Table (%u directories)\n", nb_dbg);
  1600. for (i = 0; i < nb_dbg; i++)
  1601. {
  1602. dump_dir_debug_dir(debugDir, i);
  1603. debugDir++;
  1604. }
  1605. printf("\n");
  1606. }
  1607. static inline void print_clrflags(const char *title, DWORD value)
  1608. {
  1609. printf(" %-34s 0x%X\n", title, value);
  1610. #define X(f,s) if (value & f) printf(" %s\n", s)
  1611. X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
  1612. X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
  1613. X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
  1614. X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
  1615. X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
  1616. #undef X
  1617. }
  1618. static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
  1619. {
  1620. printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, dir->VirtualAddress, dir->Size);
  1621. }
  1622. static void dump_dir_clr_header(void)
  1623. {
  1624. unsigned int size = 0;
  1625. const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
  1626. if (!dir) return;
  1627. printf( "CLR Header\n" );
  1628. print_dword( "Header Size", dir->cb );
  1629. print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
  1630. print_clrflags( "Flags", dir->Flags );
  1631. print_dword( "EntryPointToken", dir->u.EntryPointToken );
  1632. printf("\n");
  1633. printf( "CLR Data Directory\n" );
  1634. print_clrdirectory( "MetaData", &dir->MetaData );
  1635. print_clrdirectory( "Resources", &dir->Resources );
  1636. print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
  1637. print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
  1638. print_clrdirectory( "VTableFixups", &dir->VTableFixups );
  1639. print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
  1640. print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
  1641. printf("\n");
  1642. }
  1643. static void dump_dir_reloc(void)
  1644. {
  1645. unsigned int i, size = 0;
  1646. const USHORT *relocs;
  1647. const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
  1648. const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
  1649. static const char * const names[] =
  1650. {
  1651. "BASED_ABSOLUTE",
  1652. "BASED_HIGH",
  1653. "BASED_LOW",
  1654. "BASED_HIGHLOW",
  1655. "BASED_HIGHADJ",
  1656. "BASED_MIPS_JMPADDR",
  1657. "BASED_SECTION",
  1658. "BASED_REL",
  1659. "unknown 8",
  1660. "BASED_IA64_IMM64",
  1661. "BASED_DIR64",
  1662. "BASED_HIGH3ADJ",
  1663. "unknown 12",
  1664. "unknown 13",
  1665. "unknown 14",
  1666. "unknown 15"
  1667. };
  1668. if (!rel) return;
  1669. printf( "Relocations\n" );
  1670. while (rel < end - 1 && rel->SizeOfBlock)
  1671. {
  1672. printf( " Page %x\n", rel->VirtualAddress );
  1673. relocs = (const USHORT *)(rel + 1);
  1674. i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
  1675. while (i--)
  1676. {
  1677. USHORT offset = *relocs & 0xfff;
  1678. int type = *relocs >> 12;
  1679. printf( " off %04x type %s\n", offset, names[type] );
  1680. relocs++;
  1681. }
  1682. rel = (const IMAGE_BASE_RELOCATION *)relocs;
  1683. }
  1684. printf("\n");
  1685. }
  1686. static void dump_dir_tls(void)
  1687. {
  1688. IMAGE_TLS_DIRECTORY64 dir;
  1689. const DWORD *callbacks;
  1690. const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
  1691. if (!pdir) return;
  1692. if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
  1693. memcpy(&dir, pdir, sizeof(dir));
  1694. else
  1695. {
  1696. dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
  1697. dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
  1698. dir.AddressOfIndex = pdir->AddressOfIndex;
  1699. dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
  1700. dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
  1701. dir.Characteristics = pdir->Characteristics;
  1702. }
  1703. /* FIXME: This does not properly handle large images */
  1704. printf( "Thread Local Storage\n" );
  1705. printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
  1706. (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
  1707. (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
  1708. (DWORD)dir.SizeOfZeroFill );
  1709. printf( " Index address %08x\n", (DWORD)dir.AddressOfIndex );
  1710. printf( " Characteristics %08x\n", dir.Characteristics );
  1711. printf( " Callbacks %08x -> {", (DWORD)dir.AddressOfCallBacks );
  1712. if (dir.AddressOfCallBacks)
  1713. {
  1714. DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
  1715. while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
  1716. {
  1717. printf( " %08x", *callbacks );
  1718. addr += sizeof(DWORD);
  1719. }
  1720. }
  1721. printf(" }\n\n");
  1722. }
  1723. enum FileSig get_kind_dbg(void)
  1724. {
  1725. const WORD* pw;
  1726. pw = PRD(0, sizeof(WORD));
  1727. if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
  1728. if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
  1729. return SIG_UNKNOWN;
  1730. }
  1731. void dbg_dump(void)
  1732. {
  1733. const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
  1734. unsigned nb_dbg;
  1735. unsigned i;
  1736. const IMAGE_DEBUG_DIRECTORY* debugDir;
  1737. separateDebugHead = PRD(0, sizeof(*separateDebugHead));
  1738. if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
  1739. printf ("Signature: %.2s (0x%4X)\n",
  1740. (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
  1741. printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
  1742. printf ("Machine: 0x%04X (%s)\n",
  1743. separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
  1744. printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
  1745. printf ("TimeDateStamp: 0x%08X (%s)\n",
  1746. separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
  1747. printf ("CheckSum: 0x%08X\n", separateDebugHead->CheckSum);
  1748. printf ("ImageBase: 0x%08X\n", separateDebugHead->ImageBase);
  1749. printf ("SizeOfImage: 0x%08X\n", separateDebugHead->SizeOfImage);
  1750. printf ("NumberOfSections: 0x%08X\n", separateDebugHead->NumberOfSections);
  1751. printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead->ExportedNamesSize);
  1752. printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead->DebugDirectorySize);
  1753. if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
  1754. separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
  1755. {printf("Can't get the sections, aborting\n"); return;}
  1756. dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
  1757. nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
  1758. debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
  1759. separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
  1760. separateDebugHead->ExportedNamesSize,
  1761. nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
  1762. if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
  1763. printf("Debug Table (%u directories)\n", nb_dbg);
  1764. for (i = 0; i < nb_dbg; i++)
  1765. {
  1766. dump_dir_debug_dir(debugDir, i);
  1767. debugDir++;
  1768. }
  1769. }
  1770. static const char *get_resource_type( unsigned int id )
  1771. {
  1772. static const char * const types[] =
  1773. {
  1774. NULL,
  1775. "CURSOR",
  1776. "BITMAP",
  1777. "ICON",
  1778. "MENU",
  1779. "DIALOG",
  1780. "STRING",
  1781. "FONTDIR",
  1782. "FONT",
  1783. "ACCELERATOR",
  1784. "RCDATA",
  1785. "MESSAGETABLE",
  1786. "GROUP_CURSOR",
  1787. NULL,
  1788. "GROUP_ICON",
  1789. NULL,
  1790. "VERSION",
  1791. "DLGINCLUDE",
  1792. NULL,
  1793. "PLUGPLAY",
  1794. "VXD",
  1795. "ANICURSOR",
  1796. "ANIICON",
  1797. "HTML",
  1798. "RT_MANIFEST"
  1799. };
  1800. if ((size_t)id < ARRAY_SIZE(types)) return types[id];
  1801. return NULL;
  1802. }
  1803. /* dump an ASCII string with proper escaping */
  1804. static int dump_strA( const unsigned char *str, size_t len )
  1805. {
  1806. static const char escapes[32] = ".......abtnvfr.............e....";
  1807. char buffer[256];
  1808. char *pos = buffer;
  1809. int count = 0;
  1810. for (; len; str++, len--)
  1811. {
  1812. if (pos > buffer + sizeof(buffer) - 8)
  1813. {
  1814. fwrite( buffer, pos - buffer, 1, stdout );
  1815. count += pos - buffer;
  1816. pos = buffer;
  1817. }
  1818. if (*str > 127) /* hex escape */
  1819. {
  1820. pos += sprintf( pos, "\\x%02x", *str );
  1821. continue;
  1822. }
  1823. if (*str < 32) /* octal or C escape */
  1824. {
  1825. if (!*str && len == 1) continue; /* do not output terminating NULL */
  1826. if (escapes[*str] != '.')
  1827. pos += sprintf( pos, "\\%c", escapes[*str] );
  1828. else if (len > 1 && str[1] >= '0' && str[1] <= '7')
  1829. pos += sprintf( pos, "\\%03o", *str );
  1830. else
  1831. pos += sprintf( pos, "\\%o", *str );
  1832. continue;
  1833. }
  1834. if (*str == '\\') *pos++ = '\\';
  1835. *pos++ = *str;
  1836. }
  1837. fwrite( buffer, pos - buffer, 1, stdout );
  1838. count += pos - buffer;
  1839. return count;
  1840. }
  1841. /* dump a Unicode string with proper escaping */
  1842. static int dump_strW( const WCHAR *str, size_t len )
  1843. {
  1844. static const char escapes[32] = ".......abtnvfr.............e....";
  1845. char buffer[256];
  1846. char *pos = buffer;
  1847. int count = 0;
  1848. for (; len; str++, len--)
  1849. {
  1850. if (pos > buffer + sizeof(buffer) - 8)
  1851. {
  1852. fwrite( buffer, pos - buffer, 1, stdout );
  1853. count += pos - buffer;
  1854. pos = buffer;
  1855. }
  1856. if (*str > 127) /* hex escape */
  1857. {
  1858. if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
  1859. pos += sprintf( pos, "\\x%04x", *str );
  1860. else
  1861. pos += sprintf( pos, "\\x%x", *str );
  1862. continue;
  1863. }
  1864. if (*str < 32) /* octal or C escape */
  1865. {
  1866. if (!*str && len == 1) continue; /* do not output terminating NULL */
  1867. if (escapes[*str] != '.')
  1868. pos += sprintf( pos, "\\%c", escapes[*str] );
  1869. else if (len > 1 && str[1] >= '0' && str[1] <= '7')
  1870. pos += sprintf( pos, "\\%03o", *str );
  1871. else
  1872. pos += sprintf( pos, "\\%o", *str );
  1873. continue;
  1874. }
  1875. if (*str == '\\') *pos++ = '\\';
  1876. *pos++ = *str;
  1877. }
  1878. fwrite( buffer, pos - buffer, 1, stdout );
  1879. count += pos - buffer;
  1880. return count;
  1881. }
  1882. /* dump data for a STRING resource */
  1883. static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
  1884. {
  1885. int i;
  1886. for (i = 0; i < 16 && size; i++)
  1887. {
  1888. unsigned len = *ptr++;
  1889. if (len >= size)
  1890. {
  1891. len = size;
  1892. size = 0;
  1893. }
  1894. else size -= len + 1;
  1895. if (len)
  1896. {
  1897. printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
  1898. dump_strW( ptr, len );
  1899. printf( "\"\n" );
  1900. ptr += len;
  1901. }
  1902. }
  1903. }
  1904. /* dump data for a MESSAGETABLE resource */
  1905. static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
  1906. {
  1907. const MESSAGE_RESOURCE_DATA *data = ptr;
  1908. const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
  1909. unsigned i, j;
  1910. for (i = 0; i < data->NumberOfBlocks; i++, block++)
  1911. {
  1912. const MESSAGE_RESOURCE_ENTRY *entry;
  1913. entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
  1914. for (j = block->LowId; j <= block->HighId; j++)
  1915. {
  1916. if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
  1917. {
  1918. const WCHAR *str = (const WCHAR *)entry->Text;
  1919. printf( "%s%08x L\"", prefix, j );
  1920. dump_strW( str, strlenW(str) );
  1921. printf( "\"\n" );
  1922. }
  1923. else
  1924. {
  1925. const char *str = (const char *) entry->Text;
  1926. printf( "%s%08x \"", prefix, j );
  1927. dump_strA( entry->Text, strlen(str) );
  1928. printf( "\"\n" );
  1929. }
  1930. entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
  1931. }
  1932. }
  1933. }
  1934. static void dump_dir_resource(void)
  1935. {
  1936. const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
  1937. const IMAGE_RESOURCE_DIRECTORY *namedir;
  1938. const IMAGE_RESOURCE_DIRECTORY *langdir;
  1939. const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
  1940. const IMAGE_RESOURCE_DIR_STRING_U *string;
  1941. const IMAGE_RESOURCE_DATA_ENTRY *data;
  1942. int i, j, k;
  1943. if (!root) return;
  1944. printf( "Resources:" );
  1945. for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
  1946. {
  1947. e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
  1948. namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s2.OffsetToDirectory);
  1949. for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
  1950. {
  1951. e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
  1952. langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s2.OffsetToDirectory);
  1953. for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
  1954. {
  1955. e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
  1956. printf( "\n " );
  1957. if (e1->u.s.NameIsString)
  1958. {
  1959. string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u.s.NameOffset);
  1960. dump_unicode_str( string->NameString, string->Length );
  1961. }
  1962. else
  1963. {
  1964. const char *type = get_resource_type( e1->u.Id );
  1965. if (type) printf( "%s", type );
  1966. else printf( "%04x", e1->u.Id );
  1967. }
  1968. printf( " Name=" );
  1969. if (e2->u.s.NameIsString)
  1970. {
  1971. string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u.s.NameOffset);
  1972. dump_unicode_str( string->NameString, string->Length );
  1973. }
  1974. else
  1975. printf( "%04x", e2->u.Id );
  1976. printf( " Language=%04x:\n", e3->u.Id );
  1977. data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
  1978. if (e1->u.s.NameIsString)
  1979. {
  1980. dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
  1981. }
  1982. else switch(e1->u.Id)
  1983. {
  1984. case 6:
  1985. dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->u.Id, " " );
  1986. break;
  1987. case 11:
  1988. dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
  1989. e2->u.Id, " " );
  1990. break;
  1991. default:
  1992. dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
  1993. break;
  1994. }
  1995. }
  1996. }
  1997. }
  1998. printf( "\n\n" );
  1999. }
  2000. static void dump_debug(void)
  2001. {
  2002. const char* stabs = NULL;
  2003. unsigned szstabs = 0;
  2004. const char* stabstr = NULL;
  2005. unsigned szstr = 0;
  2006. unsigned i;
  2007. const IMAGE_SECTION_HEADER* sectHead;
  2008. sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
  2009. for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
  2010. {
  2011. if (!strcmp((const char *)sectHead->Name, ".stab"))
  2012. {
  2013. stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
  2014. szstabs = sectHead->Misc.VirtualSize;
  2015. }
  2016. if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
  2017. {
  2018. stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
  2019. szstr = sectHead->Misc.VirtualSize;
  2020. }
  2021. }
  2022. if (stabs && stabstr)
  2023. dump_stabs(stabs, szstabs, stabstr, szstr);
  2024. }
  2025. static void dump_symbol_table(void)
  2026. {
  2027. const IMAGE_SYMBOL* sym;
  2028. int numsym;
  2029. numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
  2030. if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
  2031. return;
  2032. sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
  2033. sizeof(*sym) * numsym);
  2034. if (!sym) return;
  2035. dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
  2036. }
  2037. enum FileSig get_kind_exec(void)
  2038. {
  2039. const WORD* pw;
  2040. const DWORD* pdw;
  2041. const IMAGE_DOS_HEADER* dh;
  2042. pw = PRD(0, sizeof(WORD));
  2043. if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
  2044. if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
  2045. if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
  2046. {
  2047. /* the signature is the first DWORD */
  2048. pdw = PRD(dh->e_lfanew, sizeof(DWORD));
  2049. if (pdw)
  2050. {
  2051. if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
  2052. if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
  2053. if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
  2054. }
  2055. return SIG_DOS;
  2056. }
  2057. return SIG_UNKNOWN;
  2058. }
  2059. void pe_dump(void)
  2060. {
  2061. int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
  2062. PE_nt_headers = get_nt_header();
  2063. print_fake_dll();
  2064. if (globals.do_dumpheader)
  2065. {
  2066. dump_pe_header();
  2067. /* FIXME: should check ptr */
  2068. dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
  2069. PE_nt_headers->FileHeader.NumberOfSections);
  2070. }
  2071. else if (!globals.dumpsect)
  2072. {
  2073. /* show at least something here */
  2074. dump_pe_header();
  2075. }
  2076. if (globals.dumpsect)
  2077. {
  2078. if (all || !strcmp(globals.dumpsect, "import"))
  2079. {
  2080. dump_dir_imported_functions();
  2081. dump_dir_delay_imported_functions();
  2082. }
  2083. if (all || !strcmp(globals.dumpsect, "export"))
  2084. dump_dir_exported_functions();
  2085. if (all || !strcmp(globals.dumpsect, "debug"))
  2086. dump_dir_debug();
  2087. if (all || !strcmp(globals.dumpsect, "resource"))
  2088. dump_dir_resource();
  2089. if (all || !strcmp(globals.dumpsect, "tls"))
  2090. dump_dir_tls();
  2091. if (all || !strcmp(globals.dumpsect, "loadcfg"))
  2092. dump_dir_loadconfig();
  2093. if (all || !strcmp(globals.dumpsect, "clr"))
  2094. dump_dir_clr_header();
  2095. if (all || !strcmp(globals.dumpsect, "reloc"))
  2096. dump_dir_reloc();
  2097. if (all || !strcmp(globals.dumpsect, "except"))
  2098. dump_dir_exceptions();
  2099. }
  2100. if (globals.do_symbol_table)
  2101. dump_symbol_table();
  2102. if (globals.do_debug)
  2103. dump_debug();
  2104. }
  2105. typedef struct _dll_symbol {
  2106. size_t ordinal;
  2107. char *symbol;
  2108. } dll_symbol;
  2109. static dll_symbol *dll_symbols = NULL;
  2110. static dll_symbol *dll_current_symbol = NULL;
  2111. /* Compare symbols by ordinal for qsort */
  2112. static int symbol_cmp(const void *left, const void *right)
  2113. {
  2114. return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
  2115. }
  2116. /*******************************************************************
  2117. * dll_close
  2118. *
  2119. * Free resources used by DLL
  2120. */
  2121. /* FIXME: Not used yet
  2122. static void dll_close (void)
  2123. {
  2124. dll_symbol* ds;
  2125. if (!dll_symbols) {
  2126. fatal("No symbols");
  2127. }
  2128. for (ds = dll_symbols; ds->symbol; ds++)
  2129. free(ds->symbol);
  2130. free (dll_symbols);
  2131. dll_symbols = NULL;
  2132. }
  2133. */
  2134. static void do_grab_sym( void )
  2135. {
  2136. const IMAGE_EXPORT_DIRECTORY*exportDir;
  2137. unsigned i, j;
  2138. const DWORD* pName;
  2139. const DWORD* pFunc;
  2140. const WORD* pOrdl;
  2141. const char* ptr;
  2142. DWORD* map;
  2143. PE_nt_headers = get_nt_header();
  2144. if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
  2145. pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
  2146. if (!pName) {printf("Can't grab functions' name table\n"); return;}
  2147. pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
  2148. if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
  2149. pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
  2150. if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
  2151. /* dll_close(); */
  2152. dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
  2153. /* bit map of used funcs */
  2154. map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
  2155. if (!map) fatal("no memory");
  2156. for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
  2157. {
  2158. map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
  2159. ptr = RVA(*pName++, sizeof(DWORD));
  2160. if (!ptr) ptr = "cant_get_function";
  2161. dll_symbols[j].symbol = xstrdup(ptr);
  2162. dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
  2163. assert(dll_symbols[j].symbol);
  2164. }
  2165. for (i = 0; i < exportDir->NumberOfFunctions; i++)
  2166. {
  2167. if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
  2168. {
  2169. char ordinal_text[256];
  2170. /* Ordinal only entry */
  2171. sprintf (ordinal_text, "%s_%u",
  2172. globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
  2173. exportDir->Base + i);
  2174. str_toupper(ordinal_text);
  2175. dll_symbols[j].symbol = xstrdup(ordinal_text);
  2176. assert(dll_symbols[j].symbol);
  2177. dll_symbols[j].ordinal = exportDir->Base + i;
  2178. j++;
  2179. assert(j <= exportDir->NumberOfFunctions);
  2180. }
  2181. }
  2182. free(map);
  2183. if (NORMAL)
  2184. printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
  2185. exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
  2186. qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
  2187. dll_symbols[j].symbol = NULL;
  2188. dll_current_symbol = dll_symbols;
  2189. }
  2190. /*******************************************************************
  2191. * dll_open
  2192. *
  2193. * Open a DLL and read in exported symbols
  2194. */
  2195. BOOL dll_open (const char *dll_name)
  2196. {
  2197. return dump_analysis(dll_name, do_grab_sym, SIG_PE);
  2198. }
  2199. /*******************************************************************
  2200. * dll_next_symbol
  2201. *
  2202. * Get next exported symbol from dll
  2203. */
  2204. BOOL dll_next_symbol (parsed_symbol * sym)
  2205. {
  2206. if (!dll_current_symbol || !dll_current_symbol->symbol)
  2207. return FALSE;
  2208. assert (dll_symbols);
  2209. sym->symbol = xstrdup (dll_current_symbol->symbol);
  2210. sym->ordinal = dll_current_symbol->ordinal;
  2211. dll_current_symbol++;
  2212. return TRUE;
  2213. }