pe.c 87 KB

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