├── .gitignore ├── BUILD-WINDOWS ├── BuildVS2010 ├── README.txt ├── build.proj ├── buildVS2010.bat ├── libudis86.vcxproj ├── libudis86.vcxproj.filters ├── udcli.vcxproj ├── udcli.vcxproj.filters └── udis86.sln ├── CHANGES ├── INSTALL ├── LICENSE ├── Makefile.am ├── README ├── autogen.sh ├── configure.ac ├── docs ├── Makefile.am ├── manual │ ├── Makefile.am │ ├── conf.py │ ├── getstarted.rst │ ├── index.rst │ ├── libudis86.rst │ └── static │ │ └── udis86.css └── x86 │ ├── Makefile.am │ ├── README │ ├── avx.xml │ ├── optable.xml │ └── optable.xsl ├── libudis86 ├── Makefile.am ├── decode.c ├── decode.h ├── extern.h ├── syn-att.c ├── syn-intel.c ├── syn.c ├── syn.h ├── types.h ├── udint.h └── udis86.c ├── m4 ├── ax_compare_version.m4 ├── ax_prog_sphinx_version.m4 ├── ax_prog_yasm_version.m4 ├── ax_with_prog.m4 └── ax_with_python.m4 ├── scripts ├── Makefile.am ├── asmtest.sh ├── ud_itab.py └── ud_opcode.py ├── tests ├── Makefile.am ├── asm │ ├── 16 │ │ ├── disp.asm │ │ └── test16.asm │ ├── 32 │ │ ├── att.asm │ │ ├── att.asm.Sref │ │ ├── att.asm.ref │ │ ├── avx.asm │ │ ├── avx.asm.Sref │ │ ├── avx.asm.ref │ │ ├── corner.asm │ │ ├── corner.asm.ref │ │ ├── disp.asm │ │ ├── disp.asm.Sref │ │ ├── invalid_seg.asm │ │ ├── invalid_seg.asm.ref │ │ ├── obscure.asm │ │ ├── obscure.asm.ref │ │ ├── reljmp.asm │ │ ├── reljmp.asm.ref │ │ ├── sext.asm │ │ ├── sext.asm.ref │ │ └── test32.asm │ └── 64 │ │ ├── amd │ │ ├── invalid.asm │ │ └── invalid.asm.ref │ │ ├── avx.asm │ │ ├── avx.asm.ref │ │ ├── branch.asm │ │ ├── branch.asm.ref │ │ ├── disp.asm │ │ ├── disp.asm.Sref │ │ ├── disp.asm.ref │ │ ├── intel │ │ ├── invalid.asm │ │ └── invalid.asm.ref │ │ ├── reljmp.asm │ │ ├── reljmp.asm.ref │ │ ├── sext.asm │ │ ├── sext.asm.ref │ │ └── test64.asm ├── difftest.sh.in ├── installcheck.c ├── libcheck.c ├── oprgen.py ├── symresolve.c └── symresolve.ref ├── udcli ├── Makefile.am └── udcli.c └── udis86.h /.gitignore: -------------------------------------------------------------------------------- 1 | Makefile 2 | Makefile.in 3 | *.lo 4 | *.o 5 | .*.swp 6 | .deps 7 | .libs 8 | config.* 9 | *.pyc 10 | aclocal.m4 11 | autom4te.cache/ 12 | build/ 13 | configure 14 | docs/manual/html/ 15 | docs/manual/texinfo/ 16 | docs/manual/udis86.info 17 | libtool 18 | libudis86/itab.c 19 | libudis86/itab.h 20 | libudis86/libudis86.la 21 | m4/libtool.m4 22 | m4/ltoptions.m4 23 | m4/ltsugar.m4 24 | m4/ltversion.m4 25 | m4/lt~obsolete.m4 26 | stamp-h1 27 | tests/ovrrun 28 | tests/test_dis 29 | tests/sym_resolver 30 | tests/test_invalid_seg.asm 31 | tests/testjmp.out 32 | udcli/udcli 33 | tests/_results/ 34 | tests/*.bin 35 | tests/*.out 36 | tests/libcheck 37 | tests/difftest.sh 38 | scripts/ud_*asmtest* 39 | 40 | # Ignore distcheck outut 41 | udis86-1.* 42 | 43 | # Ignore C++ Builder history folders 44 | libudis86/__history/ 45 | 46 | #ignore thumbnails created by windows 47 | Thumbs.db 48 | #Ignore files built by Visual Studio 49 | *.user 50 | *.aps 51 | *.pch 52 | *.vspscc 53 | *_i.c 54 | *_p.c 55 | *.ncb 56 | *.suo 57 | *.bak 58 | *.cache 59 | *.ilk 60 | *.log 61 | *.sdf 62 | [Ii]pch 63 | [Bb]uild 64 | [Bb]in 65 | [Dd]ebug*/ 66 | *.sbr 67 | obj/ 68 | Win32/ 69 | x64/ 70 | [Rr]elease*/ 71 | _ReSharper*/ 72 | *.tar.gz 73 | tests/bufoverrun 74 | tests/symresolve 75 | -------------------------------------------------------------------------------- /BUILD-WINDOWS: -------------------------------------------------------------------------------- 1 | Build Instructions for Visual Studio 2 | ************************************ 3 | 4 | Note: it is important to perform the build at least once before 5 | attempting to open the solution in Visual Studio so that the build 6 | process can generate the itab.c/itab.h files from the itab.py Python 7 | script. 8 | 9 | Prerequisites 10 | ============= 11 | 12 | The buildVS2010.bat and build.proj MSBuild project have the following 13 | prerequisites: 14 | 15 | 1. Visual Studio 2010 with VC++ (full edition is required to compile 16 | for x64). A later compiler can be used however the build batch file 17 | will need to be changed to run the correct vcvarsall.bat. 18 | 19 | 2. Python 2.7 - although a later version should work, the build script 20 | has been configured to expect "python.exe" to be installed in 21 | "c:\python27\". 22 | 23 | How to Build 24 | ============ 25 | 26 | Ensure all prerequisites are in place and then run "buildVS2010.bat". 27 | MSBuild will run the "Clean" target for the "udis86.sln" solution and 28 | then build the following targets for "build.proj" MSBuild project: 29 | 30 | 1. Clean - (remove the "./Build" directory) 31 | 2. BuildRelease_x86 - build the release configuration for x86 32 | 3. BuildRelease_x64 - build the release configuration for x64 33 | 4. PostBuild - copy license and headers to build directory 34 | 35 | If a debug version is required, the batch file can be modified to also 36 | build the BuildDebug_x86 and BuildDebug_x64 targets. 37 | 38 | If Visual Studio Express is being used you will need to comment out the 39 | x64 target(s). 40 | 41 | Build Output 42 | ============ 43 | 44 | The Build directory has the following structure (if building all 45 | Release and Debug targets): 46 | 47 | . 48 | +-- Bin 49 | ¦ +-- Debug 50 | ¦ ¦ +-- x64 51 | ¦ ¦ ¦ +-- libudis86.dll 52 | ¦ ¦ ¦ +-- libudis86.ilk 53 | ¦ ¦ ¦ +-- libudis86.pdb 54 | ¦ ¦ ¦ +-- udcli.exe 55 | ¦ ¦ ¦ +-- udcli.ilk 56 | ¦ ¦ ¦ +-- udcli.pdb 57 | ¦ ¦ +-- x86 58 | ¦ ¦ +-- libudis86.dll 59 | ¦ ¦ +-- libudis86.ilk 60 | ¦ ¦ +-- libudis86.pdb 61 | ¦ ¦ +-- udcli.exe 62 | ¦ ¦ +-- udcli.ilk 63 | ¦ ¦ +-- udcli.pdb 64 | ¦ +-- x64 65 | ¦ ¦ +-- libudis86.dll 66 | ¦ ¦ +-- libudis86.pdb 67 | ¦ ¦ +-- udcli.exe 68 | ¦ ¦ +-- udcli.pdb 69 | ¦ +-- x86 70 | ¦ +-- libudis86.dll 71 | ¦ +-- libudis86.pdb 72 | ¦ +-- udcli.exe 73 | ¦ +-- udcli.pdb 74 | +-- Include 75 | ¦ +-- libudis86 76 | ¦ ¦ +-- itab.h 77 | ¦ ¦ +-- types.h 78 | ¦ +-- udis86.h 79 | +-- Lib 80 | ¦ +-- Debug 81 | ¦ ¦ +-- x64 82 | ¦ ¦ ¦ +-- libudis86.lib 83 | ¦ ¦ +-- x86 84 | ¦ ¦ +-- libudis86.lib 85 | ¦ +-- x64 86 | ¦ ¦ +-- libudis86.lib 87 | ¦ +-- x86 88 | ¦ +-- libudis86.lib 89 | +-- LICENSE 90 | +-- README 91 | -------------------------------------------------------------------------------- /BuildVS2010/README.txt: -------------------------------------------------------------------------------- 1 | Build notes for Windows: 2 | - If you don't have python installed at C:\Python27, edit build.proj accordingly 3 | - Execute buildVS2010.bat 4 | - If you got VS2012, this will fail. 5 | - Edit/Copy the build script, so that it finds vcvars32.bat (just needed to replace 10.0 with 11.0). 6 | - Open the solution and update the toolset. 7 | - Compile with the build script and ignore an error regarding ./Win32 not deletable. 8 | - Be done with it :) -------------------------------------------------------------------------------- /BuildVS2010/build.proj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | .\Deploy 5 | .\Build 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 47 | 49 | 51 | 53 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /BuildVS2010/buildVS2010.bat: -------------------------------------------------------------------------------- 1 | echo off 2 | call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86_amd64 3 | 4 | msbuild.exe udis86.sln /t:Clean 5 | msbuild.exe build.proj /t:Clean,BuildRelease_x86,BuildRelease_x64,PostBuild 6 | REM msbuild.exe build.proj /t:Clean,BuildRelease_x86,BuildRelease_x64,BuildDebug_x86,BuildDebug_x64,PostBuild 7 | pause -------------------------------------------------------------------------------- /BuildVS2010/libudis86.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug-DLL 6 | Win32 7 | 8 | 9 | Debug-DLL 10 | x64 11 | 12 | 13 | Debug 14 | Win32 15 | 16 | 17 | Debug 18 | x64 19 | 20 | 21 | Release-DLL 22 | Win32 23 | 24 | 25 | Release-DLL 26 | x64 27 | 28 | 29 | Release 30 | Win32 31 | 32 | 33 | Release 34 | x64 35 | 36 | 37 | 38 | {D5C34A21-7218-4A08-9578-1EBB35280A42} 39 | Win32Proj 40 | libudis86 41 | 42 | 43 | 44 | StaticLibrary 45 | true 46 | Unicode 47 | 48 | 49 | DynamicLibrary 50 | true 51 | Unicode 52 | 53 | 54 | StaticLibrary 55 | true 56 | Unicode 57 | 58 | 59 | DynamicLibrary 60 | true 61 | Unicode 62 | 63 | 64 | StaticLibrary 65 | false 66 | true 67 | Unicode 68 | 69 | 70 | DynamicLibrary 71 | false 72 | true 73 | Unicode 74 | 75 | 76 | StaticLibrary 77 | false 78 | true 79 | Unicode 80 | 81 | 82 | DynamicLibrary 83 | false 84 | true 85 | Unicode 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | true 117 | $(SolutionDir)Build\Lib\Debug\x86\ 118 | $(Platform)\$(Configuration)\ 119 | 120 | 121 | true 122 | $(SolutionDir)Build\Bin\Debug\x86\ 123 | $(Platform)\$(Configuration)\ 124 | 125 | 126 | true 127 | $(SolutionDir)Build\Lib\Debug\x64\ 128 | 129 | 130 | true 131 | $(SolutionDir)Build\Bin\Debug\x64\ 132 | 133 | 134 | false 135 | $(SolutionDir)Build\Lib\x86\ 136 | $(Platform)\$(Configuration)\ 137 | 138 | 139 | false 140 | $(SolutionDir)Build\Bin\x86\ 141 | $(Platform)\$(Configuration)\ 142 | 143 | 144 | false 145 | $(SolutionDir)Build\Lib\x64\ 146 | 147 | 148 | false 149 | $(SolutionDir)Build\Bin\x64\ 150 | 151 | 152 | 153 | 154 | 155 | Level3 156 | Disabled 157 | WIN32;_DEBUG;_WINDOWS;_USRDLL;LIBUDIS86_EXPORTS;%(PreprocessorDefinitions) 158 | /c %(AdditionalOptions) 159 | 160 | 161 | Windows 162 | true 163 | 164 | 165 | 166 | 167 | 168 | 169 | Level3 170 | Disabled 171 | WIN32;_DEBUG;_WINDOWS;_USRDLL;LIBUDIS86_EXPORTS;%(PreprocessorDefinitions) 172 | /c %(AdditionalOptions) 173 | 174 | 175 | Windows 176 | true 177 | 178 | 179 | 180 | 181 | 182 | 183 | Level3 184 | Disabled 185 | WIN32;_DEBUG;_WINDOWS;_USRDLL;LIBUDIS86_EXPORTS;%(PreprocessorDefinitions) 186 | /c %(AdditionalOptions) 187 | 188 | 189 | Windows 190 | true 191 | 192 | 193 | 194 | 195 | 196 | 197 | Level3 198 | Disabled 199 | WIN32;_DEBUG;_WINDOWS;_USRDLL;LIBUDIS86_EXPORTS;%(PreprocessorDefinitions) 200 | /c %(AdditionalOptions) 201 | 202 | 203 | Windows 204 | true 205 | 206 | 207 | 208 | 209 | Level3 210 | 211 | 212 | MaxSpeed 213 | true 214 | true 215 | WIN32;NDEBUG;_WINDOWS;_USRDLL;LIBUDIS86_EXPORTS;%(PreprocessorDefinitions) 216 | 217 | 218 | Windows 219 | true 220 | true 221 | true 222 | 223 | 224 | 225 | 226 | Level3 227 | 228 | 229 | MaxSpeed 230 | true 231 | true 232 | WIN32;NDEBUG;_WINDOWS;_USRDLL;LIBUDIS86_EXPORTS;%(PreprocessorDefinitions) 233 | 234 | 235 | Windows 236 | true 237 | true 238 | true 239 | 240 | 241 | 242 | 243 | Level3 244 | 245 | 246 | MaxSpeed 247 | true 248 | true 249 | WIN32;NDEBUG;_WINDOWS;_USRDLL;LIBUDIS86_EXPORTS;%(PreprocessorDefinitions) 250 | 251 | 252 | Windows 253 | true 254 | true 255 | true 256 | 257 | 258 | 259 | 260 | Level3 261 | 262 | 263 | MaxSpeed 264 | true 265 | true 266 | WIN32;NDEBUG;_WINDOWS;_USRDLL;LIBUDIS86_EXPORTS;%(PreprocessorDefinitions) 267 | 268 | 269 | Windows 270 | true 271 | true 272 | true 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | -------------------------------------------------------------------------------- /BuildVS2010/libudis86.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | {67a7943a-ad63-4da1-be6b-daf8a3fef1d6} 18 | 19 | 20 | {a0eee912-4b82-493a-8c43-3b712061afb1} 21 | 22 | 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | Source Files\Generated 41 | 42 | 43 | 44 | 45 | Header Files 46 | 47 | 48 | Header Files 49 | 50 | 51 | Header Files 52 | 53 | 54 | Header Files 55 | 56 | 57 | Header Files 58 | 59 | 60 | Header Files\Generated 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /BuildVS2010/udcli.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug-DLL 6 | Win32 7 | 8 | 9 | Debug-DLL 10 | x64 11 | 12 | 13 | Debug 14 | Win32 15 | 16 | 17 | Debug 18 | x64 19 | 20 | 21 | Release-DLL 22 | Win32 23 | 24 | 25 | Release-DLL 26 | x64 27 | 28 | 29 | Release 30 | Win32 31 | 32 | 33 | Release 34 | x64 35 | 36 | 37 | 38 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14} 39 | Win32Proj 40 | udcli 41 | 42 | 43 | 44 | Application 45 | true 46 | Unicode 47 | 48 | 49 | Application 50 | true 51 | Unicode 52 | 53 | 54 | Application 55 | true 56 | Unicode 57 | 58 | 59 | Application 60 | true 61 | Unicode 62 | 63 | 64 | Application 65 | false 66 | true 67 | Unicode 68 | 69 | 70 | Application 71 | false 72 | true 73 | Unicode 74 | 75 | 76 | Application 77 | false 78 | true 79 | Unicode 80 | 81 | 82 | Application 83 | false 84 | true 85 | Unicode 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | true 117 | $(IncludePath) 118 | $(SolutionDir)Build\Bin\Debug\x86\ 119 | $(Platform)\$(Configuration)\ 120 | $(SolutionDir)Build\Lib\Debug\x86;$(LibraryPath) 121 | 122 | 123 | true 124 | $(IncludePath) 125 | $(SolutionDir)Build\Bin\$(Platform)\$(Configuration)\ 126 | $(Platform)\$(Configuration)\ 127 | 128 | 129 | true 130 | $(IncludePath) 131 | $(SolutionDir)Build\Bin\Debug\x64\ 132 | $(SolutionDir)Build\Lib\Debug\x64;$(LibraryPath) 133 | 134 | 135 | true 136 | $(IncludePath) 137 | $(SolutionDir)Build\Bin\$(Platform)\$(Configuration)\ 138 | 139 | 140 | false 141 | $(SolutionDir)Build\Bin\x86\ 142 | $(Platform)\$(Configuration)\ 143 | $(SolutionDir)Build\lib\x86;$(LibraryPath) 144 | 145 | 146 | false 147 | $(SolutionDir)Build\Bin\$(Platform)\$(Configuration)\ 148 | $(Platform)\$(Configuration)\ 149 | 150 | 151 | false 152 | $(SolutionDir)Build\Bin\x64\ 153 | $(SolutionDir)Build\lib\x64;$(LibraryPath) 154 | 155 | 156 | false 157 | $(SolutionDir)Build\Bin\$(Platform)\$(Configuration)\ 158 | 159 | 160 | 161 | 162 | 163 | Level3 164 | Disabled 165 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 166 | 167 | 168 | Console 169 | true 170 | libudis86.lib;%(AdditionalDependencies) 171 | 172 | 173 | 174 | 175 | 176 | 177 | Level3 178 | Disabled 179 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 180 | 181 | 182 | Console 183 | true 184 | 185 | 186 | 187 | 188 | 189 | 190 | Level3 191 | Disabled 192 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 193 | 194 | 195 | Console 196 | true 197 | libudis86.lib;%(AdditionalDependencies) 198 | 199 | 200 | 201 | 202 | 203 | 204 | Level3 205 | Disabled 206 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 207 | 208 | 209 | Console 210 | true 211 | 212 | 213 | 214 | 215 | Level3 216 | 217 | 218 | MaxSpeed 219 | true 220 | true 221 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 222 | 223 | 224 | Console 225 | true 226 | true 227 | true 228 | libudis86.lib;%(AdditionalDependencies) 229 | 230 | 231 | 232 | 233 | Level3 234 | 235 | 236 | MaxSpeed 237 | true 238 | true 239 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 240 | 241 | 242 | Console 243 | true 244 | true 245 | true 246 | 247 | 248 | 249 | 250 | Level3 251 | 252 | 253 | MaxSpeed 254 | true 255 | true 256 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 257 | 258 | 259 | Console 260 | true 261 | true 262 | true 263 | libudis86.lib;%(AdditionalDependencies) 264 | 265 | 266 | 267 | 268 | Level3 269 | 270 | 271 | MaxSpeed 272 | true 273 | true 274 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 275 | 276 | 277 | Console 278 | true 279 | true 280 | true 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | -------------------------------------------------------------------------------- /BuildVS2010/udcli.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /BuildVS2010/udis86.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libudis86", "libudis86.vcxproj", "{D5C34A21-7218-4A08-9578-1EBB35280A42}" 5 | EndProject 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "udcli", "udcli.vcxproj", "{620E885C-DA4A-4296-AFEB-AFB0077EFA14}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Debug|x64 = Debug|x64 12 | Debug-DLL|Win32 = Debug-DLL|Win32 13 | Debug-DLL|x64 = Debug-DLL|x64 14 | Release|Win32 = Release|Win32 15 | Release|x64 = Release|x64 16 | Release-DLL|Win32 = Release-DLL|Win32 17 | Release-DLL|x64 = Release-DLL|x64 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Debug|Win32.ActiveCfg = Debug|Win32 21 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Debug|Win32.Build.0 = Debug|Win32 22 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Debug|x64.ActiveCfg = Debug|x64 23 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Debug|x64.Build.0 = Debug|x64 24 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Debug-DLL|Win32.ActiveCfg = Debug-DLL|Win32 25 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Debug-DLL|Win32.Build.0 = Debug-DLL|Win32 26 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Debug-DLL|x64.ActiveCfg = Debug-DLL|x64 27 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Debug-DLL|x64.Build.0 = Debug-DLL|x64 28 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Release|Win32.ActiveCfg = Release|Win32 29 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Release|Win32.Build.0 = Release|Win32 30 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Release|x64.ActiveCfg = Release|x64 31 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Release|x64.Build.0 = Release|x64 32 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Release-DLL|Win32.ActiveCfg = Release-DLL|Win32 33 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Release-DLL|Win32.Build.0 = Release-DLL|Win32 34 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Release-DLL|x64.ActiveCfg = Release-DLL|x64 35 | {D5C34A21-7218-4A08-9578-1EBB35280A42}.Release-DLL|x64.Build.0 = Release-DLL|x64 36 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Debug|Win32.ActiveCfg = Debug|Win32 37 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Debug|Win32.Build.0 = Debug|Win32 38 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Debug|x64.ActiveCfg = Debug|x64 39 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Debug|x64.Build.0 = Debug|x64 40 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Debug-DLL|Win32.ActiveCfg = Debug-DLL|Win32 41 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Debug-DLL|x64.ActiveCfg = Debug-DLL|x64 42 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Release|Win32.ActiveCfg = Release|Win32 43 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Release|Win32.Build.0 = Release|Win32 44 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Release|x64.ActiveCfg = Release|x64 45 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Release|x64.Build.0 = Release|x64 46 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Release-DLL|Win32.ActiveCfg = Release-DLL|Win32 47 | {620E885C-DA4A-4296-AFEB-AFB0077EFA14}.Release-DLL|x64.ActiveCfg = Release-DLL|x64 48 | EndGlobalSection 49 | GlobalSection(SolutionProperties) = preSolution 50 | HideSolutionNode = FALSE 51 | EndGlobalSection 52 | EndGlobal 53 | -------------------------------------------------------------------------------- /CHANGES: -------------------------------------------------------------------------------- 1 | v1.7.2 2 | 3 | * Clean up input handling, removing unnecessary caching 4 | of input, which should speed up things. 5 | * Add the missing ud_insn_mnemonic api function. 6 | * Rename ud_opr_isgpr to ud_opr_is_gpr. 7 | * Fix decoding of relative jumps. 8 | * Fix build with automake-1.14 9 | * Minor fix to AT&T syntax (missing "$" prefix for immedaites) 10 | * Add a new api checker (tests/libcheck.c). 11 | * Add a standalone script for diff-testing (tests/difftest.sh) 12 | * Refinements to the documentation. 13 | 14 | Acknowledgements: 15 | 16 | Brendan Long (https://github.com/brendanlong) 17 | radare (https://github.com/radare) 18 | Sergey Basalaev (https://github.com/SBasalaev) 19 | ebfe (https://github.com/ebfe) 20 | 21 | v1.7.1 22 | 23 | * Full support for SSSE3, SSE4.1, SSE4.2, SMX, AES. 24 | * New Sphinx-doc/RST based documentation. 25 | * New api for client size symbol resolver. 26 | * Visual Studio 2010 Build Support. 27 | * Added an operand tester. 28 | * Python 3.0 compatibility changes. 29 | * Minor fixes to AT&T syntax. 30 | * Fix install directory for data files. 31 | * Many bug fixes, and optable updates. 32 | * Add Texinfo document (make install-info). 33 | 34 | Acknowledgements: 35 | 36 | L Peter Deutsch (https://github.com/ghghost) 37 | Bjoern Doebel (https://github.com/bjoernd) 38 | Justin Stenning (http://github.com/spazzarama) 39 | Jamie Iles (https://github.com/jamieiles) 40 | Stephen Fewer (https://github.com/stephenfewer) 41 | Piotr Gaczkowski (https://github.com/DoomHammer) 42 | Evan Pheonix 43 | mbarbu (https://github.com/mbarbu) 44 | 45 | 46 | 47 | Please see the commit logs for change information for older releases 48 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | Installation Instructions 2 | ************************* 3 | 4 | Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005, 5 | 2006 Free Software Foundation, Inc. 6 | 7 | This file is free documentation; the Free Software Foundation gives 8 | unlimited permission to copy, distribute and modify it. 9 | 10 | Basic Installation 11 | ================== 12 | 13 | Briefly, the shell commands `./configure; make; make install' should 14 | configure, build, and install this package. The following 15 | more-detailed instructions are generic; see the `README' file for 16 | instructions specific to this package. 17 | 18 | The `configure' shell script attempts to guess correct values for 19 | various system-dependent variables used during compilation. It uses 20 | those values to create a `Makefile' in each directory of the package. 21 | It may also create one or more `.h' files containing system-dependent 22 | definitions. Finally, it creates a shell script `config.status' that 23 | you can run in the future to recreate the current configuration, and a 24 | file `config.log' containing compiler output (useful mainly for 25 | debugging `configure'). 26 | 27 | It can also use an optional file (typically called `config.cache' 28 | and enabled with `--cache-file=config.cache' or simply `-C') that saves 29 | the results of its tests to speed up reconfiguring. Caching is 30 | disabled by default to prevent problems with accidental use of stale 31 | cache files. 32 | 33 | If you need to do unusual things to compile the package, please try 34 | to figure out how `configure' could check whether to do them, and mail 35 | diffs or instructions to the address given in the `README' so they can 36 | be considered for the next release. If you are using the cache, and at 37 | some point `config.cache' contains results you don't want to keep, you 38 | may remove or edit it. 39 | 40 | The file `configure.ac' (or `configure.in') is used to create 41 | `configure' by a program called `autoconf'. You need `configure.ac' if 42 | you want to change it or regenerate `configure' using a newer version 43 | of `autoconf'. 44 | 45 | The simplest way to compile this package is: 46 | 47 | 1. `cd' to the directory containing the package's source code and type 48 | `./configure' to configure the package for your system. 49 | 50 | Running `configure' might take a while. While running, it prints 51 | some messages telling which features it is checking for. 52 | 53 | 2. Type `make' to compile the package. 54 | 55 | 3. Optionally, type `make check' to run any self-tests that come with 56 | the package. 57 | 58 | 4. Type `make install' to install the programs and any data files and 59 | documentation. 60 | 61 | 5. You can remove the program binaries and object files from the 62 | source code directory by typing `make clean'. To also remove the 63 | files that `configure' created (so you can compile the package for 64 | a different kind of computer), type `make distclean'. There is 65 | also a `make maintainer-clean' target, but that is intended mainly 66 | for the package's developers. If you use it, you may have to get 67 | all sorts of other programs in order to regenerate files that came 68 | with the distribution. 69 | 70 | Compilers and Options 71 | ===================== 72 | 73 | Some systems require unusual options for compilation or linking that the 74 | `configure' script does not know about. Run `./configure --help' for 75 | details on some of the pertinent environment variables. 76 | 77 | You can give `configure' initial values for configuration parameters 78 | by setting variables in the command line or in the environment. Here 79 | is an example: 80 | 81 | ./configure CC=c99 CFLAGS=-g LIBS=-lposix 82 | 83 | *Note Defining Variables::, for more details. 84 | 85 | Compiling For Multiple Architectures 86 | ==================================== 87 | 88 | You can compile the package for more than one kind of computer at the 89 | same time, by placing the object files for each architecture in their 90 | own directory. To do this, you can use GNU `make'. `cd' to the 91 | directory where you want the object files and executables to go and run 92 | the `configure' script. `configure' automatically checks for the 93 | source code in the directory that `configure' is in and in `..'. 94 | 95 | With a non-GNU `make', it is safer to compile the package for one 96 | architecture at a time in the source code directory. After you have 97 | installed the package for one architecture, use `make distclean' before 98 | reconfiguring for another architecture. 99 | 100 | Installation Names 101 | ================== 102 | 103 | By default, `make install' installs the package's commands under 104 | `/usr/local/bin', include files under `/usr/local/include', etc. You 105 | can specify an installation prefix other than `/usr/local' by giving 106 | `configure' the option `--prefix=PREFIX'. 107 | 108 | You can specify separate installation prefixes for 109 | architecture-specific files and architecture-independent files. If you 110 | pass the option `--exec-prefix=PREFIX' to `configure', the package uses 111 | PREFIX as the prefix for installing programs and libraries. 112 | Documentation and other data files still use the regular prefix. 113 | 114 | In addition, if you use an unusual directory layout you can give 115 | options like `--bindir=DIR' to specify different values for particular 116 | kinds of files. Run `configure --help' for a list of the directories 117 | you can set and what kinds of files go in them. 118 | 119 | If the package supports it, you can cause programs to be installed 120 | with an extra prefix or suffix on their names by giving `configure' the 121 | option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. 122 | 123 | Optional Features 124 | ================= 125 | 126 | Some packages pay attention to `--enable-FEATURE' options to 127 | `configure', where FEATURE indicates an optional part of the package. 128 | They may also pay attention to `--with-PACKAGE' options, where PACKAGE 129 | is something like `gnu-as' or `x' (for the X Window System). The 130 | `README' should mention any `--enable-' and `--with-' options that the 131 | package recognizes. 132 | 133 | For packages that use the X Window System, `configure' can usually 134 | find the X include and library files automatically, but if it doesn't, 135 | you can use the `configure' options `--x-includes=DIR' and 136 | `--x-libraries=DIR' to specify their locations. 137 | 138 | Specifying the System Type 139 | ========================== 140 | 141 | There may be some features `configure' cannot figure out automatically, 142 | but needs to determine by the type of machine the package will run on. 143 | Usually, assuming the package is built to be run on the _same_ 144 | architectures, `configure' can figure that out, but if it prints a 145 | message saying it cannot guess the machine type, give it the 146 | `--build=TYPE' option. TYPE can either be a short name for the system 147 | type, such as `sun4', or a canonical name which has the form: 148 | 149 | CPU-COMPANY-SYSTEM 150 | 151 | where SYSTEM can have one of these forms: 152 | 153 | OS KERNEL-OS 154 | 155 | See the file `config.sub' for the possible values of each field. If 156 | `config.sub' isn't included in this package, then this package doesn't 157 | need to know the machine type. 158 | 159 | If you are _building_ compiler tools for cross-compiling, you should 160 | use the option `--target=TYPE' to select the type of system they will 161 | produce code for. 162 | 163 | If you want to _use_ a cross compiler, that generates code for a 164 | platform different from the build platform, you should specify the 165 | "host" platform (i.e., that on which the generated programs will 166 | eventually be run) with `--host=TYPE'. 167 | 168 | Sharing Defaults 169 | ================ 170 | 171 | If you want to set default values for `configure' scripts to share, you 172 | can create a site shell script called `config.site' that gives default 173 | values for variables like `CC', `cache_file', and `prefix'. 174 | `configure' looks for `PREFIX/share/config.site' if it exists, then 175 | `PREFIX/etc/config.site' if it exists. Or, you can set the 176 | `CONFIG_SITE' environment variable to the location of the site script. 177 | A warning: not all `configure' scripts look for a site script. 178 | 179 | Defining Variables 180 | ================== 181 | 182 | Variables not defined in a site shell script can be set in the 183 | environment passed to `configure'. However, some packages may run 184 | configure again during the build, and the customized values of these 185 | variables may be lost. In order to avoid this problem, you should set 186 | them in the `configure' command line, using `VAR=value'. For example: 187 | 188 | ./configure CC=/usr/local2/bin/gcc 189 | 190 | causes the specified `gcc' to be used as the C compiler (unless it is 191 | overridden in the site shell script). 192 | 193 | Unfortunately, this technique does not work for `CONFIG_SHELL' due to 194 | an Autoconf bug. Until the bug is fixed you can use this workaround: 195 | 196 | CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash 197 | 198 | `configure' Invocation 199 | ====================== 200 | 201 | `configure' recognizes the following options to control how it operates. 202 | 203 | `--help' 204 | `-h' 205 | Print a summary of the options to `configure', and exit. 206 | 207 | `--version' 208 | `-V' 209 | Print the version of Autoconf used to generate the `configure' 210 | script, and exit. 211 | 212 | `--cache-file=FILE' 213 | Enable the cache: use and save the results of the tests in FILE, 214 | traditionally `config.cache'. FILE defaults to `/dev/null' to 215 | disable caching. 216 | 217 | `--config-cache' 218 | `-C' 219 | Alias for `--cache-file=config.cache'. 220 | 221 | `--quiet' 222 | `--silent' 223 | `-q' 224 | Do not print messages saying which checks are being made. To 225 | suppress all normal output, redirect it to `/dev/null' (any error 226 | messages will still be shown). 227 | 228 | `--srcdir=DIR' 229 | Look for the package's source code in directory DIR. Usually 230 | `configure' can determine that directory automatically. 231 | 232 | `configure' also accepts some other, not widely useful, options. Run 233 | `configure --help' for more details. 234 | 235 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2002-2012, Vivek Thampi 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | 2. Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 17 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = -I build/m4 2 | 3 | SUBDIRS = scripts libudis86 udcli docs tests 4 | 5 | MAINTAINERCLEANFILES = \ 6 | Makefile.in \ 7 | configure \ 8 | config.h.in \ 9 | config.h.in~ \ 10 | missing \ 11 | aclocal.m4 \ 12 | build/config.guess \ 13 | build/config.sub \ 14 | build/compile \ 15 | build/config.guess \ 16 | build/config.sub \ 17 | build/depcomp \ 18 | build/install-sh \ 19 | build/ltmain.sh \ 20 | build/missing 21 | 22 | include_ladir = ${includedir} 23 | include_la_HEADERS = udis86.h 24 | 25 | .PHONY: libudis86 udcli tests docs 26 | 27 | libudis86: 28 | $(MAKE) -C $@ 29 | 30 | udcli: libudis86 31 | $(MAKE) -C $@ 32 | 33 | tests: check 34 | 35 | maintainer-clean-local: 36 | -rm -rf build/m4 37 | -rm -rf build 38 | -rm -rf autom4te.cache 39 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Udis86 2 | ====== 3 | 4 | Udis86 is a disassembler for the x86 and x86-64 class of instruction set 5 | architectures. It consists of a C library called libudis86 which 6 | provides a clean and simple interface to decode a stream of raw binary 7 | data, and to inspect the disassembled instructions in a structured 8 | manner. 9 | 10 | 11 | LICENSE 12 | ------- 13 | 14 | Udis86 is distributed under the terms of the 2-clause "Simplified BSD 15 | License". A copy of the license is included with the source in LICENSE. 16 | 17 | 18 | libudis86 19 | --------- 20 | 21 | o Supports all x86 and x86-64 (AMD64) General purpose and 22 | System instructions. 23 | o Supported ISA extensions: 24 | - MMX, FPU (x87), AMD 3DNow 25 | - SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, SSE4a 26 | - AMD-V, INTEL-VMX, SMX, AVX, BMI, FMA4, FMA, F16C 27 | - ADX, MPX, SGX, RTM, AES, SHA, CET 28 | o Instructions are defined in an XML document, with opcode 29 | tables generated for performance. 30 | o Supports output in both INTEL (NASM) as well as AT&T (GNU as) style 31 | assembly language syntax. 32 | o Supports a variety of input methods: Files, Memory Buffers, and 33 | Function Callback hooks. 34 | o Re-entrant, no dynamic memory allocation. 35 | o Fully documented API 36 | 37 | 38 | -- EXAMPLE ----------------------------------------------------------- 39 | 40 | ud_t u; 41 | 42 | ud_init(&u); 43 | ud_set_input_file(&u, stdin); 44 | ud_set_mode(&u, 64); 45 | ud_set_syntax(&u, UD_SYN_INTEL); 46 | 47 | while (ud_disassemble(&u)) { 48 | printf("\t%s\n", ud_insn_asm(&ud_obj)); 49 | } 50 | 51 | ---------------------------------------------------------------------- 52 | 53 | 54 | udcli 55 | ----- 56 | 57 | udcli is a small command-line tool for your quick disassembly needs. 58 | 59 | -- EXAMPLE ----------------------------------------------------------- 60 | 61 | $ echo "65 67 89 87 76 65 54 56 78 89 09 00 90" | udcli -32 -x 62 | 0000000080000800 656789877665 mov [gs:bx+0x6576], eax 63 | 0000000080000806 54 push esp 64 | 0000000080000807 56 push esi 65 | 0000000080000808 7889 js 0x80000793 66 | 000000008000080a 0900 or [eax], eax 67 | 000000008000080c 90 nop 68 | 69 | ---------------------------------------------------------------------- 70 | 71 | 72 | Documentation 73 | ------------- 74 | 75 | The libudis86 api is fully documented. The package distribution contains 76 | a Texinfo file which can be installed by invoking "make install-info". 77 | You can also find an online html version of the documentation available 78 | at http://udis86.sourceforge.net/. 79 | 80 | 81 | Autotools Build 82 | --------------- 83 | 84 | You need autotools if building from sources cloned form version control 85 | system, or if you need to regenerate the build system. The wrapper 86 | script 'autogen.sh' is provided that'll generate the build system. 87 | 88 | 89 | AUTHOR 90 | ------ 91 | 92 | Udis86 is written and maintained by Vivek Thampi (vivek.mt@gmail.com). 93 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | if [ ! -e build/m4 ]; then mkdir -p build/m4; fi 4 | autoreconf --force -v --install || ( echo "autogen: autoreconf -i failed." && false ) 5 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # 2 | # udis86 autoconf 3 | # 4 | AC_PREREQ(2.60) 5 | AC_INIT([udis86], [1.7.2], [vivek.mt@gmail.com]) 6 | AC_CONFIG_HEADERS(config.h) 7 | AC_CONFIG_AUX_DIR(build) 8 | AC_CONFIG_MACRO_DIR([build/m4]) 9 | 10 | m4_include([m4/ax_compare_version.m4]) 11 | m4_include([m4/ax_prog_sphinx_version.m4]) 12 | m4_include([m4/ax_prog_yasm_version.m4]) 13 | m4_include([m4/ax_with_prog.m4]) 14 | m4_include([m4/ax_with_python.m4]) 15 | 16 | # Make sure $ACLOCAL_FLAGS are used during a rebuild. 17 | AC_SUBST([ACLOCAL_AMFLAGS], ["-I $ac_macro_dir \${ACLOCAL_FLAGS}"]) 18 | 19 | # 20 | # Determine the build host; we notify automake of Windows 21 | # builds, so it can pass proper parameters for building 22 | # DLLs to the linker. 23 | # 24 | AC_CANONICAL_HOST 25 | 26 | case "$host_os" in 27 | mingw32* ) 28 | TARGET_OS=windows 29 | AC_LIBTOOL_WIN32_DLL 30 | ;; 31 | esac 32 | 33 | # Initialize the automake subsystem. 34 | AM_INIT_AUTOMAKE([1.11 -Wall -Wno-portability -Wno-extra-portability -Werror foreign]) 35 | 36 | # 37 | # In case we have a Windows build, we pass a 38 | # TARGET_WINDOWS conditional to automake. 39 | # 40 | AM_CONDITIONAL(TARGET_WINDOWS, test "$TARGET_OS" = windows) 41 | 42 | AC_PROG_CC 43 | AC_DISABLE_STATIC 44 | AC_PROG_LIBTOOL 45 | AM_PROG_CC_C_O 46 | 47 | # If this is a gnu compiler, pass -Wall 48 | if test "$ac_cv_c_compiler_gnu" = "yes"; then 49 | CFLAGS="$CFLAGS -Wall" 50 | fi 51 | 52 | # Look for python 53 | AX_WITH_PYTHON 54 | if test "x$PYTHON" = "x"; then 55 | AC_MSG_ERROR([Python not found. Use --with-python to specify path to python binary]) 56 | fi 57 | 58 | # Yasm (>= 1.2.0) for testing 59 | AX_WITH_PROG(YASM,yasm) 60 | AX_PROG_YASM_VERSION([1.2.0],[ac_have_yasm_version=1],[]) 61 | AM_CONDITIONAL(HAVE_YASM, [test -n "$ac_have_yasm_version"]) 62 | 63 | # Sphinx (>= 1.1.3) for documentation 64 | AX_WITH_PROG(SPHINX_BUILD,sphinx-build) 65 | AX_PROG_SPHINX([1.1.3],[ac_have_sphinx_version=1],[]) 66 | AM_CONDITIONAL(HAVE_SPHINX_DOC, [test -n "$ac_have_sphinx_version"]) 67 | 68 | AC_CHECK_HEADERS([assert.h stdio.h]) 69 | 70 | AC_CONFIG_FILES([ 71 | Makefile 72 | scripts/Makefile 73 | libudis86/Makefile 74 | udcli/Makefile 75 | tests/Makefile 76 | docs/Makefile 77 | docs/manual/Makefile 78 | docs/x86/Makefile 79 | tests/difftest.sh 80 | ]) 81 | 82 | AC_OUTPUT 83 | -------------------------------------------------------------------------------- /docs/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = x86 manual 2 | MAINTAINERCLEANFILES = Makefile.in 3 | -------------------------------------------------------------------------------- /docs/manual/Makefile.am: -------------------------------------------------------------------------------- 1 | manualdir = ${docdir}/manual 2 | 3 | rst_sources = \ 4 | index.rst \ 5 | getstarted.rst \ 6 | libudis86.rst 7 | 8 | # 9 | # The official distribution only contains the rst files. So users 10 | # may generate documentation using Sphinx if they wish to. 11 | # 12 | EXTRA_DIST = \ 13 | $(rst_sources) \ 14 | udis86.info \ 15 | conf.py \ 16 | static 17 | 18 | MAINTAINERCLEANFILES = \ 19 | Makefile.in \ 20 | udis86.info 21 | 22 | if HAVE_SPHINX_DOC 23 | 24 | html-local: 25 | $(SPHINX_BUILD) -c $(srcdir) -b html $(srcdir) $(builddir)/html 26 | 27 | udis86.info: $(rst_sources) 28 | $(SPHINX_BUILD) -E -c $(srcdir) -b texinfo $(srcdir) $(builddir)/texinfo 29 | $(MAKEINFO) --no-split $(builddir)/texinfo/udis86.texi 30 | 31 | check-local: html-local 32 | 33 | else 34 | 35 | udis86.info: $(rst_sources) 36 | html-local udis86.info: 37 | @echo "" 38 | @echo "------------------------------------------------------------------" 39 | @echo "" 40 | @echo " Please make sure you have Sphinx (sphinx-doc.org) version 1.1.3" 41 | @echo " or above, to be able to build documentation." 42 | @echo "" 43 | @echo " You can also find documentation at http://udis86.sourceforge.net/" 44 | @echo "" 45 | @echo "------------------------------------------------------------------" 46 | @echo "" 47 | @exit 1 48 | 49 | endif 50 | 51 | install-info-local: udis86.info 52 | install-info --info-dir=$(infodir) $< 53 | 54 | clean-local: 55 | -rm -rf $(builddir)/html 56 | -rm -rf $(builddir)/texinfo $(builddir)/udis86.info 57 | -------------------------------------------------------------------------------- /docs/manual/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import sys, os 3 | 4 | source_suffix = '.rst' 5 | master_doc = 'index' 6 | project = u'udis86' 7 | copyright = u'2013, Vivek Thampi' 8 | version = '1.7' 9 | release = '1.7.2' 10 | 11 | # List of patterns, relative to source directory, that match files and 12 | # directories to ignore when looking for source files. 13 | exclude_patterns = ['_build'] 14 | 15 | pygments_style = 'sphinx' 16 | html_theme = 'pyramid' 17 | html_theme_options = { "nosidebar" : True } 18 | html_static_path = ['static'] 19 | html_style = "udis86.css" 20 | htmlhelp_basename = 'udis86doc' 21 | 22 | latex_documents = [ 23 | ('index', 'udis86.tex', u'udis86 Documentation', 24 | u'Vivek Thampi', 'manual'), 25 | ] 26 | 27 | man_pages = [ 28 | ('index', 'udis86', u'udis86 Documentation', 29 | [u'Vivek Thampi'], 1) 30 | ] 31 | 32 | texinfo_documents = [ 33 | ('index', 'udis86', u'udis86 Documentation', 34 | u'Vivek Thampi', 'udis86', 'Disassembler library for x86.', 35 | 'Miscellaneous', True), 36 | ] 37 | -------------------------------------------------------------------------------- /docs/manual/getstarted.rst: -------------------------------------------------------------------------------- 1 | Getting Started 2 | =============== 3 | 4 | 5 | Building and Installing udis86 6 | ------------------------------ 7 | 8 | udis86 is developed for unix-like environments, and like most software, 9 | the basic steps towards building and installing it are as follows. 10 | 11 | .. code:: 12 | 13 | $ ./configure 14 | $ make 15 | $ make install 16 | 17 | Depending on your choice of install location, you may need to have root 18 | privileges to do an install. The install scripts copy the necessary header 19 | and library files to appropriate locations in your system. 20 | 21 | 22 | Interfacing with libudis86: A Quick Example 23 | ------------------------------------------- 24 | 25 | The following is an example of a program that interfaces with libudis86 26 | and uses the API to generate assembly language output for 64-bit code, 27 | input from STDIN. 28 | 29 | .. code-block:: c 30 | 31 | #include 32 | #include 33 | 34 | int main() 35 | { 36 | ud_t ud_obj; 37 | 38 | ud_init(&ud_obj); 39 | ud_set_input_file(&ud_obj, stdin); 40 | ud_set_mode(&ud_obj, 64); 41 | ud_set_syntax(&ud_obj, UD_SYN_INTEL); 42 | 43 | while (ud_disassemble(&ud_obj)) { 44 | printf("\t%s\n", ud_insn_asm(&ud_obj)); 45 | } 46 | 47 | return 0; 48 | } 49 | 50 | To compile the program (using gcc): 51 | 52 | .. code:: 53 | 54 | $ gcc -ludis86 example.c -o example 55 | 56 | This example should give you an idea of how this library can be used. The 57 | following sections describe, in detail, the complete API of libudis86. 58 | -------------------------------------------------------------------------------- /docs/manual/index.rst: -------------------------------------------------------------------------------- 1 | .. udis86 documentation master file 2 | 3 | Welcome to udis86's documentation! 4 | ================================== 5 | 6 | Udis86 is a disassembler engine that decodes a stream of binary 7 | machine code bytes as opcodes defined in the x86 and x86-64 class 8 | of Instruction Set Archictures. The core component of this project 9 | is libudis86 which provides a clean and simple interface to 10 | disassemble binary code, and to inspect the disassembly to various 11 | degrees of detail. The library is designed to aid software 12 | projects that entail analysis and manipulation of all flavors of 13 | x86 binary code. 14 | 15 | 16 | .. toctree:: 17 | :maxdepth: 3 18 | 19 | getstarted 20 | libudis86 21 | 22 | Indices and tables 23 | ================== 24 | 25 | * :ref:`genindex` 26 | * :ref:`search` 27 | 28 | -------------------------------------------------------------------------------- /docs/manual/static/udis86.css: -------------------------------------------------------------------------------- 1 | /* override pyramid */ 2 | 3 | @import url("pyramid.css"); 4 | 5 | body { 6 | background-color: #eee; 7 | background-color: #e8ecef; 8 | } 9 | 10 | pre { 11 | background-color: #e8ecef; 12 | border: 1px solid #bbb; 13 | border-radius: 5px; 14 | -moz-border-radius: 5px; 15 | } 16 | 17 | div.body { 18 | border: 1px solid #bbb; 19 | border-radius: 5px; 20 | -moz-border-radius: 5px; 21 | color: black; 22 | } 23 | 24 | div.related, 25 | div.document { 26 | width: 840px; 27 | margin-left: auto; 28 | margin-right: auto; 29 | } 30 | 31 | div.related ul { 32 | padding-left: 8px; 33 | } 34 | 35 | div.footer a, 36 | div.footer { 37 | color: #000; 38 | font-weight: bold; 39 | } 40 | 41 | div.footer { 42 | margin-top: 40px; 43 | background-color: #ddd; 44 | } 45 | 46 | /* 47 | div.body { 48 | margin-left: auto; 49 | margin-right: auto; 50 | width: 720px; 51 | }*/ 52 | 53 | body, 54 | div.body, 55 | div.body h1, 56 | div.body h2, 57 | div.body h3, 58 | div.body h4, 59 | div.body h5, 60 | div.body h6 { 61 | font-family: Arial, "Helvetica Neue", Arial, Helvetica, "sans-serif"; 62 | } 63 | 64 | div.body h1, 65 | div.body h2, 66 | div.body h3, 67 | div.body h4, 68 | div.body h5, 69 | div.body h6 { 70 | font-weight: bold; 71 | } 72 | 73 | code, 74 | .function dt, 75 | .member dt, 76 | .type dt, 77 | .var dt, 78 | .function tt.descname, 79 | .member tt.descname, 80 | .var tt.descname, 81 | .type tt.descname, 82 | pre { 83 | font-family: 'Consolas', 'Deja Vu Sans Mono', 'Bitstream Vera Sans Mono', monospace; 84 | font-size: 1em; 85 | padding-bottom: 6px; 86 | } 87 | 88 | .function dt 89 | { 90 | font-size: 1em; 91 | padding-bottom: 6px; 92 | } 93 | 94 | .function tt.descname { 95 | font-size: 1em; 96 | } 97 | 98 | a .pre, 99 | div.related a, 100 | a { 101 | text-decoration: none; 102 | color: #444; 103 | border-bottom: 1px solid #eee; 104 | } 105 | 106 | a .pre { 107 | font-weight: bold; 108 | } 109 | 110 | a:hover .pre, 111 | a:hover, div.toctree-wrapper a:hover, .indextable a:hover, #indices-and-tables a:hover { 112 | text-decoration: none; 113 | color: #111; 114 | border-bottom: 1px solid #111; 115 | } 116 | -------------------------------------------------------------------------------- /docs/x86/Makefile.am: -------------------------------------------------------------------------------- 1 | x86dir = ${docdir}/x86 2 | dist_x86_DATA = optable.xml optable.xsl 3 | 4 | MAINTAINERCLEANFILES = Makefile.in 5 | -------------------------------------------------------------------------------- /docs/x86/README: -------------------------------------------------------------------------------- 1 | x86 optable reference 2 | --------------------- 3 | (incomplete) 4 | 5 | P - modrm (reg mmx) 6 | PR - modrm (rm mmx, mod must be 11b) 7 | Q - modrm (rm mmx if mod=11b else mem) 8 | 9 | V - modrm (reg - xmm) 10 | VR - modrm (rm xmm, mod must be 11b) 11 | W - modrm (rm xmm if mod=11b else mem) 12 | MU - modrm (rm xmm if mod=11b else mem) lets us specify different sizes for reg and for mem. 13 | 14 | B - modrm (reg bounds) 15 | BM - modrm (rm bounds) 16 | 17 | K - modrm (reg opmask) 18 | KM - modrm (rm opmask if mod=11b else mem) 19 | KH - vex.vvvv (opmask) 20 | 21 | H - vex.vvvv xmm 22 | HR - vex.vvvv gpr 23 | L - xmm reg encoded in immediate byte 24 | XS - mem with base GPR and index XMM and a scale. (XSd, XSq) 25 | index will be xmm or ymm depending on vexl 26 | XSX - index will always be XMM 27 | XSY - index will always be YMM 28 | 29 | G - modrm (reg - gpr) 30 | S - modrm (reg - seg) 31 | VR - modrm (rm gpr, mod must be 11b) 32 | E - modrm (rm gpr if mod=11b else mem) 33 | M - modrm (mem), mod!=11b 34 | 35 | I - immediate 36 | J - relative immediate 37 | O - memory offset 38 | 39 | C - control reg 40 | D - debug reg 41 | 42 | 43 | opc <> 44 | 45 | /n - modrm reg field extends opcode 46 | /Mnn - disassembly mode extends opcode 47 | /Onn - operand mode extends opcode 48 | /mod=!11 - modrm mod field extends opcode 49 | 50 | 51 | -------------------------------------------------------------------------------- /docs/x86/optable.xsl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | x86 opcode table 7 | 30 | 31 | 32 | x86/optable.xml 33 | 34 | udis86.sourceforge.net 35 | 36 | 37 | Mnemonic 38 | Opcodes 39 | Vendor 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | ; 51 | ; 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | Copyright (c) 2008, Vivek Thampi 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /libudis86/Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | # -- udis86/libudis86 3 | # 4 | 5 | PYTHON = @PYTHON@ 6 | OPTABLE = @top_srcdir@/docs/x86/optable.xml 7 | 8 | MAINTAINERCLEANFILES = Makefile.in 9 | 10 | lib_LTLIBRARIES = libudis86.la 11 | 12 | libudis86_la_SOURCES = \ 13 | itab.c \ 14 | decode.c \ 15 | syn.c \ 16 | syn-intel.c \ 17 | syn-att.c \ 18 | udis86.c \ 19 | udint.h \ 20 | syn.h \ 21 | decode.h 22 | 23 | include_ladir = ${includedir}/libudis86 24 | include_la_HEADERS = \ 25 | types.h \ 26 | extern.h \ 27 | itab.h 28 | 29 | 30 | BUILT_SOURCES = \ 31 | itab.c \ 32 | itab.h 33 | 34 | # 35 | # DLLs may not contain undefined symbol references. 36 | # We have the linker check this explicitly. 37 | # 38 | if TARGET_WINDOWS 39 | libudis86_la_LDFLAGS = -no-undefined -version-info 0:0:0 40 | endif 41 | 42 | itab.c itab.h: $(OPTABLE) \ 43 | $(top_srcdir)/scripts/ud_itab.py \ 44 | $(top_srcdir)/scripts/ud_opcode.py 45 | $(PYTHON) $(top_srcdir)/scripts/ud_itab.py $(OPTABLE) $(srcdir) 46 | 47 | 48 | clean-local: 49 | rm -rf $(BUILT_SOURCES) 50 | 51 | maintainer-clean-local: 52 | -------------------------------------------------------------------------------- /libudis86/decode.h: -------------------------------------------------------------------------------- 1 | /* udis86 - libudis86/decode.h 2 | * 3 | * Copyright (c) 2002-2009 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #ifndef UD_DECODE_H 27 | #define UD_DECODE_H 28 | 29 | #include "types.h" 30 | #include "udint.h" 31 | #include "itab.h" 32 | 33 | #define MAX_INSN_LENGTH 15 34 | 35 | /* itab prefix bits */ 36 | #define P_none ( 0 ) 37 | 38 | #define P_inv64 ( 1 << 0 ) 39 | #define P_INV64(n) ( ( n >> 0 ) & 1 ) 40 | #define P_def64 ( 1 << 1 ) 41 | #define P_DEF64(n) ( ( n >> 1 ) & 1 ) 42 | 43 | #define P_oso ( 1 << 2 ) 44 | #define P_OSO(n) ( ( n >> 2 ) & 1 ) 45 | #define P_aso ( 1 << 3 ) 46 | #define P_ASO(n) ( ( n >> 3 ) & 1 ) 47 | 48 | #define P_rexb ( 1 << 4 ) 49 | #define P_REXB(n) ( ( n >> 4 ) & 1 ) 50 | #define P_rexw ( 1 << 5 ) 51 | #define P_REXW(n) ( ( n >> 5 ) & 1 ) 52 | #define P_rexr ( 1 << 6 ) 53 | #define P_REXR(n) ( ( n >> 6 ) & 1 ) 54 | #define P_rexx ( 1 << 7 ) 55 | #define P_REXX(n) ( ( n >> 7 ) & 1 ) 56 | 57 | #define P_seg ( 1 << 8 ) 58 | #define P_SEG(n) ( ( n >> 8 ) & 1 ) 59 | 60 | #define P_vexl ( 1 << 9 ) 61 | #define P_VEXL(n) ( ( n >> 9 ) & 1 ) 62 | #define P_vexw ( 1 << 10 ) 63 | #define P_VEXW(n) ( ( n >> 10 ) & 1 ) 64 | 65 | #define P_str ( 1 << 11 ) 66 | #define P_STR(n) ( ( n >> 11 ) & 1 ) 67 | #define P_strz ( 1 << 12 ) 68 | #define P_STR_ZF(n) ( ( n >> 12 ) & 1 ) 69 | 70 | /* operand type constants -- order is important! */ 71 | 72 | enum ud_operand_code { 73 | OP_NONE, 74 | 75 | OP_A, OP_E, OP_M, OP_G, 76 | OP_I, OP_F, 77 | 78 | OP_R0, OP_R1, OP_R2, OP_R3, 79 | OP_R4, OP_R5, OP_R6, OP_R7, 80 | 81 | OP_AL, OP_CL, OP_DL, 82 | OP_AX, OP_CX, OP_DX, 83 | OP_eAX, OP_eCX, OP_eDX, 84 | OP_rAX, OP_rCX, OP_rDX, 85 | 86 | OP_ES, OP_CS, OP_SS, OP_DS, 87 | OP_FS, OP_GS, 88 | 89 | OP_ST0, OP_ST1, OP_ST2, OP_ST3, 90 | OP_ST4, OP_ST5, OP_ST6, OP_ST7, 91 | 92 | OP_J, OP_S, OP_O, 93 | OP_I1, OP_I3, OP_sI, 94 | 95 | OP_V, OP_W, OP_Q, OP_P, 96 | OP_U, OP_N, OP_MU, OP_H, 97 | OP_HR, OP_L, OP_XS, OP_XSX, 98 | OP_XSY, 99 | 100 | OP_R, OP_C, OP_D, OP_B, 101 | OP_BM, OP_BMR, 102 | 103 | OP_K, OP_KM, OP_KH, 104 | 105 | OP_MR, 106 | 107 | OP_IMP_XMM0 108 | 109 | } UD_ATTR_PACKED; 110 | 111 | 112 | /* 113 | * Operand size constants 114 | * 115 | * Symbolic constants for various operand sizes. Some of these constants 116 | * are given a value equal to the width of the data (SZ_B == 8), such 117 | * that they maybe used interchangeably in the internals. Modifying them 118 | * will most certainly break things! 119 | */ 120 | typedef uint32_t ud_operand_size_t; 121 | 122 | #define SZ_NA 0 123 | #define SZ_Z 1 124 | #define SZ_V 2 125 | #define SZ_Y 3 126 | #define SZ_X 4 127 | #define SZ_RDQ 7 128 | #define SZ_B 8 129 | #define SZ_W 16 130 | #define SZ_D 32 131 | #define SZ_Q 64 132 | #define SZ_T 80 133 | #define SZ_O 128 134 | #define SZ_DQ 128 135 | #define SZ_QQ 256 136 | #define SZ_ZQ 512 137 | 138 | /* 139 | * Complex size types; that encode sizes for operands of type MR (memory or 140 | * register); for internal use only. Id space above 256. 141 | */ 142 | #define SZ_BD ((SZ_B << 16) | SZ_D) 143 | #define SZ_BV ((SZ_B << 16) | SZ_V) 144 | #define SZ_WD ((SZ_W << 16) | SZ_D) 145 | #define SZ_WV ((SZ_W << 16) | SZ_V) 146 | #define SZ_WY ((SZ_W << 16) | SZ_Y) 147 | #define SZ_DY ((SZ_D << 16) | SZ_Y) 148 | #define SZ_BO ((SZ_B << 16) | SZ_O) 149 | #define SZ_WO ((SZ_W << 16) | SZ_O) 150 | #define SZ_DO ((SZ_D << 16) | SZ_O) 151 | #define SZ_QO ((SZ_Q << 16) | SZ_O) 152 | #define SZ_DQO ((SZ_DQ << 16) | SZ_O) 153 | #define SZ_QQO ((SZ_QQ << 16) | SZ_O) 154 | #define SZ_ZQO ((SZ_ZQ << 16) | SZ_O) 155 | 156 | /* resolve complex size type. 157 | */ 158 | static UD_INLINE ud_operand_size_t 159 | Mx_mem_size(ud_operand_size_t size) 160 | { 161 | return (size >> 16) & 0xffff; 162 | } 163 | 164 | static UD_INLINE ud_operand_size_t 165 | Mx_reg_size(ud_operand_size_t size) 166 | { 167 | return size & 0xffff; 168 | } 169 | 170 | /* A single operand of an entry in the instruction table. 171 | * (internal use only) 172 | */ 173 | struct ud_itab_entry_operand 174 | { 175 | enum ud_operand_code type; 176 | ud_operand_size_t size; 177 | }; 178 | 179 | 180 | /* A single entry in an instruction table. 181 | *(internal use only) 182 | */ 183 | struct ud_itab_entry 184 | { 185 | enum ud_mnemonic_code mnemonic; 186 | struct ud_itab_entry_operand operand1; 187 | struct ud_itab_entry_operand operand2; 188 | struct ud_itab_entry_operand operand3; 189 | struct ud_itab_entry_operand operand4; 190 | uint8_t access1; 191 | uint8_t access2; 192 | uint8_t access3; 193 | uint8_t access4; 194 | uint32_t prefix; 195 | struct ud_flags flags; 196 | }; 197 | 198 | struct ud_lookup_table_list_entry { 199 | const uint16_t *table; 200 | enum ud_table_type type; 201 | const char *meta; 202 | const uint8_t limit; 203 | }; 204 | 205 | extern struct ud_itab_entry ud_itab[]; 206 | extern struct ud_lookup_table_list_entry ud_lookup_table_list[]; 207 | 208 | #endif /* UD_DECODE_H */ 209 | 210 | /* vim:cindent 211 | * vim:expandtab 212 | * vim:ts=4 213 | * vim:sw=4 214 | */ 215 | -------------------------------------------------------------------------------- /libudis86/extern.h: -------------------------------------------------------------------------------- 1 | /* udis86 - libudis86/extern.h 2 | * 3 | * Copyright (c) 2002-2009, 2013 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #ifndef UD_EXTERN_H 27 | #define UD_EXTERN_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | #include "types.h" 34 | 35 | #if defined(_MSC_VER) && defined(_USRDLL) 36 | # ifdef LIBUDIS86_EXPORTS 37 | # define LIBUDIS86_DLLEXTERN __declspec(dllexport) 38 | # else 39 | # define LIBUDIS86_DLLEXTERN __declspec(dllimport) 40 | # endif 41 | #else 42 | # define LIBUDIS86_DLLEXTERN 43 | #endif 44 | 45 | /* ============================= PUBLIC API ================================= */ 46 | 47 | extern LIBUDIS86_DLLEXTERN void ud_init(struct ud*); 48 | 49 | extern LIBUDIS86_DLLEXTERN void ud_set_mode(struct ud*, uint8_t); 50 | 51 | extern LIBUDIS86_DLLEXTERN void ud_set_pc(struct ud*, uint64_t); 52 | 53 | extern LIBUDIS86_DLLEXTERN void ud_set_input_hook(struct ud*, int (*)(struct ud*)); 54 | 55 | extern LIBUDIS86_DLLEXTERN void ud_set_input_buffer(struct ud*, const uint8_t*, size_t); 56 | 57 | #ifndef __UD_STANDALONE__ 58 | extern LIBUDIS86_DLLEXTERN void ud_set_input_file(struct ud*, FILE*); 59 | #endif /* __UD_STANDALONE__ */ 60 | 61 | extern LIBUDIS86_DLLEXTERN void ud_set_vendor(struct ud*, unsigned); 62 | 63 | extern LIBUDIS86_DLLEXTERN void ud_set_syntax(struct ud*, void (*)(struct ud*)); 64 | 65 | extern LIBUDIS86_DLLEXTERN void ud_input_skip(struct ud*, size_t); 66 | 67 | extern LIBUDIS86_DLLEXTERN int ud_input_end(const struct ud*); 68 | 69 | extern LIBUDIS86_DLLEXTERN unsigned int ud_decode(struct ud*); 70 | 71 | extern LIBUDIS86_DLLEXTERN unsigned int ud_disassemble(struct ud*); 72 | 73 | extern LIBUDIS86_DLLEXTERN void ud_translate_intel(struct ud*); 74 | 75 | extern LIBUDIS86_DLLEXTERN void ud_translate_att(struct ud*); 76 | 77 | extern LIBUDIS86_DLLEXTERN const char* ud_insn_asm(const struct ud* u); 78 | 79 | extern LIBUDIS86_DLLEXTERN const uint8_t* ud_insn_ptr(const struct ud* u); 80 | 81 | extern LIBUDIS86_DLLEXTERN uint64_t ud_insn_off(const struct ud*); 82 | 83 | extern LIBUDIS86_DLLEXTERN const char* ud_insn_hex(struct ud*); 84 | 85 | extern LIBUDIS86_DLLEXTERN unsigned int ud_insn_len(const struct ud* u); 86 | 87 | extern LIBUDIS86_DLLEXTERN const struct ud_operand* ud_insn_opr(const struct ud *u, unsigned int n); 88 | 89 | extern LIBUDIS86_DLLEXTERN int ud_opr_is_sreg(const struct ud_operand *opr); 90 | 91 | extern LIBUDIS86_DLLEXTERN int ud_opr_is_gpr(const struct ud_operand *opr); 92 | 93 | extern LIBUDIS86_DLLEXTERN enum ud_mnemonic_code ud_insn_mnemonic(const struct ud *u); 94 | 95 | extern LIBUDIS86_DLLEXTERN const char* ud_lookup_mnemonic(enum ud_mnemonic_code c); 96 | 97 | extern LIBUDIS86_DLLEXTERN const struct ud_flags* ud_lookup_flags(struct ud *u); 98 | 99 | extern LIBUDIS86_DLLEXTERN void ud_set_user_opaque_data(struct ud*, void*); 100 | 101 | extern LIBUDIS86_DLLEXTERN void* ud_get_user_opaque_data(const struct ud*); 102 | 103 | extern LIBUDIS86_DLLEXTERN void ud_set_asm_buffer(struct ud *u, char *buf, size_t size); 104 | 105 | extern LIBUDIS86_DLLEXTERN void ud_set_sym_resolver(struct ud *u, 106 | const char* (*resolver)(struct ud*, 107 | uint64_t addr, 108 | int64_t *offset)); 109 | 110 | /* ========================================================================== */ 111 | 112 | #ifdef __cplusplus 113 | } 114 | #endif 115 | #endif /* UD_EXTERN_H */ 116 | -------------------------------------------------------------------------------- /libudis86/syn-att.c: -------------------------------------------------------------------------------- 1 | /* udis86 - libudis86/syn-att.c 2 | * 3 | * Copyright (c) 2002-2009 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #include "types.h" 27 | #include "extern.h" 28 | #include "decode.h" 29 | #include "itab.h" 30 | #include "syn.h" 31 | #include "udint.h" 32 | 33 | /* ----------------------------------------------------------------------------- 34 | * opr_cast() - Prints an operand cast. 35 | * ----------------------------------------------------------------------------- 36 | */ 37 | static void 38 | opr_cast(struct ud* u, struct ud_operand* op) 39 | { 40 | switch(op->size) { 41 | case 16 : case 32 : 42 | ud_asmprintf(u, "*"); break; 43 | default: break; 44 | } 45 | } 46 | 47 | /* ----------------------------------------------------------------------------- 48 | * gen_operand() - Generates assembly output for each operand. 49 | * ----------------------------------------------------------------------------- 50 | */ 51 | static void 52 | gen_operand(struct ud* u, struct ud_operand* op) 53 | { 54 | switch(op->type) { 55 | case UD_OP_CONST: 56 | ud_asmprintf(u, "$0x%x", op->lval.udword); 57 | break; 58 | 59 | case UD_OP_REG: 60 | ud_asmprintf(u, "%%%s", ud_reg_tab[op->base - UD_R_AL]); 61 | break; 62 | 63 | case UD_OP_MEM: 64 | if (u->br_far) { 65 | opr_cast(u, op); 66 | } 67 | if (u->pfx_seg) { 68 | ud_asmprintf(u, "%%%s:", ud_reg_tab[u->pfx_seg - UD_R_AL]); 69 | } 70 | if (op->offset != 0) { 71 | ud_syn_print_mem_disp(u, op, 0); 72 | } 73 | if (op->base) { 74 | ud_asmprintf(u, "(%%%s", ud_reg_tab[op->base - UD_R_AL]); 75 | } 76 | if (op->index) { 77 | if (op->base) { 78 | ud_asmprintf(u, ","); 79 | } else { 80 | ud_asmprintf(u, "("); 81 | } 82 | ud_asmprintf(u, "%%%s", ud_reg_tab[op->index - UD_R_AL]); 83 | } 84 | if (op->scale) { 85 | ud_asmprintf(u, ",%d", op->scale); 86 | } 87 | if (op->base || op->index) { 88 | ud_asmprintf(u, ")"); 89 | } 90 | break; 91 | 92 | case UD_OP_IMM: 93 | ud_asmprintf(u, "$"); 94 | ud_syn_print_imm(u, op); 95 | break; 96 | 97 | case UD_OP_JIMM: 98 | ud_syn_print_addr(u, ud_syn_rel_target(u, op)); 99 | break; 100 | 101 | case UD_OP_PTR: 102 | switch (op->size) { 103 | case 32: 104 | ud_asmprintf(u, "$0x%x, $0x%x", op->lval.ptr.seg, 105 | op->lval.ptr.off & 0xFFFF); 106 | break; 107 | case 48: 108 | ud_asmprintf(u, "$0x%x, $0x%x", op->lval.ptr.seg, 109 | op->lval.ptr.off); 110 | break; 111 | } 112 | break; 113 | 114 | default: return; 115 | } 116 | } 117 | 118 | /* ============================================================================= 119 | * translates to AT&T syntax 120 | * ============================================================================= 121 | */ 122 | extern void 123 | ud_translate_att(struct ud *u) 124 | { 125 | int size = 0; 126 | int star = 0; 127 | 128 | /* check if P_OSO prefix is used */ 129 | if (! P_OSO(u->itab_entry->prefix) && u->pfx_opr) { 130 | switch (u->dis_mode) { 131 | case 16: 132 | ud_asmprintf(u, "o32 "); 133 | break; 134 | case 32: 135 | case 64: 136 | ud_asmprintf(u, "o16 "); 137 | break; 138 | } 139 | } 140 | 141 | /* check if P_ASO prefix was used */ 142 | if (! P_ASO(u->itab_entry->prefix) && u->pfx_adr) { 143 | switch (u->dis_mode) { 144 | case 16: 145 | ud_asmprintf(u, "a32 "); 146 | break; 147 | case 32: 148 | ud_asmprintf(u, "a16 "); 149 | break; 150 | case 64: 151 | ud_asmprintf(u, "a32 "); 152 | break; 153 | } 154 | } 155 | 156 | if (u->pfx_xacquire) { 157 | ud_asmprintf(u, "xacquire "); 158 | } 159 | else if (u->pfx_xrelease) { 160 | ud_asmprintf(u, "xrelease "); 161 | } 162 | 163 | if (u->pfx_lock) 164 | ud_asmprintf(u, "lock "); 165 | 166 | if (u->pfx_bnd) 167 | ud_asmprintf(u, "bnd "); 168 | 169 | if (u->pfx_rep) { 170 | ud_asmprintf(u, "rep "); 171 | } else if (u->pfx_repe) { 172 | ud_asmprintf(u, "repe "); 173 | } else if (u->pfx_repne) { 174 | ud_asmprintf(u, "repne "); 175 | } 176 | 177 | /* special instructions */ 178 | switch (u->mnemonic) { 179 | case UD_Iretf: 180 | ud_asmprintf(u, "lret "); 181 | break; 182 | case UD_Idb: 183 | ud_asmprintf(u, ".byte 0x%x", u->operand[0].lval.ubyte); 184 | return; 185 | case UD_Ijmp: 186 | case UD_Icall: 187 | if (u->br_far) ud_asmprintf(u, "l"); 188 | if (u->operand[0].type == UD_OP_REG) { 189 | star = 1; 190 | } 191 | ud_asmprintf(u, "%s", ud_lookup_mnemonic(u->mnemonic)); 192 | break; 193 | case UD_Ibound: 194 | case UD_Ienter: 195 | if (u->operand[0].type != UD_NONE) 196 | gen_operand(u, &u->operand[0]); 197 | if (u->operand[1].type != UD_NONE) { 198 | ud_asmprintf(u, ","); 199 | gen_operand(u, &u->operand[1]); 200 | } 201 | return; 202 | default: 203 | ud_asmprintf(u, "%s", ud_lookup_mnemonic(u->mnemonic)); 204 | } 205 | 206 | if (size == 8) { 207 | ud_asmprintf(u, "b"); 208 | } else if (size == 16) { 209 | ud_asmprintf(u, "w"); 210 | } else if (size == 64) { 211 | ud_asmprintf(u, "q"); 212 | } 213 | 214 | if (star) { 215 | ud_asmprintf(u, " *"); 216 | } else { 217 | ud_asmprintf(u, " "); 218 | } 219 | 220 | if (u->operand[3].type != UD_NONE) { 221 | gen_operand(u, &u->operand[3]); 222 | ud_asmprintf(u, ", "); 223 | } 224 | if (u->operand[2].type != UD_NONE) { 225 | gen_operand(u, &u->operand[2]); 226 | ud_asmprintf(u, ", "); 227 | } 228 | if (u->operand[1].type != UD_NONE) { 229 | gen_operand(u, &u->operand[1]); 230 | ud_asmprintf(u, ", "); 231 | } 232 | if (u->operand[0].type != UD_NONE) { 233 | gen_operand(u, &u->operand[0]); 234 | } 235 | } 236 | 237 | /* 238 | vim: set ts=2 sw=2 expandtab 239 | */ 240 | -------------------------------------------------------------------------------- /libudis86/syn-intel.c: -------------------------------------------------------------------------------- 1 | /* udis86 - libudis86/syn-intel.c 2 | * 3 | * Copyright (c) 2002-2013 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #include "types.h" 27 | #include "extern.h" 28 | #include "decode.h" 29 | #include "itab.h" 30 | #include "syn.h" 31 | #include "udint.h" 32 | 33 | /* ----------------------------------------------------------------------------- 34 | * opr_cast() - Prints an operand cast. 35 | * ----------------------------------------------------------------------------- 36 | */ 37 | static void 38 | opr_cast(struct ud* u, struct ud_operand* op) 39 | { 40 | if (u->br_far) { 41 | ud_asmprintf(u, "far "); 42 | } 43 | 44 | // Note: lea instruction will have op size == 0. 45 | 46 | switch(op->size) { 47 | case 8: ud_asmprintf(u, "byte " ); break; 48 | case 16: ud_asmprintf(u, "word " ); break; 49 | case 32: ud_asmprintf(u, "dword "); break; 50 | case 64: ud_asmprintf(u, "qword "); break; 51 | case 80: ud_asmprintf(u, "xword "); break; 52 | case 128: ud_asmprintf(u, "xmmword "); break; 53 | case 256: ud_asmprintf(u, "ymmword "); break; 54 | case 512: ud_asmprintf(u, "zmmword "); break; 55 | default: break; 56 | } 57 | } 58 | 59 | /* ----------------------------------------------------------------------------- 60 | * gen_operand() - Generates assembly output for each operand. 61 | * ----------------------------------------------------------------------------- 62 | */ 63 | static void gen_operand(struct ud* u, struct ud_operand* op) 64 | { 65 | switch(op->type) { 66 | case UD_OP_REG: 67 | ud_asmprintf(u, "%s", ud_reg_tab[op->base - UD_R_AL]); 68 | break; 69 | 70 | case UD_OP_MEM: 71 | 72 | opr_cast(u, op); 73 | if (u->pfx_seg) { 74 | ud_asmprintf(u, "%s:", ud_reg_tab[u->pfx_seg - UD_R_AL]); 75 | } 76 | 77 | ud_asmprintf(u, "["); 78 | 79 | if (op->base) { 80 | ud_asmprintf(u, "%s", ud_reg_tab[op->base - UD_R_AL]); 81 | } 82 | if (op->index) { 83 | ud_asmprintf(u, "%s%s", op->base != UD_NONE? "+" : "", 84 | ud_reg_tab[op->index - UD_R_AL]); 85 | if (op->scale) { 86 | ud_asmprintf(u, "*%d", op->scale); 87 | } 88 | } 89 | if (op->offset != 0) { 90 | ud_syn_print_mem_disp(u, op, (op->base != UD_NONE || 91 | op->index != UD_NONE) ? 1 : 0); 92 | } 93 | ud_asmprintf(u, "]"); 94 | break; 95 | 96 | case UD_OP_IMM: 97 | ud_syn_print_imm(u, op); 98 | break; 99 | 100 | 101 | case UD_OP_JIMM: 102 | ud_syn_print_addr(u, ud_syn_rel_target(u, op)); 103 | break; 104 | 105 | case UD_OP_PTR: 106 | switch (op->size) { 107 | case 32: 108 | ud_asmprintf(u, "word 0x%x:0x%x", op->lval.ptr.seg, 109 | op->lval.ptr.off & 0xFFFF); 110 | break; 111 | case 48: 112 | ud_asmprintf(u, "dword 0x%x:0x%x", op->lval.ptr.seg, 113 | op->lval.ptr.off); 114 | break; 115 | } 116 | break; 117 | 118 | case UD_OP_CONST: 119 | ud_asmprintf(u, "%d", op->lval.udword); 120 | break; 121 | 122 | default: return; 123 | } 124 | } 125 | 126 | /* ============================================================================= 127 | * translates to intel syntax 128 | * ============================================================================= 129 | */ 130 | extern void 131 | ud_translate_intel(struct ud* u) 132 | { 133 | /* check if P_OSO prefix is used */ 134 | if (!P_OSO(u->itab_entry->prefix) && u->pfx_opr) { 135 | switch (u->dis_mode) { 136 | case 16: ud_asmprintf(u, "o32 "); break; 137 | case 32: 138 | case 64: ud_asmprintf(u, "o16 "); break; 139 | } 140 | } 141 | 142 | /* check if P_ASO prefix was used */ 143 | if (!P_ASO(u->itab_entry->prefix) && u->pfx_adr) { 144 | switch (u->dis_mode) { 145 | case 16: ud_asmprintf(u, "a32 "); break; 146 | case 32: ud_asmprintf(u, "a16 "); break; 147 | case 64: ud_asmprintf(u, "a32 "); break; 148 | } 149 | } 150 | 151 | if (u->pfx_seg && 152 | u->operand[0].type != UD_OP_MEM && 153 | u->operand[1].type != UD_OP_MEM ) { 154 | ud_asmprintf(u, "%s ", ud_reg_tab[u->pfx_seg - UD_R_AL]); 155 | } 156 | 157 | if (u->pfx_xacquire) { 158 | ud_asmprintf(u, "xacquire "); 159 | } 160 | else if (u->pfx_xrelease) { 161 | ud_asmprintf(u, "xrelease "); 162 | } 163 | 164 | if (u->pfx_lock) { 165 | ud_asmprintf(u, "lock "); 166 | } 167 | 168 | if (u->pfx_bnd) { 169 | ud_asmprintf(u, "bnd "); 170 | } 171 | 172 | if (u->pfx_rep) { 173 | ud_asmprintf(u, "rep "); 174 | } else if (u->pfx_repe) { 175 | ud_asmprintf(u, "repe "); 176 | } else if (u->pfx_repne) { 177 | ud_asmprintf(u, "repne "); 178 | } 179 | 180 | /* print the instruction mnemonic */ 181 | ud_asmprintf(u, "%s", ud_lookup_mnemonic(u->mnemonic)); 182 | 183 | if (u->operand[0].type != UD_NONE) { 184 | ud_asmprintf(u, " "); 185 | gen_operand(u, &u->operand[0]); 186 | } 187 | 188 | if (u->operand[1].type != UD_NONE) { 189 | ud_asmprintf(u, ", "); 190 | gen_operand(u, &u->operand[1]); 191 | } 192 | 193 | if (u->operand[2].type != UD_NONE) { 194 | ud_asmprintf(u, ", "); 195 | gen_operand(u, &u->operand[2]); 196 | } 197 | 198 | if (u->operand[3].type != UD_NONE) { 199 | ud_asmprintf(u, ", "); 200 | gen_operand(u, &u->operand[3]); 201 | } 202 | } 203 | 204 | /* 205 | vim: set ts=2 sw=2 expandtab 206 | */ 207 | -------------------------------------------------------------------------------- /libudis86/syn.c: -------------------------------------------------------------------------------- 1 | /* udis86 - libudis86/syn.c 2 | * 3 | * Copyright (c) 2002-2013 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #include "types.h" 27 | #include "decode.h" 28 | #include "syn.h" 29 | #include "udint.h" 30 | 31 | /* 32 | * Register Table - Order Matters (types.h)! 33 | * 34 | */ 35 | const char* ud_reg_tab[] = 36 | { 37 | "al", "cl", "dl", "bl", 38 | "ah", "ch", "dh", "bh", 39 | "spl", "bpl", "sil", "dil", 40 | "r8b", "r9b", "r10b", "r11b", 41 | "r12b", "r13b", "r14b", "r15b", 42 | 43 | "ax", "cx", "dx", "bx", 44 | "sp", "bp", "si", "di", 45 | "r8w", "r9w", "r10w", "r11w", 46 | "r12w", "r13w", "r14w", "r15w", 47 | 48 | "eax", "ecx", "edx", "ebx", 49 | "esp", "ebp", "esi", "edi", 50 | "r8d", "r9d", "r10d", "r11d", 51 | "r12d", "r13d", "r14d", "r15d", 52 | 53 | "rax", "rcx", "rdx", "rbx", 54 | "rsp", "rbp", "rsi", "rdi", 55 | "r8", "r9", "r10", "r11", 56 | "r12", "r13", "r14", "r15", 57 | 58 | "es", "cs", "ss", "ds", 59 | "fs", "gs", 60 | 61 | "cr0", "cr1", "cr2", "cr3", 62 | "cr4", "cr5", "cr6", "cr7", 63 | "cr8", "cr9", "cr10", "cr11", 64 | "cr12", "cr13", "cr14", "cr15", 65 | 66 | "dr0", "dr1", "dr2", "dr3", 67 | "dr4", "dr5", "dr6", "dr7", 68 | "dr8", "dr9", "dr10", "dr11", 69 | "dr12", "dr13", "dr14", "dr15", 70 | 71 | "mm0", "mm1", "mm2", "mm3", 72 | "mm4", "mm5", "mm6", "mm7", 73 | 74 | "st0", "st1", "st2", "st3", 75 | "st4", "st5", "st6", "st7", 76 | 77 | "xmm0", "xmm1", "xmm2", "xmm3", 78 | "xmm4", "xmm5", "xmm6", "xmm7", 79 | "xmm8", "xmm9", "xmm10", "xmm11", 80 | "xmm12", "xmm13", "xmm14", "xmm15", 81 | "xmm16", "xmm17", "xmm18", "xmm19", 82 | "xmm20", "xmm21", "xmm22", "xmm23", 83 | "xmm24", "xmm25", "xmm26", "xmm27", 84 | "xmm28", "xmm29", "xmm30", "xmm31", 85 | 86 | "ymm0", "ymm1", "ymm2", "ymm3", 87 | "ymm4", "ymm5", "ymm6", "ymm7", 88 | "ymm8", "ymm9", "ymm10", "ymm11", 89 | "ymm12", "ymm13", "ymm14", "ymm15", 90 | "ymm16", "ymm17", "ymm18", "ymm19", 91 | "ymm20", "ymm21", "ymm22", "ymm23", 92 | "ymm24", "ymm25", "ymm26", "ymm27", 93 | "ymm28", "ymm29", "ymm30", "ymm31", 94 | 95 | "zmm0", "zmm1", "zmm2", "zmm3", 96 | "zmm4", "zmm5", "zmm6", "zmm7", 97 | "zmm8", "zmm9", "zmm10", "zmm11", 98 | "zmm12", "zmm13", "zmm14", "zmm15", 99 | "zmm16", "zmm17", "zmm18", "zmm19", 100 | "zmm20", "zmm21", "zmm22", "zmm23", 101 | "zmm24", "zmm25", "zmm26", "zmm27", 102 | "zmm28", "zmm29", "zmm30", "zmm31", 103 | 104 | "k0", "k1", "k2", "k3", 105 | "k4", "k5", "k6", "k7", 106 | 107 | "bnd0", "bnd1", "bnd2", "bnd3", 108 | 109 | "rip" 110 | }; 111 | 112 | /* 113 | * Flag Table - Order Matters (types.h)! 114 | * 115 | */ 116 | const char* ud_flag_tab[] = 117 | { 118 | "of", 119 | "sf", 120 | "zf", 121 | "af", 122 | "pf", 123 | "cf", 124 | "tf", 125 | "if", 126 | "df", 127 | "nf", 128 | "rf", 129 | "ac" 130 | }; 131 | 132 | uint64_t ud_syn_rel_target(struct ud *u, struct ud_operand *opr) 133 | { 134 | // https://github.com/radare/udis86/commit/968a72a6cb555686b9771ffbdd8aa44019335ab3 135 | // https://github.com/radare/udis86/commit/87312274bf0bda13e0629de1681dcf6a5abd0327 136 | uint64_t trunc_mask = 0xffffffffffffffffull; 137 | 138 | if( u->dis_mode < 32 ) 139 | trunc_mask >>= (64 - u->opr_mode); 140 | 141 | switch( opr->size ) 142 | { 143 | case 8: 144 | { 145 | return (u->pc + opr->lval.sbyte) & trunc_mask; 146 | } 147 | case 16: 148 | { 149 | // https://github.com/radare/udis86/commit/c618b2871b22c3504ca3b5615107e12ec6558ead 150 | int delta = (opr->lval.sword & trunc_mask); 151 | if( (u->pc + delta) > 0xffff ) 152 | return (u->pc & 0xf0000) + ((u->pc + delta) & 0xffff); 153 | 154 | return ( u->pc + delta ); 155 | } 156 | case 32: 157 | { 158 | return (u->pc + opr->lval.sdword) & trunc_mask; 159 | } 160 | default: 161 | { 162 | UD_ASSERT( !"invalid relative offset size." ); 163 | return 0ull; 164 | } 165 | } 166 | } 167 | 168 | 169 | /* 170 | * asmprintf 171 | * Printf style function for printing translated assembly 172 | * output. Returns the number of characters written and 173 | * moves the buffer pointer forward. On an overflow, 174 | * returns a negative number and truncates the output. 175 | */ 176 | int 177 | ud_asmprintf(struct ud *u, const char *fmt, ...) 178 | { 179 | int ret; 180 | int avail; 181 | va_list ap; 182 | va_start(ap, fmt); 183 | avail = u->asm_buf_size - u->asm_buf_fill - 1 /* nullchar */; 184 | ret = vsnprintf((char*) u->asm_buf + u->asm_buf_fill, avail, fmt, ap); 185 | if (ret < 0 || ret > avail) { 186 | u->asm_buf_fill = u->asm_buf_size - 1; 187 | } else { 188 | u->asm_buf_fill += ret; 189 | } 190 | va_end(ap); 191 | return ret; 192 | } 193 | 194 | 195 | void 196 | ud_syn_print_addr(struct ud *u, uint64_t addr) 197 | { 198 | const char *name = NULL; 199 | if (u->sym_resolver) { 200 | int64_t offset = 0; 201 | name = u->sym_resolver(u, addr, &offset); 202 | if (name) { 203 | if (offset) { 204 | ud_asmprintf(u, "%s%+" FMT64 "d", name, offset); 205 | } else { 206 | ud_asmprintf(u, "%s", name); 207 | } 208 | return; 209 | } 210 | } 211 | ud_asmprintf(u, "0x%" FMT64 "x", addr); 212 | } 213 | 214 | 215 | void 216 | ud_syn_print_imm(struct ud* u, const struct ud_operand *op) 217 | { 218 | uint64_t v; 219 | if (op->_oprcode == OP_sI && op->size != u->opr_mode) { 220 | if (op->size == 8) { 221 | v = (int64_t)op->lval.sbyte; 222 | } else { 223 | UD_ASSERT(op->size == 32); 224 | v = (int64_t)op->lval.sdword; 225 | } 226 | if (u->opr_mode < 64) { 227 | v = v & ((1ull << u->opr_mode) - 1ull); 228 | } 229 | } else { 230 | switch (op->size) { 231 | case 8 : v = op->lval.ubyte; break; 232 | case 16: v = op->lval.uword; break; 233 | case 32: v = op->lval.udword; break; 234 | case 64: v = op->lval.uqword; break; 235 | default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */ 236 | } 237 | } 238 | ud_asmprintf(u, "0x%" FMT64 "x", v); 239 | } 240 | 241 | 242 | void 243 | ud_syn_print_mem_disp(struct ud* u, const struct ud_operand *op, int sign) 244 | { 245 | UD_ASSERT(op->offset != 0); 246 | if (op->base == UD_NONE && op->index == UD_NONE) { 247 | uint64_t v; 248 | UD_ASSERT(op->scale == UD_NONE && op->offset != 8); 249 | /* unsigned mem-offset */ 250 | switch (op->offset) { 251 | case 16: v = op->lval.uword; break; 252 | case 32: v = op->lval.udword; break; 253 | case 64: v = op->lval.uqword; break; 254 | default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */ 255 | } 256 | ud_asmprintf(u, "0x%" FMT64 "x", v); 257 | } else { 258 | int64_t v; 259 | UD_ASSERT(op->offset != 64); 260 | switch (op->offset) { 261 | case 8 : v = op->lval.sbyte; break; 262 | case 16: v = op->lval.sword; break; 263 | case 32: v = op->lval.sdword; break; 264 | default: UD_ASSERT(!"invalid offset"); v = 0; /* keep cc happy */ 265 | } 266 | if (v < 0) { 267 | ud_asmprintf(u, "-0x%" FMT64 "x", -v); 268 | } else if (v > 0) { 269 | ud_asmprintf(u, "%s0x%" FMT64 "x", sign? "+" : "", v); 270 | } 271 | } 272 | } 273 | 274 | /* 275 | vim: set ts=2 sw=2 expandtab 276 | */ 277 | -------------------------------------------------------------------------------- /libudis86/syn.h: -------------------------------------------------------------------------------- 1 | /* udis86 - libudis86/syn.h 2 | * 3 | * Copyright (c) 2002-2009 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #ifndef UD_SYN_H 27 | #define UD_SYN_H 28 | 29 | #include "types.h" 30 | #ifndef __UD_STANDALONE__ 31 | # include 32 | #endif /* __UD_STANDALONE__ */ 33 | 34 | extern const char* ud_reg_tab[]; 35 | extern const char* ud_flag_tab[]; 36 | 37 | uint64_t ud_syn_rel_target(struct ud*, struct ud_operand*); 38 | 39 | #ifdef __GNUC__ 40 | int ud_asmprintf(struct ud *u, const char *fmt, ...) 41 | __attribute__ ((format (printf, 2, 3))); 42 | #else 43 | int ud_asmprintf(struct ud *u, const char *fmt, ...); 44 | #endif 45 | 46 | void ud_syn_print_addr(struct ud *u, uint64_t addr); 47 | void ud_syn_print_imm(struct ud* u, const struct ud_operand *op); 48 | void ud_syn_print_mem_disp(struct ud* u, const struct ud_operand *, int sign); 49 | 50 | #endif /* UD_SYN_H */ 51 | 52 | /* 53 | vim: set ts=2 sw=2 expandtab 54 | */ 55 | -------------------------------------------------------------------------------- /libudis86/types.h: -------------------------------------------------------------------------------- 1 | /* udis86 - libudis86/types.h 2 | * 3 | * Copyright (c) 2002-2013 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #ifndef UD_TYPES_H 27 | #define UD_TYPES_H 28 | 29 | #ifdef __KERNEL__ 30 | /* 31 | * -D__KERNEL__ is automatically passed on the command line when 32 | * building something as part of the Linux kernel. Assume standalone 33 | * mode. 34 | */ 35 | # include 36 | # include 37 | # ifndef __UD_STANDALONE__ 38 | # define __UD_STANDALONE__ 1 39 | # endif 40 | #endif /* __KERNEL__ */ 41 | 42 | #if !defined(__UD_STANDALONE__) 43 | # include 44 | # include 45 | #endif 46 | 47 | /* gcc specific extensions */ 48 | #ifdef __GNUC__ 49 | # define UD_ATTR_PACKED __attribute__((packed)) 50 | #else 51 | # define UD_ATTR_PACKED 52 | #endif /* UD_ATTR_PACKED */ 53 | 54 | 55 | /* ----------------------------------------------------------------------------- 56 | * All possible "types" of objects in udis86. Order is Important! 57 | * 58 | * NOTE: *** modify type2value() when adding new types here *** 59 | * 60 | * ----------------------------------------------------------------------------- 61 | */ 62 | enum ud_type 63 | { 64 | UD_NONE=0, 65 | 66 | // Note: UD_R_AL must be first register. 67 | 68 | /* 8 bit GPRs */ 69 | UD_R_AL, UD_R_CL, UD_R_DL, UD_R_BL, 70 | UD_R_AH, UD_R_CH, UD_R_DH, UD_R_BH, 71 | UD_R_SPL, UD_R_BPL, UD_R_SIL, UD_R_DIL, 72 | UD_R_R8B, UD_R_R9B, UD_R_R10B, UD_R_R11B, 73 | UD_R_R12B, UD_R_R13B, UD_R_R14B, UD_R_R15B, 74 | 75 | /* 16 bit GPRs */ 76 | UD_R_AX, UD_R_CX, UD_R_DX, UD_R_BX, 77 | UD_R_SP, UD_R_BP, UD_R_SI, UD_R_DI, 78 | UD_R_R8W, UD_R_R9W, UD_R_R10W, UD_R_R11W, 79 | UD_R_R12W, UD_R_R13W, UD_R_R14W, UD_R_R15W, 80 | 81 | /* 32 bit GPRs */ 82 | UD_R_EAX, UD_R_ECX, UD_R_EDX, UD_R_EBX, 83 | UD_R_ESP, UD_R_EBP, UD_R_ESI, UD_R_EDI, 84 | UD_R_R8D, UD_R_R9D, UD_R_R10D, UD_R_R11D, 85 | UD_R_R12D, UD_R_R13D, UD_R_R14D, UD_R_R15D, 86 | 87 | /* 64 bit GPRs */ 88 | UD_R_RAX, UD_R_RCX, UD_R_RDX, UD_R_RBX, 89 | UD_R_RSP, UD_R_RBP, UD_R_RSI, UD_R_RDI, 90 | UD_R_R8, UD_R_R9, UD_R_R10, UD_R_R11, 91 | UD_R_R12, UD_R_R13, UD_R_R14, UD_R_R15, 92 | 93 | /* segment registers */ 94 | UD_R_ES, UD_R_CS, UD_R_SS, UD_R_DS, 95 | UD_R_FS, UD_R_GS, 96 | 97 | /* control registers*/ 98 | UD_R_CR0, UD_R_CR1, UD_R_CR2, UD_R_CR3, 99 | UD_R_CR4, UD_R_CR5, UD_R_CR6, UD_R_CR7, 100 | UD_R_CR8, UD_R_CR9, UD_R_CR10, UD_R_CR11, 101 | UD_R_CR12, UD_R_CR13, UD_R_CR14, UD_R_CR15, 102 | 103 | /* debug registers */ 104 | UD_R_DR0, UD_R_DR1, UD_R_DR2, UD_R_DR3, 105 | UD_R_DR4, UD_R_DR5, UD_R_DR6, UD_R_DR7, 106 | UD_R_DR8, UD_R_DR9, UD_R_DR10, UD_R_DR11, 107 | UD_R_DR12, UD_R_DR13, UD_R_DR14, UD_R_DR15, 108 | 109 | /* mmx registers */ 110 | UD_R_MM0, UD_R_MM1, UD_R_MM2, UD_R_MM3, 111 | UD_R_MM4, UD_R_MM5, UD_R_MM6, UD_R_MM7, 112 | 113 | /* x87 registers */ 114 | UD_R_ST0, UD_R_ST1, UD_R_ST2, UD_R_ST3, 115 | UD_R_ST4, UD_R_ST5, UD_R_ST6, UD_R_ST7, 116 | 117 | /* extended multimedia registers */ 118 | UD_R_XMM0, UD_R_XMM1, UD_R_XMM2, UD_R_XMM3, 119 | UD_R_XMM4, UD_R_XMM5, UD_R_XMM6, UD_R_XMM7, 120 | UD_R_XMM8, UD_R_XMM9, UD_R_XMM10, UD_R_XMM11, 121 | UD_R_XMM12, UD_R_XMM13, UD_R_XMM14, UD_R_XMM15, 122 | UD_R_XMM16, UD_R_XMM17, UD_R_XMM18, UD_R_XMM19, 123 | UD_R_XMM20, UD_R_XMM21, UD_R_XMM22, UD_R_XMM23, 124 | UD_R_XMM24, UD_R_XMM25, UD_R_XMM26, UD_R_XMM27, 125 | UD_R_XMM28, UD_R_XMM29, UD_R_XMM30, UD_R_XMM31, 126 | 127 | /* 256B multimedia registers */ 128 | UD_R_YMM0, UD_R_YMM1, UD_R_YMM2, UD_R_YMM3, 129 | UD_R_YMM4, UD_R_YMM5, UD_R_YMM6, UD_R_YMM7, 130 | UD_R_YMM8, UD_R_YMM9, UD_R_YMM10, UD_R_YMM11, 131 | UD_R_YMM12, UD_R_YMM13, UD_R_YMM14, UD_R_YMM15, 132 | UD_R_YMM16, UD_R_YMM17, UD_R_YMM18, UD_R_YMM19, 133 | UD_R_YMM20, UD_R_YMM21, UD_R_YMM22, UD_R_YMM23, 134 | UD_R_YMM24, UD_R_YMM25, UD_R_YMM26, UD_R_YMM27, 135 | UD_R_YMM28, UD_R_YMM29, UD_R_YMM30, UD_R_YMM31, 136 | 137 | /* 512B multimedia registers */ 138 | UD_R_ZMM0, UD_R_ZMM1, UD_R_ZMM2, UD_R_ZMM3, 139 | UD_R_ZMM4, UD_R_ZMM5, UD_R_ZMM6, UD_R_ZMM7, 140 | UD_R_ZMM8, UD_R_ZMM9, UD_R_ZMM10, UD_R_ZMM11, 141 | UD_R_ZMM12, UD_R_ZMM13, UD_R_ZMM14, UD_R_ZMM15, 142 | UD_R_ZMM16, UD_R_ZMM17, UD_R_ZMM18, UD_R_ZMM19, 143 | UD_R_ZMM20, UD_R_ZMM21, UD_R_ZMM22, UD_R_ZMM23, 144 | UD_R_ZMM24, UD_R_ZMM25, UD_R_ZMM26, UD_R_ZMM27, 145 | UD_R_ZMM28, UD_R_ZMM29, UD_R_ZMM30, UD_R_ZMM31, 146 | 147 | /* AVX512 Opmask Registers */ 148 | UD_R_K0, UD_R_K1, UD_R_K2, UD_R_K3, 149 | UD_R_K4, UD_R_K5, UD_R_K6, UD_R_K7, 150 | 151 | /* Bounds Registers (Intel MPX)*/ 152 | UD_R_BND0, UD_R_BND1, UD_R_BND2, UD_R_BND3, 153 | 154 | // Note: must be last register 155 | UD_R_RIP, 156 | 157 | /* Operand Types */ 158 | UD_OP_REG, UD_OP_MEM, UD_OP_PTR, UD_OP_IMM, 159 | UD_OP_JIMM, UD_OP_CONST 160 | }; 161 | 162 | #define UD_ACCESS_NONE 0 163 | #define UD_ACCESS_READ (1 << 1) 164 | #define UD_ACCESS_WRITE (1 << 2) 165 | 166 | enum ud_flag_state 167 | { 168 | UD_FLAG_UNCHANGED, 169 | UD_FLAG_TESTED, 170 | UD_FLAG_MODIFIED, 171 | UD_FLAG_RESET, 172 | UD_FLAG_SET, 173 | UD_FLAG_UNDEFINED, 174 | UD_FLAG_PRIOR 175 | }; 176 | 177 | #define UD_FLAG_OF 0 178 | #define UD_FLAG_SF 1 179 | #define UD_FLAG_ZF 2 180 | #define UD_FLAG_AF 3 181 | #define UD_FLAG_PF 4 182 | #define UD_FLAG_CF 5 183 | #define UD_FLAG_TF 6 184 | #define UD_FLAG_IF 7 185 | #define UD_FLAG_DF 8 186 | #define UD_FLAG_NF 9 187 | #define UD_FLAG_RF 10 188 | #define UD_FLAG_AC 11 189 | 190 | #define UD_FLAG_MAX (UD_FLAG_AC+1) 191 | 192 | /* This structure describes the state of the EFLAGS register 193 | * once an instruction has been executed. 194 | */ 195 | struct ud_flags 196 | { 197 | enum ud_flag_state flag[UD_FLAG_MAX]; 198 | }; 199 | 200 | #include "itab.h" 201 | 202 | union ud_lval { 203 | int8_t sbyte; 204 | uint8_t ubyte; 205 | int16_t sword; 206 | uint16_t uword; 207 | int32_t sdword; 208 | uint32_t udword; 209 | int64_t sqword; 210 | uint64_t uqword; 211 | struct { 212 | uint16_t seg; 213 | uint32_t off; 214 | } ptr; 215 | }; 216 | 217 | /* ----------------------------------------------------------------------------- 218 | * struct ud_operand - Disassembled instruction Operand. 219 | * ----------------------------------------------------------------------------- 220 | */ 221 | struct ud_operand { 222 | union ud_lval lval; 223 | enum ud_type type; 224 | //enum ud_type opmask; // AVX512 opmask, ko-k7 225 | enum ud_type base; 226 | enum ud_type index; 227 | uint16_t size; 228 | uint8_t scale; 229 | uint8_t offset; 230 | uint8_t access; 231 | /* 232 | * internal use only 233 | */ 234 | uint8_t _oprcode; 235 | }; 236 | 237 | /* ----------------------------------------------------------------------------- 238 | * struct ud - The udis86 object. 239 | * ----------------------------------------------------------------------------- 240 | */ 241 | struct ud 242 | { 243 | /* 244 | * input buffering 245 | */ 246 | int (*inp_hook) (struct ud*); 247 | #ifndef __UD_STANDALONE__ 248 | FILE* inp_file; 249 | #endif 250 | const uint8_t* inp_buf; 251 | size_t inp_buf_size; 252 | size_t inp_buf_index; 253 | uint8_t inp_curr; 254 | size_t inp_ctr; 255 | uint8_t inp_sess[64]; 256 | int inp_end; 257 | int inp_peek; 258 | 259 | void (*translator)(struct ud*); 260 | uint64_t insn_offset; 261 | char insn_hexcode[64]; 262 | 263 | /* 264 | * Assembly output buffer 265 | */ 266 | char *asm_buf; 267 | size_t asm_buf_size; 268 | size_t asm_buf_fill; 269 | char asm_buf_int[128]; 270 | 271 | /* 272 | * Symbol resolver for use in the translation phase. 273 | */ 274 | const char* (*sym_resolver)(struct ud*, uint64_t addr, int64_t *offset); 275 | 276 | uint8_t dis_mode; 277 | uint64_t pc; 278 | uint8_t vendor; 279 | enum ud_mnemonic_code mnemonic; 280 | struct ud_operand operand[4]; 281 | uint8_t error; 282 | uint8_t _rex; 283 | uint8_t pfx_rex; 284 | uint8_t pfx_seg; 285 | uint8_t pfx_opr; 286 | uint8_t pfx_adr; 287 | uint8_t pfx_lock; 288 | uint8_t pfx_str; 289 | uint8_t pfx_bnd; 290 | uint8_t pfx_xacquire; 291 | uint8_t pfx_xrelease; 292 | uint8_t pfx_rep; 293 | uint8_t pfx_repe; 294 | uint8_t pfx_repne; 295 | uint8_t opr_mode; 296 | uint8_t adr_mode; 297 | uint8_t br_far; 298 | uint8_t br_near; 299 | uint8_t have_modrm; 300 | uint8_t modrm; 301 | uint8_t vex_op; 302 | uint8_t vex_b1; 303 | uint8_t vex_b2; 304 | void * user_opaque_data; 305 | struct ud_itab_entry * itab_entry; 306 | struct ud_lookup_table_list_entry *le; 307 | }; 308 | 309 | /* ----------------------------------------------------------------------------- 310 | * Type-definitions 311 | * ----------------------------------------------------------------------------- 312 | */ 313 | typedef enum ud_type ud_type_t; 314 | typedef enum ud_mnemonic_code ud_mnemonic_code_t; 315 | 316 | typedef struct ud ud_t; 317 | typedef struct ud_operand ud_operand_t; 318 | 319 | #define UD_SYN_INTEL ud_translate_intel 320 | #define UD_SYN_ATT ud_translate_att 321 | #define UD_EOI (-1) 322 | #define UD_INP_CACHE_SZ 32 323 | #define UD_VENDOR_AMD 0 324 | #define UD_VENDOR_INTEL 1 325 | #define UD_VENDOR_ANY 2 326 | 327 | #endif 328 | 329 | /* 330 | vim: set ts=2 sw=2 expandtab 331 | */ 332 | -------------------------------------------------------------------------------- /libudis86/udint.h: -------------------------------------------------------------------------------- 1 | /* udis86 - libudis86/udint.h -- definitions for internal use only 2 | * 3 | * Copyright (c) 2002-2009 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #ifndef _UDINT_H_ 27 | #define _UDINT_H_ 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* HAVE_CONFIG_H */ 32 | 33 | #if defined(UD_DEBUG) && HAVE_ASSERT_H 34 | # include 35 | # define UD_ASSERT(_x) assert(_x) 36 | #else 37 | # define UD_ASSERT(_x) 38 | #endif /* !HAVE_ASSERT_H */ 39 | 40 | #if defined(UD_DEBUG) 41 | #define UDERR(u, msg) \ 42 | do { \ 43 | (u)->error = 1; \ 44 | fprintf(stderr, "decode-error: %s:%d: %s", \ 45 | __FILE__, __LINE__, (msg)); \ 46 | } while (0) 47 | #else 48 | #define UDERR(u, m) \ 49 | do { \ 50 | (u)->error = 1; \ 51 | } while (0) 52 | #endif /* !LOGERR */ 53 | 54 | #define UD_RETURN_ON_ERROR(u) \ 55 | do { \ 56 | if ((u)->error != 0) { \ 57 | return (u)->error; \ 58 | } \ 59 | } while (0) 60 | 61 | #define UD_RETURN_WITH_ERROR(u, m) \ 62 | do { \ 63 | UDERR(u, m); \ 64 | return (u)->error; \ 65 | } while (0) 66 | 67 | #ifndef __UD_STANDALONE__ 68 | # define UD_NON_STANDALONE(x) x 69 | #else 70 | # define UD_NON_STANDALONE(x) 71 | #endif 72 | 73 | /* printf formatting int64 specifier */ 74 | #ifdef FMT64 75 | # undef FMT64 76 | #endif 77 | #if defined(_MSC_VER) || defined(__BORLANDC__) 78 | # define FMT64 "I64" 79 | #else 80 | # if defined(__APPLE__) 81 | # define FMT64 "ll" 82 | # elif defined(__amd64__) || defined(__x86_64__) 83 | # define FMT64 "l" 84 | # else 85 | # define FMT64 "ll" 86 | # endif /* !x64 */ 87 | #endif 88 | 89 | /* define an inline macro */ 90 | #if defined(_MSC_VER) || defined(__BORLANDC__) 91 | # define UD_INLINE __inline /* MS Visual Studio requires __inline 92 | instead of inline for C code */ 93 | #else 94 | # define UD_INLINE inline 95 | #endif 96 | 97 | #endif /* _UDINT_H_ */ 98 | -------------------------------------------------------------------------------- /libudis86/udis86.c: -------------------------------------------------------------------------------- 1 | /* udis86 - libudis86/udis86.c 2 | * 3 | * Copyright (c) 2002-2013 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #include 27 | #include "udint.h" 28 | #include "extern.h" 29 | #include "decode.h" 30 | 31 | #if !defined(__UD_STANDALONE__) 32 | # if HAVE_STRING_H 33 | # include 34 | # endif 35 | #endif /* !__UD_STANDALONE__ */ 36 | 37 | static void ud_inp_init(struct ud *u); 38 | 39 | /* ============================================================================= 40 | * ud_init 41 | * Initializes ud_t object. 42 | * ============================================================================= 43 | */ 44 | extern void 45 | ud_init(struct ud* u) 46 | { 47 | memset((void*)u, 0, sizeof(struct ud)); 48 | ud_set_mode(u, 16); 49 | u->mnemonic = UD_Iinvalid; 50 | ud_set_pc(u, 0); 51 | #ifndef __UD_STANDALONE__ 52 | ud_set_input_file(u, stdin); 53 | #endif /* __UD_STANDALONE__ */ 54 | 55 | ud_set_asm_buffer(u, u->asm_buf_int, sizeof(u->asm_buf_int)); 56 | } 57 | 58 | 59 | /* ============================================================================= 60 | * ud_disassemble 61 | * Disassembles one instruction and returns the number of 62 | * bytes disassembled. A zero means end of disassembly. 63 | * ============================================================================= 64 | */ 65 | extern unsigned int 66 | ud_disassemble(struct ud* u) 67 | { 68 | int len; 69 | if (u->inp_end) { 70 | return 0; 71 | } 72 | if ((len = ud_decode(u)) > 0) { 73 | if (u->translator != NULL) { 74 | u->asm_buf[0] = '\0'; 75 | u->translator(u); 76 | } 77 | } 78 | return len; 79 | } 80 | 81 | 82 | /* ============================================================================= 83 | * ud_set_mode() - Set Disassemly Mode. 84 | * ============================================================================= 85 | */ 86 | extern void 87 | ud_set_mode(struct ud* u, uint8_t m) 88 | { 89 | switch(m) { 90 | case 16: 91 | case 32: 92 | case 64: u->dis_mode = m ; return; 93 | default: u->dis_mode = 16; return; 94 | } 95 | } 96 | 97 | /* ============================================================================= 98 | * ud_set_vendor() - Set vendor. 99 | * ============================================================================= 100 | */ 101 | extern void 102 | ud_set_vendor(struct ud* u, unsigned v) 103 | { 104 | switch(v) { 105 | case UD_VENDOR_INTEL: 106 | u->vendor = v; 107 | break; 108 | case UD_VENDOR_ANY: 109 | u->vendor = v; 110 | break; 111 | default: 112 | u->vendor = UD_VENDOR_AMD; 113 | } 114 | } 115 | 116 | /* ============================================================================= 117 | * ud_set_pc() - Sets code origin. 118 | * ============================================================================= 119 | */ 120 | extern void 121 | ud_set_pc(struct ud* u, uint64_t o) 122 | { 123 | u->pc = o; 124 | } 125 | 126 | /* ============================================================================= 127 | * ud_set_syntax() - Sets the output syntax. 128 | * ============================================================================= 129 | */ 130 | extern void 131 | ud_set_syntax(struct ud* u, void (*t)(struct ud*)) 132 | { 133 | u->translator = t; 134 | } 135 | 136 | /* ============================================================================= 137 | * ud_insn() - returns the disassembled instruction 138 | * ============================================================================= 139 | */ 140 | const char* 141 | ud_insn_asm(const struct ud* u) 142 | { 143 | return u->asm_buf; 144 | } 145 | 146 | /* ============================================================================= 147 | * ud_insn_offset() - Returns the offset. 148 | * ============================================================================= 149 | */ 150 | uint64_t 151 | ud_insn_off(const struct ud* u) 152 | { 153 | return u->insn_offset; 154 | } 155 | 156 | 157 | /* ============================================================================= 158 | * ud_insn_hex() - Returns hex form of disassembled instruction. 159 | * ============================================================================= 160 | */ 161 | const char* 162 | ud_insn_hex(struct ud* u) 163 | { 164 | u->insn_hexcode[0] = 0; 165 | if (!u->error) { 166 | unsigned int i; 167 | const unsigned char *src_ptr = ud_insn_ptr(u); 168 | char* src_hex; 169 | src_hex = (char*) u->insn_hexcode; 170 | /* for each byte used to decode instruction */ 171 | for (i = 0; i < ud_insn_len(u) && i < sizeof(u->insn_hexcode) / 2; 172 | ++i, ++src_ptr) { 173 | sprintf(src_hex, "%02x", *src_ptr & 0xFF); 174 | src_hex += 2; 175 | } 176 | } 177 | return u->insn_hexcode; 178 | } 179 | 180 | 181 | /* ============================================================================= 182 | * ud_insn_ptr 183 | * Returns a pointer to buffer containing the bytes that were 184 | * disassembled. 185 | * ============================================================================= 186 | */ 187 | extern const uint8_t* 188 | ud_insn_ptr(const struct ud* u) 189 | { 190 | return (u->inp_buf == NULL) ? 191 | u->inp_sess : u->inp_buf + (u->inp_buf_index - u->inp_ctr); 192 | } 193 | 194 | 195 | /* ============================================================================= 196 | * ud_insn_len 197 | * Returns the count of bytes disassembled. 198 | * ============================================================================= 199 | */ 200 | extern unsigned int 201 | ud_insn_len(const struct ud* u) 202 | { 203 | return u->inp_ctr; 204 | } 205 | 206 | 207 | /* ============================================================================= 208 | * ud_insn_get_opr 209 | * Return the operand struct representing the nth operand of 210 | * the currently disassembled instruction. Returns NULL if 211 | * there's no such operand. 212 | * ============================================================================= 213 | */ 214 | const struct ud_operand* 215 | ud_insn_opr(const struct ud *u, unsigned int n) 216 | { 217 | if (n > 3 || u->operand[n].type == UD_NONE) { 218 | return NULL; 219 | } else { 220 | return &u->operand[n]; 221 | } 222 | } 223 | 224 | 225 | /* ============================================================================= 226 | * ud_opr_is_sreg 227 | * Returns non-zero if the given operand is of a segment register type. 228 | * ============================================================================= 229 | */ 230 | int 231 | ud_opr_is_sreg(const struct ud_operand *opr) 232 | { 233 | return opr->type == UD_OP_REG && 234 | opr->base >= UD_R_ES && 235 | opr->base <= UD_R_GS; 236 | } 237 | 238 | 239 | /* ============================================================================= 240 | * ud_opr_is_sreg 241 | * Returns non-zero if the given operand is of a general purpose 242 | * register type. 243 | * ============================================================================= 244 | */ 245 | int 246 | ud_opr_is_gpr(const struct ud_operand *opr) 247 | { 248 | return opr->type == UD_OP_REG && 249 | opr->base >= UD_R_AL && 250 | opr->base <= UD_R_R15; 251 | } 252 | 253 | 254 | /* ============================================================================= 255 | * ud_set_user_opaque_data 256 | * ud_get_user_opaque_data 257 | * Get/set user opaqute data pointer 258 | * ============================================================================= 259 | */ 260 | void 261 | ud_set_user_opaque_data(struct ud * u, void* opaque) 262 | { 263 | u->user_opaque_data = opaque; 264 | } 265 | 266 | void* 267 | ud_get_user_opaque_data(const struct ud *u) 268 | { 269 | return u->user_opaque_data; 270 | } 271 | 272 | 273 | /* ============================================================================= 274 | * ud_set_asm_buffer 275 | * Allow the user to set an assembler output buffer. If `buf` is NULL, 276 | * we switch back to the internal buffer. 277 | * ============================================================================= 278 | */ 279 | void 280 | ud_set_asm_buffer(struct ud *u, char *buf, size_t size) 281 | { 282 | if (buf == NULL) { 283 | ud_set_asm_buffer(u, u->asm_buf_int, sizeof(u->asm_buf_int)); 284 | } else { 285 | u->asm_buf = buf; 286 | u->asm_buf_size = size; 287 | } 288 | } 289 | 290 | 291 | /* ============================================================================= 292 | * ud_set_sym_resolver 293 | * Set symbol resolver for relative targets used in the translation 294 | * phase. 295 | * 296 | * The resolver is a function that takes a uint64_t address and returns a 297 | * symbolic name for the that address. The function also takes a second 298 | * argument pointing to an integer that the client can optionally set to a 299 | * non-zero value for offsetted targets. (symbol+offset) The function may 300 | * also return NULL, in which case the translator only prints the target 301 | * address. 302 | * 303 | * The function pointer maybe NULL which resets symbol resolution. 304 | * ============================================================================= 305 | */ 306 | void 307 | ud_set_sym_resolver(struct ud *u, const char* (*resolver)(struct ud*, 308 | uint64_t addr, 309 | int64_t *offset)) 310 | { 311 | u->sym_resolver = resolver; 312 | } 313 | 314 | 315 | /* ============================================================================= 316 | * ud_insn_mnemonic 317 | * Return the current instruction mnemonic. 318 | * ============================================================================= 319 | */ 320 | enum ud_mnemonic_code 321 | ud_insn_mnemonic(const struct ud *u) 322 | { 323 | return u->mnemonic; 324 | } 325 | 326 | 327 | /* ============================================================================= 328 | * ud_lookup_mnemonic 329 | * Looks up mnemonic code in the mnemonic string table. 330 | * Returns NULL if the mnemonic code is invalid. 331 | * ============================================================================= 332 | */ 333 | const char* 334 | ud_lookup_mnemonic(enum ud_mnemonic_code c) 335 | { 336 | if (c < UD_MAX_MNEMONIC_CODE) { 337 | return ud_mnemonics_str[c]; 338 | } else { 339 | return NULL; 340 | } 341 | } 342 | 343 | /* ============================================================================= 344 | * ud_lookup_eflags 345 | * Looks up eflags information structure 346 | * Returns NULL if invalid. 347 | * ============================================================================= 348 | */ 349 | const struct ud_flags* ud_lookup_flags(struct ud *u) 350 | { 351 | if (u == NULL || u->itab_entry == NULL) { 352 | return NULL; 353 | } else { 354 | return &u->itab_entry->flags; 355 | } 356 | } 357 | 358 | /* 359 | * ud_inp_init 360 | * Initializes the input system. 361 | */ 362 | static void 363 | ud_inp_init(struct ud *u) 364 | { 365 | u->inp_hook = NULL; 366 | u->inp_buf = NULL; 367 | u->inp_buf_size = 0; 368 | u->inp_buf_index = 0; 369 | u->inp_curr = 0; 370 | u->inp_ctr = 0; 371 | u->inp_end = 0; 372 | u->inp_peek = UD_EOI; 373 | UD_NON_STANDALONE(u->inp_file = NULL); 374 | } 375 | 376 | 377 | /* ============================================================================= 378 | * ud_inp_set_hook 379 | * Sets input hook. 380 | * ============================================================================= 381 | */ 382 | void 383 | ud_set_input_hook(register struct ud* u, int (*hook)(struct ud*)) 384 | { 385 | ud_inp_init(u); 386 | u->inp_hook = hook; 387 | } 388 | 389 | /* ============================================================================= 390 | * ud_inp_set_buffer 391 | * Set buffer as input. 392 | * ============================================================================= 393 | */ 394 | void 395 | ud_set_input_buffer(register struct ud* u, const uint8_t* buf, size_t len) 396 | { 397 | ud_inp_init(u); 398 | u->inp_buf = buf; 399 | u->inp_buf_size = len; 400 | u->inp_buf_index = 0; 401 | } 402 | 403 | 404 | #ifndef __UD_STANDALONE__ 405 | /* ============================================================================= 406 | * ud_input_set_file 407 | * Set FILE as input. 408 | * ============================================================================= 409 | */ 410 | static int 411 | inp_file_hook(struct ud* u) 412 | { 413 | return fgetc(u->inp_file); 414 | } 415 | 416 | void 417 | ud_set_input_file(register struct ud* u, FILE* f) 418 | { 419 | ud_inp_init(u); 420 | u->inp_hook = inp_file_hook; 421 | u->inp_file = f; 422 | } 423 | #endif /* __UD_STANDALONE__ */ 424 | 425 | 426 | /* ============================================================================= 427 | * ud_input_skip 428 | * Skip n input bytes. 429 | * ============================================================================ 430 | */ 431 | void 432 | ud_input_skip(struct ud* u, size_t n) 433 | { 434 | if (u->inp_end) { 435 | return; 436 | } 437 | if (u->inp_buf == NULL) { 438 | while (n--) { 439 | int c = u->inp_hook(u); 440 | if (c == UD_EOI) { 441 | goto eoi; 442 | } 443 | } 444 | return; 445 | } else { 446 | if (n > u->inp_buf_size || 447 | u->inp_buf_index > u->inp_buf_size - n) { 448 | u->inp_buf_index = u->inp_buf_size; 449 | goto eoi; 450 | } 451 | u->inp_buf_index += n; 452 | return; 453 | } 454 | eoi: 455 | u->inp_end = 1; 456 | UDERR(u, "cannot skip, eoi received\b"); 457 | return; 458 | } 459 | 460 | 461 | /* ============================================================================= 462 | * ud_input_end 463 | * Returns non-zero on end-of-input. 464 | * ============================================================================= 465 | */ 466 | int 467 | ud_input_end(const struct ud *u) 468 | { 469 | return u->inp_end; 470 | } 471 | 472 | /* vim:set ts=2 sw=2 expandtab */ 473 | -------------------------------------------------------------------------------- /m4/ax_compare_version.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_compare_version.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_COMPARE_VERSION(VERSION_A, OP, VERSION_B, [ACTION-IF-TRUE], [ACTION-IF-FALSE]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # This macro compares two version strings. Due to the various number of 12 | # minor-version numbers that can exist, and the fact that string 13 | # comparisons are not compatible with numeric comparisons, this is not 14 | # necessarily trivial to do in a autoconf script. This macro makes doing 15 | # these comparisons easy. 16 | # 17 | # The six basic comparisons are available, as well as checking equality 18 | # limited to a certain number of minor-version levels. 19 | # 20 | # The operator OP determines what type of comparison to do, and can be one 21 | # of: 22 | # 23 | # eq - equal (test A == B) 24 | # ne - not equal (test A != B) 25 | # le - less than or equal (test A <= B) 26 | # ge - greater than or equal (test A >= B) 27 | # lt - less than (test A < B) 28 | # gt - greater than (test A > B) 29 | # 30 | # Additionally, the eq and ne operator can have a number after it to limit 31 | # the test to that number of minor versions. 32 | # 33 | # eq0 - equal up to the length of the shorter version 34 | # ne0 - not equal up to the length of the shorter version 35 | # eqN - equal up to N sub-version levels 36 | # neN - not equal up to N sub-version levels 37 | # 38 | # When the condition is true, shell commands ACTION-IF-TRUE are run, 39 | # otherwise shell commands ACTION-IF-FALSE are run. The environment 40 | # variable 'ax_compare_version' is always set to either 'true' or 'false' 41 | # as well. 42 | # 43 | # Examples: 44 | # 45 | # AX_COMPARE_VERSION([3.15.7],[lt],[3.15.8]) 46 | # AX_COMPARE_VERSION([3.15],[lt],[3.15.8]) 47 | # 48 | # would both be true. 49 | # 50 | # AX_COMPARE_VERSION([3.15.7],[eq],[3.15.8]) 51 | # AX_COMPARE_VERSION([3.15],[gt],[3.15.8]) 52 | # 53 | # would both be false. 54 | # 55 | # AX_COMPARE_VERSION([3.15.7],[eq2],[3.15.8]) 56 | # 57 | # would be true because it is only comparing two minor versions. 58 | # 59 | # AX_COMPARE_VERSION([3.15.7],[eq0],[3.15]) 60 | # 61 | # would be true because it is only comparing the lesser number of minor 62 | # versions of the two values. 63 | # 64 | # Note: The characters that separate the version numbers do not matter. An 65 | # empty string is the same as version 0. OP is evaluated by autoconf, not 66 | # configure, so must be a string, not a variable. 67 | # 68 | # The author would like to acknowledge Guido Draheim whose advice about 69 | # the m4_case and m4_ifvaln functions make this macro only include the 70 | # portions necessary to perform the specific comparison specified by the 71 | # OP argument in the final configure script. 72 | # 73 | # LICENSE 74 | # 75 | # Copyright (c) 2008 Tim Toolan 76 | # 77 | # Copying and distribution of this file, with or without modification, are 78 | # permitted in any medium without royalty provided the copyright notice 79 | # and this notice are preserved. This file is offered as-is, without any 80 | # warranty. 81 | 82 | #serial 11 83 | 84 | dnl ######################################################################### 85 | AC_DEFUN([AX_COMPARE_VERSION], [ 86 | AC_REQUIRE([AC_PROG_AWK]) 87 | 88 | # Used to indicate true or false condition 89 | ax_compare_version=false 90 | 91 | # Convert the two version strings to be compared into a format that 92 | # allows a simple string comparison. The end result is that a version 93 | # string of the form 1.12.5-r617 will be converted to the form 94 | # 0001001200050617. In other words, each number is zero padded to four 95 | # digits, and non digits are removed. 96 | AS_VAR_PUSHDEF([A],[ax_compare_version_A]) 97 | A=`echo "$1" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \ 98 | -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \ 99 | -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \ 100 | -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \ 101 | -e 's/[[^0-9]]//g'` 102 | 103 | AS_VAR_PUSHDEF([B],[ax_compare_version_B]) 104 | B=`echo "$3" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \ 105 | -e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \ 106 | -e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \ 107 | -e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \ 108 | -e 's/[[^0-9]]//g'` 109 | 110 | dnl # In the case of le, ge, lt, and gt, the strings are sorted as necessary 111 | dnl # then the first line is used to determine if the condition is true. 112 | dnl # The sed right after the echo is to remove any indented white space. 113 | m4_case(m4_tolower($2), 114 | [lt],[ 115 | ax_compare_version=`echo "x$A 116 | x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/false/;s/x${B}/true/;1q"` 117 | ], 118 | [gt],[ 119 | ax_compare_version=`echo "x$A 120 | x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"` 121 | ], 122 | [le],[ 123 | ax_compare_version=`echo "x$A 124 | x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"` 125 | ], 126 | [ge],[ 127 | ax_compare_version=`echo "x$A 128 | x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/true/;s/x${B}/false/;1q"` 129 | ],[ 130 | dnl Split the operator from the subversion count if present. 131 | m4_bmatch(m4_substr($2,2), 132 | [0],[ 133 | # A count of zero means use the length of the shorter version. 134 | # Determine the number of characters in A and B. 135 | ax_compare_version_len_A=`echo "$A" | $AWK '{print(length)}'` 136 | ax_compare_version_len_B=`echo "$B" | $AWK '{print(length)}'` 137 | 138 | # Set A to no more than B's length and B to no more than A's length. 139 | A=`echo "$A" | sed "s/\(.\{$ax_compare_version_len_B\}\).*/\1/"` 140 | B=`echo "$B" | sed "s/\(.\{$ax_compare_version_len_A\}\).*/\1/"` 141 | ], 142 | [[0-9]+],[ 143 | # A count greater than zero means use only that many subversions 144 | A=`echo "$A" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"` 145 | B=`echo "$B" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"` 146 | ], 147 | [.+],[ 148 | AC_WARNING( 149 | [illegal OP numeric parameter: $2]) 150 | ],[]) 151 | 152 | # Pad zeros at end of numbers to make same length. 153 | ax_compare_version_tmp_A="$A`echo $B | sed 's/./0/g'`" 154 | B="$B`echo $A | sed 's/./0/g'`" 155 | A="$ax_compare_version_tmp_A" 156 | 157 | # Check for equality or inequality as necessary. 158 | m4_case(m4_tolower(m4_substr($2,0,2)), 159 | [eq],[ 160 | test "x$A" = "x$B" && ax_compare_version=true 161 | ], 162 | [ne],[ 163 | test "x$A" != "x$B" && ax_compare_version=true 164 | ],[ 165 | AC_WARNING([illegal OP parameter: $2]) 166 | ]) 167 | ]) 168 | 169 | AS_VAR_POPDEF([A])dnl 170 | AS_VAR_POPDEF([B])dnl 171 | 172 | dnl # Execute ACTION-IF-TRUE / ACTION-IF-FALSE. 173 | if test "$ax_compare_version" = "true" ; then 174 | m4_ifvaln([$4],[$4],[:])dnl 175 | m4_ifvaln([$5],[else $5])dnl 176 | fi 177 | ]) dnl AX_COMPARE_VERSION 178 | -------------------------------------------------------------------------------- /m4/ax_prog_sphinx_version.m4: -------------------------------------------------------------------------------- 1 | # SYNOPSIS 2 | # 3 | # AX_PROG_SPHINX_VERSION([VERSION],[ACTION-IF-TRUE],[ACTION-IF-FALSE]) 4 | # 5 | # DESCRIPTION 6 | # 7 | # Makes sure that sphinx-build supports the version indicated. If true 8 | # the shell commands in ACTION-IF-TRUE are executed. If not the shell 9 | # commands in ACTION-IF-FALSE are run. Note if $SPHINX_BUILD is not set 10 | # (for example by running AX_WITH_PROG) the macro will fail. 11 | # 12 | # Example: 13 | # 14 | # AX_WITH_PROG(SPHINX_BUILD,sphinx-build) 15 | # AX_PROG_SPHINX([1.1.1],[ ... ],[ ... ]) 16 | # 17 | # LICENSE 18 | # 19 | # ax_prog_python_version.m4 20 | # 21 | # Copyright (c) 2009 Francesco Salvestrini 22 | # 23 | # Copying and distribution of this file, with or without modification, are 24 | # permitted in any medium without royalty provided the copyright notice 25 | # and this notice are preserved. This file is offered as-is, without any 26 | # warranty. 27 | # 28 | # ax_prog_sphinx_version.m4 29 | # 30 | # Copyright (c) 2013 Vivek Thampi 31 | 32 | 33 | AC_DEFUN([AX_PROG_SPHINX],[ 34 | AC_REQUIRE([AC_PROG_SED]) 35 | AC_REQUIRE([AC_PROG_GREP]) 36 | 37 | 38 | AS_IF([test -n "$SPHINX_BUILD"],[ 39 | ax_sphinx_version="$1" 40 | 41 | AC_MSG_CHECKING([for sphinx version]) 42 | changequote(<<,>>) 43 | sphinx_version=`$SPHINX_BUILD -h 2>&1 | $GREP "^Sphinx v" | $SED -e 's/^.* v\([0-9]*\.[0-9]*\.[0-9]*\)/\1/'` 44 | changequote([,]) 45 | AC_MSG_RESULT($sphinx_version) 46 | 47 | AC_SUBST([SPHINX_VERSION],[$sphinx_version]) 48 | 49 | AX_COMPARE_VERSION([$ax_sphinx_version],[le],[$sphinx_version],[ 50 | : 51 | $2 52 | ],[ 53 | : 54 | $3 55 | ]) 56 | ],[ 57 | AC_MSG_WARN([could not find the sphinx documentation tool]) 58 | $3 59 | ]) 60 | ]) 61 | -------------------------------------------------------------------------------- /m4/ax_prog_yasm_version.m4: -------------------------------------------------------------------------------- 1 | # SYNOPSIS 2 | # 3 | # AX_PROG_YASM_VERSION([VERSION],[ACTION-IF-TRUE],[ACTION-IF-FALSE]) 4 | # 5 | # DESCRIPTION 6 | # 7 | # Makes sure that yasm supports the version indicated. If true 8 | # the shell commands in ACTION-IF-TRUE are executed. If not the shell 9 | # commands in ACTION-IF-FALSE are run. Note if $YASM is not set 10 | # (for example by running AX_WITH_PROG) the macro will fail. 11 | # 12 | # Example: 13 | # 14 | # AX_WITH_PROG(YASM,yasm) 15 | # AX_PROG_YASM_VERSION([1.1.1],[ ... ],[ ... ]) 16 | # 17 | # LICENSE 18 | # 19 | # ax_prog_python_version.m4 20 | # 21 | # Copyright (c) 2009 Francesco Salvestrini 22 | # 23 | # Copying and distribution of this file, with or without modification, are 24 | # permitted in any medium without royalty provided the copyright notice 25 | # and this notice are preserved. This file is offered as-is, without any 26 | # warranty. 27 | # 28 | # ax_prog_yasm_version.m4 29 | # 30 | # Copyright (c) 2013 Vivek Thampi 31 | 32 | 33 | AC_DEFUN([AX_PROG_YASM_VERSION],[ 34 | AC_REQUIRE([AC_PROG_SED]) 35 | AC_REQUIRE([AC_PROG_GREP]) 36 | 37 | 38 | AS_IF([test -n "$YASM"],[ 39 | ax_yasm_version="$1" 40 | 41 | AC_MSG_CHECKING([for yasm version]) 42 | changequote(<<,>>) 43 | yasm_version=`$YASM --version 2>&1 | $GREP "^yasm " | $SED -e 's/^.* \([0-9]*\.[0-9]*\.[0-9]*\)/\1/'` 44 | changequote([,]) 45 | AC_MSG_RESULT($yasm_version) 46 | 47 | AC_SUBST([YASM_VERSION],[$yasm_version]) 48 | 49 | AX_COMPARE_VERSION([$ax_yasm_version],[le],[$yasm_version],[ 50 | : 51 | $2 52 | ],[ 53 | : 54 | $3 55 | ]) 56 | ],[ 57 | AC_MSG_WARN([could not find the yasm]) 58 | $3 59 | ]) 60 | ]) 61 | -------------------------------------------------------------------------------- /m4/ax_with_prog.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.nongnu.org/autoconf-archive/ax_with_prog.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_WITH_PROG([VARIABLE],[program],[VALUE-IF-NOT-FOUND],[PATH]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Locates an installed program binary, placing the result in the precious 12 | # variable VARIABLE. Accepts a present VARIABLE, then --with-program, and 13 | # failing that searches for program in the given path (which defaults to 14 | # the system path). If program is found, VARIABLE is set to the full path 15 | # of the binary; if it is not found VARIABLE is set to VALUE-IF-NOT-FOUND 16 | # if provided, unchanged otherwise. 17 | # 18 | # A typical example could be the following one: 19 | # 20 | # AX_WITH_PROG(PERL,perl) 21 | # 22 | # NOTE: This macro is based upon the original AX_WITH_PYTHON macro from 23 | # Dustin J. Mitchell . 24 | # 25 | # LICENSE 26 | # 27 | # Copyright (c) 2008 Francesco Salvestrini 28 | # Copyright (c) 2008 Dustin J. Mitchell 29 | # 30 | # Copying and distribution of this file, with or without modification, are 31 | # permitted in any medium without royalty provided the copyright notice 32 | # and this notice are preserved. 33 | 34 | AC_DEFUN([AX_WITH_PROG],[ 35 | AC_PREREQ([2.61]) 36 | 37 | pushdef([VARIABLE],$1) 38 | pushdef([EXECUTABLE],$2) 39 | pushdef([VALUE_IF_NOT_FOUND],$3) 40 | pushdef([PATH_PROG],$4) 41 | 42 | AC_ARG_VAR(VARIABLE,Absolute path to EXECUTABLE executable) 43 | 44 | AS_IF(test -z "$VARIABLE",[ 45 | AC_MSG_CHECKING(whether EXECUTABLE executable path has been provided) 46 | AC_ARG_WITH(EXECUTABLE,AS_HELP_STRING([--with-EXECUTABLE=[[[[PATH]]]]],absolute path to EXECUTABLE executable), [ 47 | AS_IF([test "$withval" != yes -a "$withval" != no],[ 48 | VARIABLE="$withval" 49 | AC_MSG_RESULT($VARIABLE) 50 | ],[ 51 | VARIABLE="" 52 | AC_MSG_RESULT([no]) 53 | AS_IF([test "$withval" != no], [ 54 | AC_PATH_PROG([]VARIABLE[],[]EXECUTABLE[],[]VALUE_IF_NOT_FOUND[],[]PATH_PROG[]) 55 | ]) 56 | ]) 57 | ],[ 58 | AC_MSG_RESULT([no]) 59 | AC_PATH_PROG([]VARIABLE[],[]EXECUTABLE[],[]VALUE_IF_NOT_FOUND[],[]PATH_PROG[]) 60 | ]) 61 | ]) 62 | 63 | popdef([PATH_PROG]) 64 | popdef([VALUE_IF_NOT_FOUND]) 65 | popdef([EXECUTABLE]) 66 | popdef([VARIABLE]) 67 | ]) 68 | -------------------------------------------------------------------------------- /m4/ax_with_python.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.nongnu.org/autoconf-archive/ax_with_python.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_WITH_PYTHON([VALUE-IF-NOT-FOUND],[PATH]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Locates an installed Python binary, placing the result in the precious 12 | # variable $PYTHON. Accepts a present $PYTHON, then --with-python, and 13 | # failing that searches for python in the given path (which defaults to 14 | # the system path). If python is found, $PYTHON is set to the full path of 15 | # the binary; if it is not found $PYTHON is set to VALUE-IF-NOT-FOUND if 16 | # provided, unchanged otherwise. 17 | # 18 | # A typical use could be the following one: 19 | # 20 | # AX_WITH_PYTHON 21 | # 22 | # LICENSE 23 | # 24 | # Copyright (c) 2008 Francesco Salvestrini 25 | # 26 | # Copying and distribution of this file, with or without modification, are 27 | # permitted in any medium without royalty provided the copyright notice 28 | # and this notice are preserved. 29 | 30 | AC_DEFUN([AX_WITH_PYTHON],[ 31 | AX_WITH_PROG(PYTHON,python,$1,$2) 32 | ]) 33 | -------------------------------------------------------------------------------- /scripts/Makefile.am: -------------------------------------------------------------------------------- 1 | EXTRA_DIST = \ 2 | ud_opcode.py \ 3 | ud_itab.py 4 | 5 | MAINTAINERCLEANFILES = Makefile.in 6 | 7 | clean-local: 8 | -rm -f *.pyc 9 | -rm -f ud_asmtest* 10 | -------------------------------------------------------------------------------- /scripts/asmtest.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | objdump="otool -tV" 4 | yasm=yasm 5 | asmfile="ud_yasmtest.asm" 6 | binfile="ud_yasmtest.bin" 7 | Sfile="ud_yasmtest.S" 8 | objfile="ud_yasmtest.o" 9 | 10 | echo "[bits $1]" > $asmfile 11 | echo $2 >> $asmfile 12 | 13 | $yasm -f bin -o $binfile $asmfile 14 | 15 | if [ ! $? -eq 0 ]; then 16 | echo "error: failed to assemble" 17 | exit 1 18 | fi 19 | 20 | echo "-- hexdump --------------------------------------" 21 | hexdump $binfile 22 | echo 23 | 24 | echo "-- objdump --------------------------------------" 25 | hexdump -e '1/1 ".byte 0x%02x\n"' $binfile > $Sfile 26 | gcc -c $Sfile -o $objfile 27 | $objdump -d $objfile 28 | echo 29 | 30 | echo "-- udcli (intel) ---------------------------------" 31 | ../udcli/udcli -$1 $binfile 32 | echo 33 | 34 | echo "-- udcli (at&t) ----------------------------------" 35 | ../udcli/udcli -$1 -att $binfile 36 | echo 37 | 38 | exit 0 39 | -------------------------------------------------------------------------------- /tests/Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | # Automake definitions for udis86 tests 3 | # 4 | check_PROGRAMS = \ 5 | symresolve \ 6 | libcheck 7 | 8 | symresolve_SOURCES = symresolve.c 9 | symresolve_LDADD = $(top_builddir)/libudis86/libudis86.la 10 | symresolve_CFLAGS = -I$(top_srcdir)/libudis86 -I$(top_srcdir) 11 | 12 | libcheck_SOURCES = libcheck.c 13 | libcheck_LDADD = $(top_builddir)/libudis86/libudis86.la 14 | libcheck_CFLAGS = -I$(top_srcdir)/libudis86 -I$(top_srcdir) 15 | 16 | MAINTAINERCLEANFILES = Makefile.in 17 | 18 | DISTCLEANFILES = difftest.sh 19 | 20 | YASM = @YASM@ 21 | PYTHON = @PYTHON@ 22 | TESTDIS = $(top_builddir)/udcli/udcli 23 | 24 | EXTRA_DIST = \ 25 | $(srcdir)/asm \ 26 | $(srcdir)/oprgen.py \ 27 | $(srcdir)/symresolve.ref \ 28 | $(srcdir)/installcheck.c \ 29 | $(srcdir)/libcheck.c 30 | 31 | if HAVE_YASM 32 | tests: difftest test-sym-resolver test-libcheck 33 | else 34 | tests: warn_no_yasm test-sym-resolver test-libcheck 35 | endif 36 | 37 | SEED = 1984 38 | 39 | 40 | .PHONY: test-sym-resolver 41 | test-sym-resolver: symresolve 42 | @$(top_builddir)/tests/$< > $@.out 43 | @diff -w $(srcdir)/symresolve.ref $@.out && echo "$@: passed." 44 | 45 | .PHONY: test-libcheck 46 | test-libcheck: libcheck 47 | @./libcheck && echo "$@: passed" 48 | 49 | # 50 | # generate operand tests 51 | # 52 | oprtest_generate = \ 53 | outdir=$(builddir)/_results/asm/$(1) && \ 54 | mkdir -p $${outdir} && \ 55 | PYTHONPATH=$(top_srcdir)/scripts $(PYTHON) $(top_srcdir)/tests/oprgen.py \ 56 | $(top_srcdir)/docs/x86/optable.xml $(SEED) $(1) >$${outdir}/oprtest.asm 57 | 58 | .PHONY: oprtest 59 | oprtest: oprgen.py 60 | @echo "Generating operand tests." 61 | @$(call oprtest_generate,64) 62 | @$(call oprtest_generate,32) 63 | @$(call oprtest_generate,16) 64 | 65 | 66 | .PHONY: difftest 67 | difftest: oprtest $(builddir)/difftest.sh 68 | @bash $(builddir)/difftest.sh 69 | 70 | 71 | .PHONY: difftest-refup 72 | difftest-refup: $(builddir)/difftest.sh 73 | @bash $(builddir)/difftest.sh refup 74 | 75 | 76 | .PHONY: warn_no_yasm 77 | warn_no_yasm: 78 | @echo "*** YASM NOT FOUND: Poor Test Coverage ***" 79 | 80 | check-local: tests 81 | 82 | installcheck-local: 83 | @$(CC) $(srcdir)/installcheck.c -o installcheck.bin -I$(includedir) -L$(libdir) -ludis86 84 | @echo "$@: passed" 85 | 86 | clean-local: 87 | rm -f $(builddir)/*.bin $(builddir)/*.out $(builddir)/*.pyc 88 | rm -rf $(builddir)/_results 89 | -------------------------------------------------------------------------------- /tests/asm/16/disp.asm: -------------------------------------------------------------------------------- 1 | [bits 16] 2 | mov ax, [eax-0x10] 3 | add bx, [esi+0x10] 4 | add ax, [0xffff] 5 | add ax, [esi+edi*4-0x10] 6 | add ax, [bx+si-0x4877] 7 | -------------------------------------------------------------------------------- /tests/asm/16/test16.asm: -------------------------------------------------------------------------------- 1 | [bits 16] 2 | movzx eax, word [bx] 3 | iretd 4 | dpps xmm2, xmm1, 0x10 5 | blendvpd xmm1, xmm2 6 | -------------------------------------------------------------------------------- /tests/asm/32/att.asm: -------------------------------------------------------------------------------- 1 | [bits 32] 2 | mov eax, 0x1234 3 | mov eax, [0x1234] 4 | -------------------------------------------------------------------------------- /tests/asm/32/att.asm.Sref: -------------------------------------------------------------------------------- 1 | 0000000000000000 b834120000 mov $0x1234, %eax 2 | 0000000000000005 a134120000 mov 0x1234, %eax 3 | -------------------------------------------------------------------------------- /tests/asm/32/att.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 b834120000 mov eax, 0x1234 2 | 0000000000000005 a134120000 mov eax, [0x1234] 3 | -------------------------------------------------------------------------------- /tests/asm/32/avx.asm: -------------------------------------------------------------------------------- 1 | [bits 32] 2 | vaddsd xmm1, xmm2, xmm4 3 | vaddsd xmm2, xmm3, [eax] 4 | vaddps ymm1, ymm2, ymm3 5 | vaddps ymm1, ymm7, [eax] 6 | vblendpd ymm1, ymm7, ymm4, 0x42 7 | vcvtpd2ps xmm1, xmm2 8 | vcvtpd2ps xmm1, ymm3 9 | vcvtpd2ps xmm1, oword [eax] 10 | vcvtpd2ps xmm1, yword [eax] 11 | vcvtpd2dq xmm1, xmm2 12 | vcvtpd2dq xmm1, ymm3 13 | vcvtpd2dq xmm1, oword [eax] 14 | vcvtpd2dq xmm1, yword [eax] 15 | vcvttpd2dq xmm1, xmm2 16 | vcvttpd2dq xmm1, ymm3 17 | vcvttpd2dq xmm1, oword [eax] 18 | vcvttpd2dq xmm1, yword [eax] 19 | -------------------------------------------------------------------------------- /tests/asm/32/avx.asm.Sref: -------------------------------------------------------------------------------- 1 | 0000000000000000 c5eb58cc vaddsd %xmm4, %xmm2, %xmm1 2 | 0000000000000004 c5e35810 vaddsd (%eax), %xmm3, %xmm2 3 | 0000000000000008 c5ec58cb vaddps %ymm3, %ymm2, %ymm1 4 | 000000000000000c c5c45808 vaddps (%eax), %ymm7, %ymm1 5 | 0000000000000010 c4e3450dcc42 vblendpd $0x42, %ymm4, %ymm7, %ymm1 6 | 0000000000000016 c5f95aca vcvtpd2ps %xmm2, %xmm1 7 | 000000000000001a c5fd5acb vcvtpd2ps %ymm3, %xmm1 8 | 000000000000001e c5f95a08 vcvtpd2ps (%eax), %xmm1 9 | 0000000000000022 c5fd5a08 vcvtpd2ps (%eax), %xmm1 10 | 0000000000000026 c5fbe6ca vcvtpd2dq %xmm2, %xmm1 11 | 000000000000002a c5ffe6cb vcvtpd2dq %ymm3, %xmm1 12 | 000000000000002e c5fbe608 vcvtpd2dq (%eax), %xmm1 13 | 0000000000000032 c5ffe608 vcvtpd2dq (%eax), %xmm1 14 | 0000000000000036 c5f9e6ca vcvttpd2dq %xmm2, %xmm1 15 | 000000000000003a c5fde6cb vcvttpd2dq %ymm3, %xmm1 16 | 000000000000003e c5f9e608 vcvttpd2dq (%eax), %xmm1 17 | 0000000000000042 c5fde608 vcvttpd2dq (%eax), %xmm1 18 | -------------------------------------------------------------------------------- /tests/asm/32/avx.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 c5eb58cc vaddsd xmm1, xmm2, xmm4 2 | 0000000000000004 c5e35810 vaddsd xmm2, xmm3, [eax] 3 | 0000000000000008 c5ec58cb vaddps ymm1, ymm2, ymm3 4 | 000000000000000c c5c45808 vaddps ymm1, ymm7, [eax] 5 | 0000000000000010 c4e3450dcc42 vblendpd ymm1, ymm7, ymm4, 0x42 6 | 0000000000000016 c5f95aca vcvtpd2ps xmm1, xmm2 7 | 000000000000001a c5fd5acb vcvtpd2ps xmm1, ymm3 8 | 000000000000001e c5f95a08 vcvtpd2ps xmm1, [eax] 9 | 0000000000000022 c5fd5a08 vcvtpd2ps xmm1, yword [eax] 10 | 0000000000000026 c5fbe6ca vcvtpd2dq xmm1, xmm2 11 | 000000000000002a c5ffe6cb vcvtpd2dq xmm1, ymm3 12 | 000000000000002e c5fbe608 vcvtpd2dq xmm1, [eax] 13 | 0000000000000032 c5ffe608 vcvtpd2dq xmm1, yword [eax] 14 | 0000000000000036 c5f9e6ca vcvttpd2dq xmm1, xmm2 15 | 000000000000003a c5fde6cb vcvttpd2dq xmm1, ymm3 16 | 000000000000003e c5f9e608 vcvttpd2dq xmm1, [eax] 17 | 0000000000000042 c5fde608 vcvttpd2dq xmm1, yword [eax] 18 | -------------------------------------------------------------------------------- /tests/asm/32/corner.asm: -------------------------------------------------------------------------------- 1 | [bits 32] 2 | 3 | lar eax, [bx+si] 4 | nop 5 | pause 6 | -------------------------------------------------------------------------------- /tests/asm/32/corner.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 670f0200 lar eax, word [bx+si] 2 | 0000000000000004 90 nop 3 | 0000000000000005 f390 pause 4 | -------------------------------------------------------------------------------- /tests/asm/32/disp.asm: -------------------------------------------------------------------------------- 1 | [bits 32] 2 | mov eax, [eax-0x10] 3 | add eax, [esi+0x10] 4 | add eax, [0x10] 5 | add eax, [esi+edi*4+0x10] 6 | add eax, [bx+si-0x4877] 7 | -------------------------------------------------------------------------------- /tests/asm/32/disp.asm.Sref: -------------------------------------------------------------------------------- 1 | 0000000000000000 8b40f0 mov -0x10(%eax), %eax 2 | 0000000000000003 034610 add 0x10(%esi), %eax 3 | 0000000000000006 030510000000 add 0x10, %eax 4 | 000000000000000c 0344be10 add 0x10(%esi,%edi,4), %eax 5 | 0000000000000010 67038089b7 add -0x4877(%bx,%si), %eax 6 | -------------------------------------------------------------------------------- /tests/asm/32/invalid_seg.asm: -------------------------------------------------------------------------------- 1 | [bits 32] 2 | db 0x8C, 0x38 3 | -------------------------------------------------------------------------------- /tests/asm/32/invalid_seg.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 8c38 invalid 2 | -------------------------------------------------------------------------------- /tests/asm/32/obscure.asm: -------------------------------------------------------------------------------- 1 | 2 | db 0xd1, 0xf6 ; shl Ev, 0x1 3 | db 0xd0, 0xf6 ; shl Eb, 0x1 4 | db 0xd9, 0xd9 ; fstp1 st1 5 | db 0xdc, 0xd0 ; fcom2 6 | db 0xdc, 0xd8 ; fcomp3 7 | db 0xdd, 0xc8 ; fxch4 8 | db 0xde, 0xd1 ; fcomp5 9 | db 0xdf, 0xc3 ; fxch7 10 | db 0xdf, 0xd0 ; fstp8 11 | db 0xdf, 0xd8 ; fstp9 12 | db 0x83, 0xe2, 0xdf ; and edx, 0xffffffdf (sign-extension) 13 | -------------------------------------------------------------------------------- /tests/asm/32/obscure.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 d1f6 shl esi, 1 2 | 0000000000000002 d0f6 shl dh, 1 3 | 0000000000000004 d9d9 fstp1 st1 4 | 0000000000000006 dcd0 fcom2 st0 5 | 0000000000000008 dcd8 fcomp3 st0 6 | 000000000000000a ddc8 fxch4 st0 7 | 000000000000000c ded1 fcomp5 st1 8 | 000000000000000e dfc3 ffreep st3 9 | 0000000000000010 dfd0 fstp8 st0 10 | 0000000000000012 dfd8 fstp9 st0 11 | 0000000000000014 83e2df and edx, 0xffffffdf 12 | -------------------------------------------------------------------------------- /tests/asm/32/reljmp.asm: -------------------------------------------------------------------------------- 1 | [bits 32] 2 | [org 0x80000000] 3 | 4 | l1: 5 | nop 6 | nop 7 | nop 8 | nop 9 | nop 10 | 11 | jmp l1 12 | nop 13 | jmp word l2 14 | 15 | nop 16 | nop 17 | jmp dword l2 18 | nop 19 | nop 20 | nop 21 | l2: 22 | nop 23 | nop 24 | jmp l1 25 | -------------------------------------------------------------------------------- /tests/asm/32/reljmp.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000080000000 90 nop 2 | 0000000080000001 90 nop 3 | 0000000080000002 90 nop 4 | 0000000080000003 90 nop 5 | 0000000080000004 90 nop 6 | 0000000080000005 ebf9 jmp 0x80000000 7 | 0000000080000007 90 nop 8 | 0000000080000008 66e90a00 jmp 0x16 9 | 000000008000000c 90 nop 10 | 000000008000000d 90 nop 11 | 000000008000000e e903000000 jmp 0x80000016 12 | 0000000080000013 90 nop 13 | 0000000080000014 90 nop 14 | 0000000080000015 90 nop 15 | 0000000080000016 90 nop 16 | 0000000080000017 90 nop 17 | 0000000080000018 ebe6 jmp 0x80000000 18 | -------------------------------------------------------------------------------- /tests/asm/32/sext.asm: -------------------------------------------------------------------------------- 1 | [bits 32] 2 | 3 | ;; test sign extension 4 | 5 | adc ax, -100 6 | and edx, -3 7 | or dx, -1000 8 | or dx, -1 9 | add edx, -1000 10 | imul dx, bx, -100 11 | imul edx, ebx, -1 12 | imul edx, ebx, -128 13 | imul edx, ebx, -129 14 | imul ax, bx, -129 15 | sub dword [eax], -1 16 | sub word [eax], -2000 17 | test eax, 1 18 | test eax, -1 19 | push byte -1 20 | push word -1 21 | push dword -1000 22 | push word -1000 23 | 24 | -------------------------------------------------------------------------------- /tests/asm/32/sext.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 6683d09c adc ax, 0xff9c 2 | 0000000000000004 83e2fd and edx, 0xfffffffd 3 | 0000000000000007 6681ca18fc or dx, 0xfc18 4 | 000000000000000c 6683caff or dx, 0xffff 5 | 0000000000000010 81c218fcffff add edx, 0xfffffc18 6 | 0000000000000016 666bd39c imul dx, bx, 0xff9c 7 | 000000000000001a 6bd3ff imul edx, ebx, 0xffffffff 8 | 000000000000001d 6bd380 imul edx, ebx, 0xffffff80 9 | 0000000000000020 69d37fffffff imul edx, ebx, 0xffffff7f 10 | 0000000000000026 6669c37fff imul ax, bx, 0xff7f 11 | 000000000000002b 8328ff sub dword [eax], 0xffffffff 12 | 000000000000002e 66812830f8 sub word [eax], 0xf830 13 | 0000000000000033 a901000000 test eax, 0x1 14 | 0000000000000038 a9ffffffff test eax, 0xffffffff 15 | 000000000000003d 6aff push 0xffffffff 16 | 000000000000003f 666aff push 0xffff 17 | 0000000000000042 6818fcffff push 0xfffffc18 18 | 0000000000000047 666818fc push 0xfc18 19 | -------------------------------------------------------------------------------- /tests/asm/32/test32.asm: -------------------------------------------------------------------------------- 1 | [bits 32] 2 | mov [0x1000], bx 3 | mov [0x10], ax 4 | mov ax, [0x10] 5 | mov byte [bx+si], 0x10 6 | mov byte [bx+si+0x10], 0x10 7 | mov word [bp+0x10], 0x10 8 | mov word [bp+di+0x10], 0x10 9 | mov dword [si+0x10], 0x10 10 | mov word [di+0x10], 0x10 11 | mov dword [bx+0x10], 0x1000 12 | mov word [bx+0x1000], 0x1000 13 | mov dword [ebx+ecx*4], 0x100 14 | mov [eax+eax*2], eax 15 | mov [edx+esi*8], ebp 16 | mov dword [ecx*4+0x490], 0x100 17 | mov byte [cs:0x100000], 0x10 18 | mov word [eax+0x10], 0x10 19 | mov [eax+0x10], ax 20 | mov [eax+0x1000], ebx 21 | mov [ebp+eax+0x20], esi 22 | mov [ebp+edi+0x100000], esp 23 | mov byte [esp], 0x10 24 | add al, 0x10 25 | add eax, ebx 26 | push es 27 | pop es 28 | adc eax, 0x10000 29 | and [eax], al 30 | daa 31 | inc ax 32 | inc edx 33 | push eax 34 | push ax 35 | pushad 36 | bound eax, [eax] 37 | bound ax, [ecx] 38 | bsr ax, ax 39 | bsf eax, [bx+si] 40 | bswap eax 41 | bt [eax], ax 42 | btr ax, 0x10 43 | btc ebx, 0x10 44 | bts word [ebx], 0x10 45 | call ax 46 | call word [bx+si] 47 | call eax 48 | call dword [eax+ecx] 49 | call word 0x10:0x100 50 | call dword 0x10:0x10000 51 | call far word [eax] 52 | call far dword [bp+si] 53 | cbw 54 | cwd 55 | clc 56 | cld 57 | clflush [eax] 58 | cmp eax, ebx 59 | cmp ecx, [bx] 60 | cmpsb 61 | cmpsw 62 | cmpsd 63 | cmpxchg [eax], ax 64 | cmpxchg8b [ebx] 65 | cpuid 66 | das 67 | inc eax 68 | inc word [ecx] 69 | dec byte [si] 70 | in al, 0x10 71 | in ax, 0x10 72 | in eax, 0x10 73 | insb 74 | insw 75 | insd 76 | int 0x10 77 | into 78 | lahf 79 | lds ax, [eax] 80 | les eax, [ebx] 81 | lea ax, [eax] 82 | lea eax, [bx+si] 83 | leave 84 | lodsb 85 | lodsw 86 | lodsd 87 | test al, bl 88 | test [eax], bl 89 | test [eax], ebx 90 | test [eax], bx 91 | ret 92 | ret 0x10 93 | aad 0x10 94 | aam 0x10 95 | salc 96 | hlt 97 | cmc 98 | lock xchg ebx, eax 99 | repne mov eax, ebx 100 | repe mov eax, 0x10 101 | push cs 102 | outsd 103 | outsw 104 | mov ax, es 105 | mov bx, ds 106 | mov [eax], es 107 | mov [ebx+ecx], cs 108 | mov cs, [ebx+ecx] 109 | wait 110 | pushfw 111 | pushfd 112 | lodsw 113 | lodsd 114 | retf 0x10 115 | int3 116 | into 117 | iretw 118 | iretd 119 | lsl ax, [bx] 120 | syscall 121 | clts 122 | sysret 123 | movups xmm0, xmm1 124 | mov dr0, eax 125 | ror word [ebx], cl 126 | wrmsr 127 | rdmsr 128 | rdtsc 129 | rdtscp 130 | rdpmc 131 | sysenter 132 | sysexit 133 | cmovo eax, [eax] 134 | cmovno eax, [bx] 135 | cmovb eax, [eax] 136 | cmovae eax, [bx] 137 | cmovo eax, [eax] 138 | cmovz eax, ebx 139 | cmovnz eax, [eax] 140 | cmovbe eax, [bx] 141 | cmova eax, [bx] 142 | movmskps eax, xmm0 143 | movmskpd eax, xmm0 144 | sqrtps xmm1, xmm0 145 | rsqrtps xmm1, xmm0 146 | rcpps xmm1, xmm0 147 | andps xmm1, xmm0 148 | orps xmm1, xmm0 149 | xorps xmm1, xmm0 150 | andnps xmm1, xmm0 151 | sqrtss xmm1, xmm0 152 | rsqrtss xmm1, xmm0 153 | rcpss xmm1, xmm0 154 | sqrtpd xmm1, xmm0 155 | andpd xmm1, xmm0 156 | andnpd xmm1, xmm0 157 | orpd xmm1, xmm0 158 | xorpd xmm1, xmm0 159 | sqrtsd xmm1, xmm0 160 | punpcklbw mm0, [eax] 161 | punpcklwd mm0, mm1 162 | punpckldq mm0, mm1 163 | packsswb mm0, mm1 164 | packsswb mm0, mm1 165 | pcmpgtb mm0, mm1 166 | pcmpgtw mm0, mm1 167 | pcmpgtd mm0, mm1 168 | packuswb mm0, mm1 169 | punpcklbw xmm0, [eax] 170 | punpcklwd xmm0, xmm1 171 | punpckldq xmm0, xmm1 172 | packsswb xmm0, xmm1 173 | packsswb xmm0, xmm1 174 | pcmpgtb xmm0, xmm1 175 | pcmpgtw xmm0, xmm1 176 | pcmpgtd xmm0, xmm1 177 | packuswb xmm0, xmm1 178 | pshufw mm0, mm1, 0x10 179 | pcmpeqb mm0, mm1 180 | pcmpeqw mm0, mm1 181 | pcmpeqd mm0, mm1 182 | pcmpeqb xmm0, xmm1 183 | pcmpeqw xmm0, xmm1 184 | pcmpeqd xmm0, xmm1 185 | emms 186 | pshufhw xmm0, xmm1, 0x10 187 | pshufd xmm0, xmm1, 0x10 188 | pshuflw xmm0, xmm1, 0x10 189 | seto byte [eax] 190 | setno byte [bx] 191 | setz byte [es:eax+ecx*2+0x100] 192 | push fs 193 | pop fs 194 | cpuid 195 | bt [eax], eax 196 | shld eax, ebx, 0x10 197 | shld [eax], bx, cl 198 | cmpxchg [eax], eax 199 | lss eax, [eax] 200 | btr [eax], eax 201 | movnti [eax], eax 202 | psrlw mm0, 0x10 203 | fadd dword [eax] 204 | imul eax, [eax], 0xf6 205 | movd dword [eax], xmm0 206 | movzx eax, word [eax] 207 | push word [0x10] 208 | insw 209 | insd 210 | fnstsw ax 211 | fucomip st0, st1 212 | fcomip st0, st7 213 | fucomp st4 214 | fucom st5 215 | fstp st3 216 | fst st1 217 | ffree st0 218 | fdiv st7, st0 219 | fdivr st2, st0 220 | fsub st4, st0 221 | fsubr st6, st0 222 | fmul st0, st0 223 | fadd st5, st0 224 | ficom word [eax] 225 | fidivr word [eax] 226 | fimul word [ebx] 227 | fisub word [ecx] 228 | fld qword [bx+si] 229 | fisttp qword [edx+0x100] 230 | fnstsw word [eax] 231 | frstor [ebx] 232 | prefetch [bx+si] 233 | psrlq xmm0, 0x10 234 | psrldq xmm0, 0x10 235 | movsldup xmm0, [eax] 236 | add [0xffffffff], eax 237 | cvtsi2ss xmm1, dword [eax] 238 | pop dword [eax] 239 | out 0x0, al 240 | lldt word [0x100] 241 | lgdt [0x221] 242 | sldt word [0x233] 243 | sgdt [0x443] 244 | lidt [eax+0x333] 245 | lldt ax 246 | ltr bx 247 | verr cx 248 | verw dx 249 | sldt ax 250 | str bx 251 | str eax 252 | and esp, 0xfc 253 | psrlw xmm1, 0x10 254 | psraw xmm7, 0x1 255 | psllw xmm2, 0x23 256 | fldenv [0x10] 257 | fldenv [0x123] 258 | fldcw word [0x100] 259 | fnstcw word [0x10] 260 | ficom word [eax+ebx+0x10] 261 | fstp tword [0x10] 262 | fadd qword [eax+ebx*2+0x1] 263 | frstor [0x100] 264 | fnstsw word [0x100] 265 | fiadd word [0x100] 266 | fild word [0x10] 267 | monitor 268 | mwait 269 | lfence 270 | mfence 271 | sfence 272 | vmrun 273 | vmmcall 274 | vmload 275 | vmsave 276 | stgi 277 | clgi 278 | skinit 279 | invlpga 280 | blendpd xmm1, xmm6, 0x8 281 | psignw xmm4, [eax] 282 | blendpd xmm1, [eax], 0x9 283 | -------------------------------------------------------------------------------- /tests/asm/64/amd/invalid.asm: -------------------------------------------------------------------------------- 1 | ;; Test amd specific 64bit instructions 2 | 3 | [bits 64] 4 | 5 | ;; Invalid instructions in amd 64bit mode 6 | db 0x0f, 0x34 ; sysenter (invalid) 7 | db 0x0f, 0x35 ; sysexit (invalid) 8 | -------------------------------------------------------------------------------- /tests/asm/64/amd/invalid.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 0f34 invalid 2 | 0000000000000002 0f35 invalid 3 | -------------------------------------------------------------------------------- /tests/asm/64/avx.asm: -------------------------------------------------------------------------------- 1 | [bits 64] 2 | vaddsd xmm12, xmm4, xmm1 3 | vminsd xmm13, xmm15, qword [rbx+r8-0x10] 4 | vaddps ymm8, ymm3, ymm14 5 | vaddps ymm8, ymm3, [rax] 6 | -------------------------------------------------------------------------------- /tests/asm/64/avx.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 c55b58e1 vaddsd xmm12, xmm4, xmm1 2 | 0000000000000004 c421035d6c03f0 vminsd xmm13, xmm15, qword [rbx+r8-0x10] 3 | 000000000000000b c4416458c6 vaddps ymm8, ymm3, ymm14 4 | 0000000000000010 c5645800 vaddps ymm8, ymm3, [rax] 5 | -------------------------------------------------------------------------------- /tests/asm/64/branch.asm: -------------------------------------------------------------------------------- 1 | ;; Test branching instructions 2 | ;; 3 | [bits 64] 4 | 5 | jnz near x 6 | jo near x 7 | jno word x 8 | jc near x 9 | jnc word x 10 | jae dword x 11 | jcxz x 12 | jecxz x 13 | jrcxz x 14 | jmp dword near x 15 | call dword near x 16 | jmp word x 17 | jmp dword x 18 | jmp word [eax] 19 | x: jmp qword [rax] 20 | jmp word x 21 | jmp dword x 22 | -------------------------------------------------------------------------------- /tests/asm/64/branch.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 0f853b000000 jnz 0x41 2 | 0000000000000006 0f8035000000 jo 0x41 3 | 000000000000000c 660f813000 jno 0x41 4 | 0000000000000011 0f822a000000 jb 0x41 5 | 0000000000000017 660f832500 jae 0x41 6 | 000000000000001c 0f831f000000 jae 0x41 7 | 0000000000000022 67e31c jecxz 0x41 8 | 0000000000000025 67e319 jecxz 0x41 9 | 0000000000000028 e317 jrcxz 0x41 10 | 000000000000002a e912000000 jmp 0x41 11 | 000000000000002f e80d000000 call 0x41 12 | 0000000000000034 66e90900 jmp 0x41 13 | 0000000000000038 e904000000 jmp 0x41 14 | 000000000000003d 6766ff20 jmp word [eax] 15 | 0000000000000041 ff20 jmp qword [rax] 16 | 0000000000000043 66e9faff jmp 0x41 17 | 0000000000000047 e9f5ffffff jmp 0x41 18 | -------------------------------------------------------------------------------- /tests/asm/64/disp.asm: -------------------------------------------------------------------------------- 1 | [bits 64] 2 | mov ax, [eax-0x10] 3 | add bx, [esi+0x10] 4 | add rax, [0xffff] 5 | add ax, [esi+edi*4-0x10] 6 | add r8, [rax+rbx*4-0x80000000] 7 | mov rax, [qword 0x800000000000] 8 | -------------------------------------------------------------------------------- /tests/asm/64/disp.asm.Sref: -------------------------------------------------------------------------------- 1 | 0000000000000000 67668b40f0 mov -0x10(%eax), %ax 2 | 0000000000000005 6766035e10 add 0x10(%esi), %bx 3 | 000000000000000a 48030425ffff0000 add 0xffff, %rax 4 | 0000000000000012 67660344bef0 add -0x10(%esi,%edi,4), %ax 5 | 0000000000000018 4c03849800000080 add -0x80000000(%rax,%rbx,4), %r8 6 | 0000000000000020 48a1000000000080 mov 0x800000000000, %rax 7 | -0000 8 | -------------------------------------------------------------------------------- /tests/asm/64/disp.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 67668b40f0 mov ax, [eax-0x10] 2 | 0000000000000005 6766035e10 add bx, [esi+0x10] 3 | 000000000000000a 48030425ffff0000 add rax, [0xffff] 4 | 0000000000000012 67660344bef0 add ax, [esi+edi*4-0x10] 5 | 0000000000000018 4c03849800000080 add r8, [rax+rbx*4-0x80000000] 6 | 0000000000000020 48a1000000000080 mov rax, [0x800000000000] 7 | -0000 8 | -------------------------------------------------------------------------------- /tests/asm/64/intel/invalid.asm: -------------------------------------------------------------------------------- 1 | ;; Test intel specific instructions in 64bit mode 2 | 3 | [bits 64] 4 | 5 | ;; yasm doesn't seem to support a mode for intel 6 | ;; specific instructions 7 | db 0x0f, 0x34 ; sysenter 8 | db 0x0f, 0x35 ; sysexit 9 | -------------------------------------------------------------------------------- /tests/asm/64/intel/invalid.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 0f34 sysenter 2 | 0000000000000002 0f35 sysexit 3 | -------------------------------------------------------------------------------- /tests/asm/64/reljmp.asm: -------------------------------------------------------------------------------- 1 | [bits 64] 2 | [org 0x8000000000000000] 3 | 4 | l1: 5 | nop 6 | nop 7 | nop 8 | nop 9 | nop 10 | 11 | jmp l1 12 | nop 13 | jmp word l2 14 | 15 | nop 16 | nop 17 | jmp dword l2 18 | nop 19 | nop 20 | nop 21 | l2: 22 | nop 23 | nop 24 | jmp l1 25 | -------------------------------------------------------------------------------- /tests/asm/64/reljmp.asm.ref: -------------------------------------------------------------------------------- 1 | 8000000000000000 90 nop 2 | 8000000000000001 90 nop 3 | 8000000000000002 90 nop 4 | 8000000000000003 90 nop 5 | 8000000000000004 90 nop 6 | 8000000000000005 ebf9 jmp 0x8000000000000000 7 | 8000000000000007 90 nop 8 | 8000000000000008 66e90a00 jmp 0x16 9 | 800000000000000c 90 nop 10 | 800000000000000d 90 nop 11 | 800000000000000e e903000000 jmp 0x8000000000000016 12 | 8000000000000013 90 nop 13 | 8000000000000014 90 nop 14 | 8000000000000015 90 nop 15 | 8000000000000016 90 nop 16 | 8000000000000017 90 nop 17 | 8000000000000018 ebe6 jmp 0x8000000000000000 18 | -------------------------------------------------------------------------------- /tests/asm/64/sext.asm: -------------------------------------------------------------------------------- 1 | [bits 64] 2 | 3 | ;; test sign extension 4 | 5 | adc al, -100 6 | adc ax, -100 7 | adc eax, -100 8 | adc rax, -100 9 | imul dx, bx, -100 10 | imul edx, ebx, -100 11 | imul rdx, r11, -100 12 | push byte -1 13 | push word -1 14 | push dword -1000 15 | push word -1000 16 | push -1 17 | push byte -1 18 | push dword -1 19 | push word -1 20 | -------------------------------------------------------------------------------- /tests/asm/64/sext.asm.ref: -------------------------------------------------------------------------------- 1 | 0000000000000000 149c adc al, 0x9c 2 | 0000000000000002 6683d09c adc ax, 0xff9c 3 | 0000000000000006 83d09c adc eax, 0xffffff9c 4 | 0000000000000009 4883d09c adc rax, 0xffffffffffffff9c 5 | 000000000000000d 666bd39c imul dx, bx, 0xff9c 6 | 0000000000000011 6bd39c imul edx, ebx, 0xffffff9c 7 | 0000000000000014 496bd39c imul rdx, r11, 0xffffffffffffff9c 8 | 0000000000000018 6aff push 0xffffffffffffffff 9 | 000000000000001a 666aff push 0xffff 10 | 000000000000001d 6818fcffff push 0xfffffffffffffc18 11 | 0000000000000022 666818fc push 0xfc18 12 | 0000000000000026 6aff push 0xffffffffffffffff 13 | 0000000000000028 6aff push 0xffffffffffffffff 14 | 000000000000002a 6aff push 0xffffffffffffffff 15 | 000000000000002c 666aff push 0xffff 16 | -------------------------------------------------------------------------------- /tests/asm/64/test64.asm: -------------------------------------------------------------------------------- 1 | [bits 64] 2 | mov rax, 0x102030405060708 3 | mov [eax+0x10], ax 4 | mov [eax+0x1000], ebx 5 | mov [ebp+eax+0x40], esi 6 | mov qword [ebp+edi+0x1000], 0x10 7 | mov byte [esp], 0x10 8 | mov [eax], rax 9 | mov [r12], eax 10 | mov [r13+r12+0x200], eax 11 | mov [r8*4+0x670], sil 12 | inc rax 13 | dec rax 14 | mov [rip+0x200], rax 15 | mov rax, 0x10000 16 | push rax 17 | push r12 18 | call qword [r8] 19 | call qword [rax] 20 | call far word [r8] 21 | call far dword [rax] 22 | cbw 23 | cwde 24 | cdqe 25 | cwd 26 | cdq 27 | cqo 28 | cmovl rax, [rax] 29 | cmovge eax, [rax] 30 | clflush [r14] 31 | cmp [rax], rax 32 | cmp rbx, r12 33 | cmp r12d, r8d 34 | cmpsb 35 | cmpsw 36 | cmpsd 37 | cmpsq 38 | cmpxchg [eax], r14 39 | cmpxchg8b [ebx] 40 | inc r12d 41 | inc dword [rax] 42 | dec r11w 43 | hlt 44 | imul rax, [eax+ebx*8+0x100000], 0x10 45 | idiv dword [r12] 46 | enter 0x100, 0x0 47 | enter 0x100, 0x1 48 | enter 0x100, 0x10 49 | in al, 0x10 50 | in ax, 0x10 51 | in eax, 0x10 52 | lfs eax, [eax] 53 | lgs eax, [ebx] 54 | lea eax, [rbx] 55 | lea r11, [eax] 56 | lodsb 57 | lodsq 58 | lodsd 59 | push rax 60 | push r11 61 | xchg [eax], sil 62 | xchg [eax], ebx 63 | xchg [eax], bx 64 | xchg r8, rax 65 | xchg r9, rax 66 | xchg cx, ax 67 | movsd 68 | movsq 69 | mov al, r11b 70 | mov sil, al 71 | mov r11b, dil 72 | ret 0x10 73 | pop rax 74 | pop r11 75 | pop qword [eax] 76 | insd 77 | outsd 78 | mov [r14d], cs 79 | mov cs, [ebx+ecx] 80 | pushfq 81 | scasq 82 | lsl ax, [rbx] 83 | movups xmm0, [r12d] 84 | movupd xmm0, [r12d] 85 | movsldup xmm0, [r12d] 86 | movups xmm0, xmm1 87 | movups xmm10, xmm12 88 | movups xmm0, xmm12 89 | movlps xmm0, [rax] 90 | movlps [rax], xmm0 91 | unpcklps xmm4, xmm5 92 | unpckhps xmm4, xmm5 93 | movhps xmm3, [eax] 94 | movhps [rax], xmm3 95 | movss xmm0, dword [eax] 96 | movss [eax], xmm0 97 | movlpd [eax], xmm0 98 | movlpd xmm0, [eax] 99 | unpcklpd xmm2, xmm4 100 | unpckhpd xmm3, [eax+ebx*8-0x1243] 101 | movhpd xmm3, [rax] 102 | movhpd [rax], xmm2 103 | movsd xmm3, xmm4 104 | movddup xmm3, [rax] 105 | mov dr0, rax 106 | mov rax, dr1 107 | movnti [eax], eax 108 | movnti [rax], rax 109 | movd dword [eax], xmm0 110 | movd dword [eax], xmm0 111 | movq xmm11, [eax] 112 | vmmcall 113 | vmrun 114 | clgi 115 | stgi 116 | mfence 117 | lfence 118 | sfence 119 | clflush [rax] 120 | mov cr8, rax 121 | push qword [eax] 122 | push word [eax] 123 | add bh, bh 124 | add dil, dil 125 | add sil, bpl 126 | add al, sil 127 | add rax, r12 128 | add eax, r12d 129 | prefetcht0 [eax] 130 | prefetchnta [eax] 131 | prefetch [eax] 132 | add [r8], r9b 133 | mov [rax-0x1], eax 134 | inc rax 135 | jmp qword [eax] 136 | jmp rax 137 | mov [0x10], rax 138 | mov rax, [0x10] 139 | mov rax, 0x102030405060708 140 | xchg r8, rax 141 | push ax 142 | push rax 143 | push r11 144 | mov rax, [0x100] 145 | pmovmskb r12d, xmm14 146 | movdq2q mm0, xmm13 147 | psrlw xmm10, 0x10 148 | psraw xmm7, 0x1 149 | psllw xmm12, 0x23 150 | swapgs 151 | fadd dword [rax+rbx] 152 | shl rsi, 1 153 | cvtsi2ss xmm1, qword [eax] 154 | cvtss2si rax, dword [eax] 155 | cvttsd2si rax, [eax] 156 | call 0x64 157 | mpsadbw xmm7, xmm6, 0x7 158 | popfq 159 | -------------------------------------------------------------------------------- /tests/difftest.sh.in: -------------------------------------------------------------------------------- 1 | # udis86 - tests/difftest.sh.in 2 | # 3 | # Copyright (c) 2013 Vivek Thampi 4 | # All rights reserved. 5 | # 6 | # Redistribution and use in source and binary forms, with or without modification, 7 | # are permitted provided that the following conditions are met: 8 | # 9 | # * Redistributions of source code must retain the above copyright notice, 10 | # this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above copyright notice, 12 | # this list of conditions and the following disclaimer in the documentation 13 | # and/or other materials provided with the distribution. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | 26 | 27 | udcli=@top_builddir@/udcli/udcli 28 | srcdir=@srcdir@ 29 | builddir=@builddir@ 30 | yasm=@YASM@ 31 | 32 | 33 | function asm_org # (srcasm) 34 | { 35 | cat $1 | sed -n 's/\[org \(.*\)\]/\1/p' 36 | } 37 | 38 | 39 | function vendor_opt # (vendor) 40 | { 41 | if [ ! -z "$1" -a "$1" != "any" ]; then 42 | echo "-v $1" 43 | fi 44 | } 45 | 46 | 47 | function org_opt # (org) 48 | { 49 | if [ ! -z "$1" ]; then 50 | echo "-o $1" 51 | fi 52 | } 53 | 54 | 55 | function update_ref # (srcasm, outasm, mode, vendor) 56 | { 57 | local srcasm=$1 58 | local outasm=$2 59 | local mode=$3 60 | local vendor=$4 61 | local org=`org_opt $4` 62 | local vendor=`vendor_opt $5` 63 | 64 | $yasm -f bin ${srcasm} -o ${outasm}.bin && 65 | if [ -f "${srcasm}.ref" ]; then 66 | echo "REFUP ${outasm}.out -> ${srcasm}.ref" 67 | $udcli $vendor $org -${mode} ${outasm}.bin > ${outasm}.out && 68 | cp ${outasm}.out ${srcasm}.ref 69 | fi && 70 | if [ -f "${srcasm}.Sref" ]; then 71 | echo "REFUP ${outasm}.out -> ${srcasm}.Sref" 72 | $udcli $vendor $org -att -${mode} ${outasm}.bin > ${outasm}.out && 73 | cp ${outasm}.out ${srcasm}.Sref 74 | fi 75 | } 76 | 77 | 78 | 79 | function diff_test # (srcasm, outasm, mode, org, vendor) 80 | { 81 | local srcasm=$1 82 | local outasm=$2 83 | local mode=$3 84 | local vendor=$4 85 | local org=`org_opt $4` 86 | local vendor=`vendor_opt $5` 87 | 88 | $yasm -f bin ${srcasm} -o ${outasm}.bin && 89 | if [ ! -f "${srcasm}.ref" ]; then 90 | echo "[bits ${mode}]" > ${outasm}.out && 91 | $udcli $vendor $org -${mode} -noff -nohex ${outasm}.bin >> ${outasm}.out && 92 | diff -w ${srcasm} ${outasm}.out 2>&1 > ${outasm}.diff.log 93 | else 94 | $udcli ${vendor} $org -${mode} ${outasm}.bin > ${outasm}.out && 95 | diff -w ${srcasm}.ref ${outasm}.out 2>&1 > ${outasm}.diff.log 96 | fi && 97 | if [ -f "${srcasm}.Sref" ]; then 98 | $udcli ${vendor} $org -att -${mode} ${outasm}.bin > ${outasm}.out && 99 | diff -w ${srcasm}.Sref ${outasm}.out 2>&1 > ${outasm}.diff.log 100 | fi && 101 | echo "DIFFTEST ${srcasm}: PASS" || 102 | ( echo "DIFFTEST ${srcasm}: ***FAIL*** (${outasm}.diff.log)" && return 1 ) 103 | } 104 | 105 | 106 | function for_each_asm # (do_op) 107 | { 108 | local do_op=$1 109 | local rc=0 110 | for mode in 16 32 64; do 111 | for vendor in amd intel any; do 112 | if [ "${vendor}" == "any" ]; then 113 | asmdir=${srcdir}/asm/${mode} 114 | outdir=${builddir}/_results/asm/${mode} 115 | else 116 | asmdir=${srcdir}/asm/${mode}/${vendor} 117 | outdir=${builddir}/_results/asm/${mode}/${vendor} 118 | fi 119 | 120 | if [ ! -d "${asmdir}" ]; then 121 | continue 122 | fi 123 | 124 | mkdir -p ${outdir} || ( echo "failed to create output dir" && exit 1 ) 125 | 126 | for a in `find ${asmdir} -maxdepth 1 -name "*.asm"` \ 127 | `find ${outdir} -maxdepth 1 -name "*.asm"`; do 128 | srcasm=$a 129 | outasm=${outdir}/`basename $a` 130 | org=`asm_org $srcasm` 131 | $do_op $srcasm $outasm $mode "$org" "$vendor" || 132 | rc=$? 133 | done 134 | done 135 | done 136 | return $rc 137 | } 138 | 139 | 140 | if [ "$1" == "refup" ]; then 141 | for_each_asm update_ref && exit 0 || exit 1 142 | else 143 | for_each_asm diff_test && exit 0 || exit 1 144 | fi 145 | -------------------------------------------------------------------------------- /tests/installcheck.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | ud_t ud_obj; 7 | 8 | ud_init(&ud_obj); 9 | ud_set_input_file(&ud_obj, stdin); 10 | ud_set_mode(&ud_obj, 64); 11 | ud_set_syntax(&ud_obj, UD_SYN_INTEL); 12 | 13 | while (ud_disassemble(&ud_obj)) { 14 | printf("\t%s\n", ud_insn_asm(&ud_obj)); 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /tests/libcheck.c: -------------------------------------------------------------------------------- 1 | /* udis86 - tests/libcheck.c 2 | * 3 | * Copyright (c) 2013 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #include 27 | #include 28 | #include 29 | 30 | unsigned int testcase_check_count; 31 | unsigned int testcase_check_fails; 32 | 33 | #define TEST_DECL(name) \ 34 | const char * __testcase_name = name \ 35 | 36 | #define TEST_CASE() \ 37 | do { \ 38 | volatile int __c = ++ testcase_check_count; \ 39 | if (0) __c += 1; \ 40 | do 41 | 42 | #define TEST_CASE_SET_FAIL() \ 43 | do { \ 44 | testcase_check_fails++; \ 45 | printf("Testcase %s: failure at line %d\n", __testcase_name, __LINE__); \ 46 | } while (0) 47 | 48 | #define TEST_CASE_REPORT_ACTUAL(v) \ 49 | printf("Testcase %s: actual = %d\n", __testcase_name, (v)) 50 | #define TEST_CASE_REPORT_EXPECTED(v) \ 51 | printf("Testcase %s: expected = %d\n", __testcase_name, (v)) 52 | 53 | #define TEST_CASE_END() \ 54 | while (0); \ 55 | } while (0) 56 | 57 | #define TEST_CHECK(cond) \ 58 | TEST_CASE() { \ 59 | int eval = (cond); \ 60 | if (!eval) { \ 61 | TEST_CASE_SET_FAIL(); \ 62 | } \ 63 | } TEST_CASE_END() 64 | 65 | #define TEST_CHECK_INT(expr, val) \ 66 | TEST_CASE() { \ 67 | int eval = (expr); \ 68 | int val2 = (val); \ 69 | if (eval != val2) { \ 70 | TEST_CASE_SET_FAIL(); \ 71 | TEST_CASE_REPORT_EXPECTED(val2); \ 72 | TEST_CASE_REPORT_ACTUAL(eval); \ 73 | } \ 74 | } TEST_CASE_END() 75 | 76 | #define TEST_CHECK_OP_REG(o, n, r) \ 77 | TEST_CHECK(ud_insn_opr(o, n)->type == UD_OP_REG && \ 78 | ud_insn_opr(o, n)->base == (r)) 79 | 80 | 81 | static int 82 | input_callback(ud_t *u) 83 | { 84 | int *n = (int *) ud_get_user_opaque_data(u); 85 | if (*n == 0) { 86 | return UD_EOI; 87 | } 88 | --*n; 89 | return 0x90; 90 | } 91 | 92 | static void 93 | check_input(ud_t *ud_obj) 94 | { 95 | TEST_DECL("check_input"); 96 | const uint8_t code[] = { 0x89, 0xc8 }; /* mov eax, ecx */ 97 | int i; 98 | 99 | /* truncate buffer */ 100 | ud_set_mode(ud_obj, 32); 101 | for (i = 0; i < 5; ++i) { 102 | ud_set_input_buffer(ud_obj, code, (sizeof code) - 1); 103 | TEST_CHECK(ud_disassemble(ud_obj) == 1); 104 | TEST_CHECK(ud_insn_len(ud_obj) == 1); 105 | TEST_CHECK(ud_obj->mnemonic == UD_Iinvalid); 106 | } 107 | 108 | /* input skip on buffer */ 109 | { 110 | const uint8_t code[] = { 0x89, 0xc8, /* mov eax, ecx*/ 111 | 0x90 }; /* nop */ 112 | ud_set_input_buffer(ud_obj, code, (sizeof code)); 113 | ud_input_skip(ud_obj, 2); 114 | TEST_CHECK_INT(ud_disassemble(ud_obj), 1); 115 | TEST_CHECK_INT(ud_obj->mnemonic, UD_Inop); 116 | 117 | ud_set_input_buffer(ud_obj, code, (sizeof code)); 118 | ud_input_skip(ud_obj, 0); 119 | TEST_CHECK_INT(ud_disassemble(ud_obj), 2); 120 | TEST_CHECK_INT(ud_obj->mnemonic, UD_Imov); 121 | TEST_CHECK(ud_insn_ptr(ud_obj)[0] == 0x89); 122 | TEST_CHECK(ud_insn_ptr(ud_obj)[1] == 0xc8); 123 | 124 | /* bad skip */ 125 | ud_set_input_buffer(ud_obj, code, (sizeof code)); 126 | ud_input_skip(ud_obj, 3); 127 | TEST_CHECK_INT(ud_disassemble(ud_obj), 0); 128 | ud_input_skip(ud_obj, 1); 129 | TEST_CHECK_INT(ud_disassemble(ud_obj), 0); 130 | ud_set_input_buffer(ud_obj, code, (sizeof code)); 131 | ud_input_skip(ud_obj, 0); 132 | TEST_CHECK_INT(ud_disassemble(ud_obj), 2); 133 | ud_input_skip(ud_obj, 1000); 134 | TEST_CHECK_INT(ud_disassemble(ud_obj), 0); 135 | } 136 | 137 | /* input hook test */ 138 | { 139 | int n; 140 | ud_set_user_opaque_data(ud_obj, (void *) &n); 141 | ud_set_input_hook(ud_obj, &input_callback); 142 | 143 | n = 0; 144 | TEST_CHECK(ud_disassemble(ud_obj) == 0); 145 | 146 | n = 1; 147 | ud_set_input_hook(ud_obj, &input_callback); 148 | TEST_CHECK_INT(ud_disassemble(ud_obj), 1); 149 | TEST_CHECK(ud_insn_ptr(ud_obj)[0] == 0x90); 150 | TEST_CHECK_INT(ud_obj->mnemonic, UD_Inop); 151 | 152 | n = 2; 153 | ud_set_input_hook(ud_obj, &input_callback); 154 | ud_input_skip(ud_obj, 1); 155 | TEST_CHECK(ud_disassemble(ud_obj) == 1); 156 | TEST_CHECK(ud_obj->mnemonic == UD_Inop); 157 | TEST_CHECK(ud_disassemble(ud_obj) == 0); 158 | TEST_CHECK(ud_insn_len(ud_obj) == 0); 159 | TEST_CHECK(ud_obj->mnemonic == UD_Iinvalid); 160 | 161 | n = 1; 162 | ud_input_skip(ud_obj, 2); 163 | TEST_CHECK_INT(ud_disassemble(ud_obj), 0); 164 | TEST_CHECK(ud_input_end(ud_obj)); 165 | } 166 | 167 | /* a known buffer overrun test case (used to be bufoverrun.c) */ 168 | { 169 | const uint8_t code[] = { 0xf0, 0x66, 0x36, 0x67, 0x65, 0x66, 170 | 0xf3, 0x67, 0xda }; 171 | ud_set_mode(ud_obj, 16); 172 | ud_set_input_buffer(ud_obj, code, sizeof code); 173 | TEST_CHECK(ud_disassemble(ud_obj) > 0); 174 | } 175 | } 176 | 177 | static void 178 | check_mode(ud_t *ud_obj) 179 | { 180 | TEST_DECL("check_mode"); 181 | const uint8_t code[] = { 0x89, 0xc8 }; /* mov eax, ecx */ 182 | ud_set_input_buffer(ud_obj, code, sizeof code); 183 | ud_set_mode(ud_obj, 32); 184 | TEST_CHECK(ud_disassemble(ud_obj) == 2); 185 | TEST_CHECK_OP_REG(ud_obj, 0, UD_R_EAX); 186 | TEST_CHECK_OP_REG(ud_obj, 1, UD_R_ECX); 187 | } 188 | 189 | static void 190 | check_disasm(ud_t *ud_obj) 191 | { 192 | TEST_DECL("check_mode"); 193 | const uint8_t code[] = { 0x89, 0xc8, /* mov eax, ecx */ 194 | 0x90 }; /* nop */ 195 | ud_set_input_buffer(ud_obj, code, sizeof code); 196 | ud_set_mode(ud_obj, 32); 197 | ud_set_pc(ud_obj, 0x100); 198 | 199 | TEST_CHECK(ud_disassemble(ud_obj) == 2); 200 | TEST_CHECK(ud_insn_off(ud_obj) == 0x100); 201 | TEST_CHECK(ud_insn_ptr(ud_obj)[0] == 0x89); 202 | TEST_CHECK(ud_insn_ptr(ud_obj)[1] == 0xc8); 203 | TEST_CHECK(ud_insn_mnemonic(ud_obj) == UD_Imov); 204 | TEST_CHECK(strcmp(ud_lookup_mnemonic(UD_Imov), "mov") == 0); 205 | 206 | TEST_CHECK(ud_disassemble(ud_obj) == 1); 207 | TEST_CHECK(ud_insn_off(ud_obj) == 0x102); 208 | TEST_CHECK(ud_insn_ptr(ud_obj)[0] == 0x90); 209 | TEST_CHECK(ud_insn_mnemonic(ud_obj) == UD_Inop); 210 | TEST_CHECK(strcmp(ud_lookup_mnemonic(UD_Inop), "nop") == 0); 211 | } 212 | 213 | int 214 | main(void) 215 | { 216 | ud_t ud_obj; 217 | ud_init(&ud_obj); 218 | ud_set_syntax(&ud_obj, UD_SYN_INTEL); 219 | 220 | check_input(&ud_obj); 221 | check_mode(&ud_obj); 222 | check_disasm(&ud_obj); 223 | 224 | if (testcase_check_fails > 0) { 225 | printf("libcheck result: %d checks, %d failures\n", 226 | testcase_check_count, testcase_check_fails); 227 | return 1; 228 | } 229 | return 0; 230 | } 231 | 232 | /* vim: set ts=2 sw=2 expandtab: */ 233 | -------------------------------------------------------------------------------- /tests/symresolve.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #if defined(__amd64__) || defined(__x86_64__) 5 | # define FMT "l" 6 | #else 7 | # define FMT "ll" 8 | #endif 9 | 10 | uint8_t bin[] = { 11 | 0x01, 0xc0, 0xeb, 0x11, 0x01, 0xc0, 0xe8, 0x14, 12 | 0x00, 0x00, 0x00, 0x01, 0xc0, 0x01, 0xc0, 0x74, 13 | 0x02, 0x01, 0xc0, 0x90, 0x90, 0xeb, 0xfe, 0x90, 14 | 0x90, 0xeb, 0xf8, 0x90, 0x90, 0x74, 0xf6, 0x90, 15 | 0x90, 0xe8, 0xf4, 0xff, 0xff, 0xff 16 | }; 17 | 18 | static const char* 19 | resolve(struct ud *u, uint64_t addr, int64_t *offset) 20 | { 21 | *offset = addr - 0x15; 22 | return "target"; 23 | } 24 | 25 | static void 26 | dis_loop(struct ud *ud_obj) 27 | { 28 | while (ud_disassemble(ud_obj) != 0) { 29 | printf("%016llx %-16s %s\n", ud_insn_off(ud_obj), 30 | ud_insn_hex(ud_obj), 31 | ud_insn_asm(ud_obj)); 32 | } 33 | } 34 | 35 | int 36 | main() { 37 | ud_t ud_obj; 38 | ud_init(&ud_obj); 39 | ud_set_mode(&ud_obj, 32); 40 | ud_set_input_buffer(&ud_obj, bin, sizeof(bin)); 41 | ud_set_syntax(&ud_obj, UD_SYN_INTEL); 42 | 43 | printf("==> Without Symbol Resolution\n"); 44 | dis_loop(&ud_obj); 45 | 46 | printf("==> With Symbol Resolution\n"); 47 | ud_set_pc(&ud_obj, 0); 48 | ud_set_input_buffer(&ud_obj, bin, sizeof(bin)); 49 | ud_set_sym_resolver(&ud_obj, &resolve); 50 | dis_loop(&ud_obj); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /tests/symresolve.ref: -------------------------------------------------------------------------------- 1 | ==> Without Symbol Resolution 2 | 0000000000000000 01c0 add eax, eax 3 | 0000000000000002 eb11 jmp 0x15 4 | 0000000000000004 01c0 add eax, eax 5 | 0000000000000006 e814000000 call 0x1f 6 | 000000000000000b 01c0 add eax, eax 7 | 000000000000000d 01c0 add eax, eax 8 | 000000000000000f 7402 jz 0x13 9 | 0000000000000011 01c0 add eax, eax 10 | 0000000000000013 90 nop 11 | 0000000000000014 90 nop 12 | 0000000000000015 ebfe jmp 0x15 13 | 0000000000000017 90 nop 14 | 0000000000000018 90 nop 15 | 0000000000000019 ebf8 jmp 0x13 16 | 000000000000001b 90 nop 17 | 000000000000001c 90 nop 18 | 000000000000001d 74f6 jz 0x15 19 | 000000000000001f 90 nop 20 | 0000000000000020 90 nop 21 | 0000000000000021 e8f4ffffff call 0x1a 22 | ==> With Symbol Resolution 23 | 0000000000000000 01c0 add eax, eax 24 | 0000000000000002 eb11 jmp target 25 | 0000000000000004 01c0 add eax, eax 26 | 0000000000000006 e814000000 call target+10 27 | 000000000000000b 01c0 add eax, eax 28 | 000000000000000d 01c0 add eax, eax 29 | 000000000000000f 7402 jz target-2 30 | 0000000000000011 01c0 add eax, eax 31 | 0000000000000013 90 nop 32 | 0000000000000014 90 nop 33 | 0000000000000015 ebfe jmp target 34 | 0000000000000017 90 nop 35 | 0000000000000018 90 nop 36 | 0000000000000019 ebf8 jmp target-2 37 | 000000000000001b 90 nop 38 | 000000000000001c 90 nop 39 | 000000000000001d 74f6 jz target 40 | 000000000000001f 90 nop 41 | 0000000000000020 90 nop 42 | 0000000000000021 e8f4ffffff call target+5 43 | -------------------------------------------------------------------------------- /udcli/Makefile.am: -------------------------------------------------------------------------------- 1 | bin_PROGRAMS = udcli 2 | udcli_SOURCES = udcli.c 3 | udcli_CFLAGS = -I$(top_srcdir)/libudis86 -I$(top_srcdir) 4 | udcli_LDADD = $(top_builddir)/libudis86/libudis86.la 5 | MAINTAINERCLEANFILES = Makefile.in 6 | -------------------------------------------------------------------------------- /udcli/udcli.c: -------------------------------------------------------------------------------- 1 | /* udis86 - udcli/udcli.c 2 | * 3 | * Copyright (c) 2002-2013 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #include 27 | #include 28 | #include 29 | #include 30 | #ifdef _MSC_VER 31 | #include "..\udis86.h" 32 | #define PACKAGE_STRING "udis86 pre-1.8" 33 | #else 34 | #include 35 | #include 36 | #endif 37 | 38 | #if defined(__APPLE__) 39 | # define FMT64 "ll" 40 | #elif defined(__amd64__) || defined(__x86_64__) 41 | # define FMT64 "l" 42 | # else 43 | # define FMT64 "ll" 44 | #endif 45 | 46 | #if defined(__DJGPP__) || defined(_WIN32) 47 | # include 48 | # include 49 | #endif 50 | 51 | #ifdef __DJGPP__ 52 | # include /* for isatty() */ 53 | # define _setmode setmode 54 | # define _fileno fileno 55 | # define _O_BINARY O_BINARY 56 | #endif 57 | 58 | /* help string */ 59 | static char help[] = 60 | { 61 | "Usage: %s [-option[s]] file\n" 62 | "Options:\n" 63 | " -16 : Set the disassembly mode to 16 bits. \n" 64 | " -32 : Set the disassembly mode to 32 bits. (default)\n" 65 | " -64 : Set the disassembly mode to 64 bits.\n" 66 | " -intel : Set the output to INTEL (NASM like) syntax. (default)\n" 67 | " -att : Set the output to AT&T (GAS like) syntax.\n" 68 | " -v : Set vendor. = {intel, amd}.\n" 69 | " -o : Set the value of program counter to . (default = 0)\n" 70 | " -s : Set the number of bytes to skip before disassembly to .\n" 71 | " -c : Set the number of bytes to disassemble to .\n" 72 | " -x : Set the input mode to whitespace separated 8-bit numbers in\n" 73 | " hexadecimal representation. Example: 0f 01 ae 00\n" 74 | " -noff : Do not display the offset of instructions.\n" 75 | " -nohex : Do not display the hexadecimal code of instructions.\n" 76 | " -h : Display this help message.\n" 77 | " --version: Show version.\n" 78 | "\n" 79 | "Udcli is a front-end to the Udis86 Disassembler Library.\n" 80 | "http://udis86.sourceforge.net/\n" 81 | }; 82 | 83 | FILE* fptr = NULL; 84 | uint64_t o_skip = 0; 85 | uint64_t o_count = 0; 86 | unsigned char o_do_count= 0; 87 | unsigned char o_do_off = 1; 88 | unsigned char o_do_hex = 1; 89 | unsigned char o_do_x = 0; 90 | unsigned o_vendor = UD_VENDOR_AMD; 91 | 92 | int input_hook_x(ud_t* u); 93 | int input_hook_file(ud_t* u); 94 | 95 | int main(int argc, char **argv) 96 | { 97 | char *prog_path = *argv; 98 | char *s; 99 | ud_t ud_obj; 100 | 101 | /* initialize */ 102 | ud_init(&ud_obj); 103 | ud_set_mode(&ud_obj, 32); 104 | ud_set_syntax(&ud_obj, UD_SYN_INTEL); 105 | 106 | #ifdef __DJGPP__ 107 | if ( !isatty( fileno( stdin ) ) ) 108 | #endif 109 | #if defined(__DJGPP) || defined(_WIN32) 110 | _setmode(_fileno(stdin), _O_BINARY); 111 | #endif 112 | 113 | fptr = stdin; 114 | 115 | argv++; 116 | 117 | /* loop through the args */ 118 | while(--argc > 0) { 119 | if (strcmp(*argv, "-h") == 0) { 120 | printf(help, prog_path); 121 | exit(EXIT_SUCCESS); 122 | } else if (strcmp(*argv,"-16") == 0) { 123 | ud_set_mode(&ud_obj, 16); 124 | } else if (strcmp(*argv,"-32") == 0) { 125 | ud_set_mode(&ud_obj, 32); 126 | } else if (strcmp(*argv,"-64") == 0) { 127 | ud_set_mode(&ud_obj, 64); 128 | } else if (strcmp(*argv,"-intel") == 0) 129 | ud_set_syntax(&ud_obj, UD_SYN_INTEL); 130 | else if (strcmp(*argv,"-att") == 0) 131 | ud_set_syntax(&ud_obj, UD_SYN_ATT); 132 | else if (strcmp(*argv,"-noff") == 0) 133 | o_do_off = 0; 134 | else if (strcmp(*argv,"-nohex") == 0) 135 | o_do_hex = 0; 136 | else if (strcmp(*argv,"-x") == 0) 137 | o_do_x = 1; 138 | else if (strcmp(*argv,"-s") == 0) 139 | if (--argc) { 140 | s = *(++argv); 141 | if (sscanf(s, "%" FMT64 "u", &o_skip) == 0) 142 | fprintf(stderr, "Invalid value given for -s.\n"); 143 | } else { 144 | fprintf(stderr, "No value given for -s.\n"); 145 | printf(help, prog_path); 146 | exit(EXIT_FAILURE); 147 | } 148 | else if (strcmp(*argv,"-c") == 0) 149 | if (--argc) { 150 | o_do_count= 1; 151 | s = *(++argv); 152 | if (sscanf(s, "%" FMT64 "u", &o_count) == 0) 153 | fprintf(stderr, "Invalid value given for -c.\n"); 154 | } else { 155 | fprintf(stderr, "No value given for -c.\n"); 156 | printf(help, prog_path); 157 | exit(EXIT_FAILURE); 158 | } 159 | else if (strcmp(*argv,"-v") == 0) 160 | if (--argc) { 161 | s = *(++argv); 162 | if (*s == 'i') 163 | ud_set_vendor(&ud_obj, UD_VENDOR_INTEL); 164 | } else { 165 | fprintf(stderr, "No value given for -v.\n"); 166 | printf(help, prog_path); 167 | exit(EXIT_FAILURE); 168 | } 169 | else if (strcmp(*argv,"-o") == 0) { 170 | if (--argc) { 171 | uint64_t pc = 0; 172 | s = *(++argv); 173 | if (sscanf(s, "%" FMT64 "x", &pc) == 0) 174 | fprintf(stderr, "Invalid value given for -o.\n"); 175 | ud_set_pc(&ud_obj, pc); 176 | } else { 177 | fprintf(stderr, "No value given for -o.\n"); 178 | printf(help, prog_path); 179 | exit(EXIT_FAILURE); 180 | } 181 | } else if ( strcmp( *argv, "--version" ) == 0 ) { 182 | fprintf(stderr, "%s\n", PACKAGE_STRING ); 183 | exit(0); 184 | } else if((*argv)[0] == '-') { 185 | fprintf(stderr, "Invalid option %s.\n", *argv); 186 | printf(help, prog_path); 187 | exit(EXIT_FAILURE); 188 | } else { 189 | static int i = 0; 190 | s = *argv; 191 | if (i) { 192 | fprintf(stderr, "Multiple files specified.\n"); 193 | exit(EXIT_FAILURE); 194 | } else i = 1; 195 | if ((fptr = fopen(s, "rb")) == NULL) { 196 | fprintf(stderr, "Failed to open file: %s.\n", s); 197 | exit(EXIT_FAILURE); 198 | } 199 | } 200 | argv++; 201 | } 202 | 203 | if (o_do_x) 204 | ud_set_input_hook(&ud_obj, input_hook_x); 205 | else ud_set_input_hook(&ud_obj, input_hook_file); 206 | 207 | if (o_skip) { 208 | o_count += o_skip; 209 | ud_input_skip(&ud_obj, o_skip); 210 | } 211 | 212 | /* disassembly loop */ 213 | while (ud_disassemble(&ud_obj)) { 214 | if (o_do_off) 215 | printf("%016" FMT64 "x ", ud_insn_off(&ud_obj)); 216 | if (o_do_hex) { 217 | const char* hex1, *hex2; 218 | hex1 = ud_insn_hex(&ud_obj); 219 | hex2 = hex1 + 16; 220 | printf("%-16.16s %-24s", hex1, ud_insn_asm(&ud_obj)); 221 | if (strlen(hex1) > 16) { 222 | printf("\n"); 223 | if (o_do_off) 224 | printf("%15s -", ""); 225 | printf("%-16s", hex2); 226 | } 227 | } 228 | else printf(" %-24s", ud_insn_asm(&ud_obj)); 229 | 230 | printf("\n"); 231 | } 232 | 233 | exit(EXIT_SUCCESS); 234 | return 0; 235 | } 236 | 237 | int input_hook_x(ud_t* u) 238 | { 239 | unsigned int c, i; 240 | 241 | if (o_do_count) { 242 | if (! o_count) 243 | return UD_EOI; 244 | else --o_count; 245 | } 246 | 247 | i = fscanf(fptr, "%x", &c); 248 | 249 | if (i == EOF) 250 | return UD_EOI; 251 | if (i == 0) { 252 | fprintf(stderr, "Error: Invalid input, should be in hexadecimal form (8-bit).\n"); 253 | return UD_EOI; 254 | } 255 | if (c > 0xFF) 256 | fprintf(stderr, "Warning: Casting non-8-bit input (%x), to %x.\n", c, c & 0xFF); 257 | return (int) (c & 0xFF); 258 | } 259 | 260 | int input_hook_file(ud_t* u) 261 | { 262 | int c; 263 | 264 | if (o_do_count) { 265 | if (! o_count) { 266 | return -1; 267 | } else o_count -- ; 268 | } 269 | 270 | if ((c = fgetc(fptr)) == EOF) 271 | return UD_EOI; 272 | return c; 273 | } 274 | -------------------------------------------------------------------------------- /udis86.h: -------------------------------------------------------------------------------- 1 | /* udis86 - udis86.h 2 | * 3 | * Copyright (c) 2002-2009 Vivek Thampi 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | #ifndef UDIS86_H 27 | #define UDIS86_H 28 | 29 | #include "libudis86/types.h" 30 | #include "libudis86/extern.h" 31 | #include "libudis86/itab.h" 32 | 33 | #endif 34 | --------------------------------------------------------------------------------
34 | udis86.sourceforge.net
67 | Copyright (c) 2008, Vivek Thampi 68 |