{"componentChunkName":"component---src-templates-post-template-js","path":"/windows-windbg-006-symbol-en","result":{"data":{"markdownRemark":{"id":"87373fa7-175c-5ebb-b86e-8ae4e47aa0a8","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/windows-windbg-006-symbol\">original page</a>.</p>\n</blockquote>\n<p>My goal is to become proficient with WinDbg for Windows debugging and dump-based troubleshooting.</p>\n<p>For a full list of articles on Windows debugging and dump analysis with WinDbg, see the index page:</p>\n<p>Reference: <a href=\"/windows-windbg-001-index-en\">Debugging and Troubleshooting Techniques with WinDbg</a></p>\n<p>This article introduces the build environment used to compile the sample programs featured in the WinDbg series above.</p>\n<p>I’ll be setting up the environment on Ubuntu 20.04 running on WSL2 to satisfy the following requirements.\nThe setup uses Docker containers, so it should work in any environment where Docker is available.</p>\n<ol>\n<li><strong>Cross-compile EXE files in a Linux environment</strong></li>\n<li><strong>Generate symbol files (.pdb files) in a Linux environment</strong></li>\n</ol>\n<!-- omit in toc -->\n<h2 id=\"table-of-contents\" style=\"position:relative;\"><a href=\"#table-of-contents\" aria-label=\"table of contents permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Table of Contents</h2>\n<ul>\n<li><a href=\"#setting-up-the-build-environment\">Setting Up the Build Environment</a></li>\n<li><a href=\"#what-are-symbol-files-pdb-files\">What Are Symbol Files (.pdb Files)?</a></li>\n<li><a href=\"#how-to-generate-symbol-files-during-compilation-in-a-linux-environment\">How to Generate Symbol Files During Compilation in a Linux Environment</a></li>\n<li><a href=\"#preparing-the-llvm-mingw-environment\">Preparing the llvm-mingw Environment</a></li>\n<li><a href=\"#compiling-a-c-file-with-a-pdb-file-using-llvm-mingw\">Compiling a C++ File with a PDB File Using llvm-mingw</a></li>\n<li><a href=\"#wrap-up\">Wrap-up</a></li>\n</ul>\n<h2 id=\"setting-up-the-build-environment\" style=\"position:relative;\"><a href=\"#setting-up-the-build-environment\" aria-label=\"setting up the build environment permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setting Up the Build Environment</h2>\n<p>The sample programs for WinDbg testing are hosted in the following repository:</p>\n<p>Reference: <a href=\"https://github.com/kash1064/Try2WinDbg\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">kash1064/Try2WinDbg</a></p>\n<p>First, clone the repository into any directory on an OS where Docker is available.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">git</span> clone https://github.com/kash1064/Try2WinDbg</code></pre></div>\n<p>Next, pull the following container image for compilation:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">docker</span> pull kashiwabayuki/try2windbg:1.0</code></pre></div>\n<p>Reference: <a href=\"https://hub.docker.com/r/kashiwabayuki/try2windbg\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">kashiwabayuki/try2windbg</a></p>\n<p>This container image is a customized version of the <a href=\"https://hub.docker.com/r/mstorsjo/llvm-mingw/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">mstorsjo/llvm-mingw</a> image. Details are described later.</p>\n<p>Once the repository and container image have been downloaded, change into the <code class=\"language-text\">Try2WinDbg</code> directory.</p>\n<p>Run the following commands to generate compiled EXE files and symbol files directly under <code class=\"language-text\">Try2WinDbg/src</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token builtin class-name\">cd</span> Try2WinDbg\n\n<span class=\"token comment\"># Specify the container image to use for the build</span>\n<span class=\"token assign-left variable\">CONTAINER</span><span class=\"token operator\">=</span>kashiwabayuki/try2windbg:1.0\n<span class=\"token function\">docker</span> run --rm -it -v <span class=\"token variable\"><span class=\"token variable\">`</span><span class=\"token builtin class-name\">pwd</span><span class=\"token variable\">`</span></span>/src:/try2windbg <span class=\"token variable\">$CONTAINER</span> <span class=\"token function\">bash</span> -c <span class=\"token string\">\"cd /try2windbg &amp;&amp; make\"</span></code></pre></div>\n<p>The environment setup is now complete.</p>\n<h2 id=\"what-are-symbol-files-pdb-files\" style=\"position:relative;\"><a href=\"#what-are-symbol-files-pdb-files\" aria-label=\"what are symbol files pdb files permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What Are Symbol Files (.pdb Files)?</h2>\n<p>Files with the <code class=\"language-text\">.pdb</code> extension are called <strong>symbol files</strong>.</p>\n<p>PDB stands for <strong>Program Database</strong>. A PDB file maps identifiers and statements in a project’s source code to the corresponding identifiers and instructions in the compiled application.</p>\n<p>Using symbol files makes it significantly more efficient to analyze applications and processes with a debugger.</p>\n<p>Analysis is still possible without symbol files, but there is a notable difference in how the debugger displays information for the same address depending on whether an appropriate symbol file is loaded:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">sample+0x110     <span class=\"token comment\"># Without symbol file</span>\nsample<span class=\"token operator\">!</span>main+0x10 <span class=\"token comment\"># With symbol file</span></code></pre></div>\n<p>Loading symbol files properly allows you to quickly identify suspect locations, infer behavior from function names, and debug more efficiently overall.</p>\n<p>Reference: <a href=\"https://docs.microsoft.com/en-us/visualstudio/debugger/specify-symbol-dot-pdb-and-source-files-in-the-visual-studio-debugger?view=vs-2019\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Specify symbol (.pdb) and source files in the Visual Studio debugger | Microsoft Docs</a></p>\n<h2 id=\"how-to-generate-symbol-files-during-compilation-in-a-linux-environment\" style=\"position:relative;\"><a href=\"#how-to-generate-symbol-files-during-compilation-in-a-linux-environment\" aria-label=\"how to generate symbol files during compilation in a linux environment permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to Generate Symbol Files During Compilation in a Linux Environment</h2>\n<p>Symbol files are critically important when debugging Windows applications. When using the Microsoft compiler, they are generated automatically at build time.</p>\n<p>However, when cross-compiling in a Linux environment using tools like MinGW, symbol files are not normally produced.</p>\n<p>Some resources — such as the Stack Overflow thread linked below — suggest using <code class=\"language-text\">cv2pdb</code> to create symbol files for MinGW cross-compiled EXE files, but this approach does not work on Linux.</p>\n<p>Reference: <a href=\"https://stackoverflow.com/questions/19269350/how-to-generate-pdb-files-while-building-library-using-mingw/28627790\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">c++ - how to generate pdb files while building library using mingw? - Stack Overflow</a></p>\n<p>Therefore, I used <a href=\"https://github.com/mstorsjo/llvm-mingw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">llvm-mingw</a> instead.</p>\n<p><a href=\"https://github.com/mstorsjo/llvm-mingw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">llvm-mingw</a> is a <code class=\"language-text\">mingw-w64</code> toolchain based on <a href=\"https://llvm.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">LLVM</a>/<a href=\"https://clang.llvm.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Clang</a>/<a href=\"https://lld.llvm.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">LLD</a>.</p>\n<p>Reference: <a href=\"https://github.com/mstorsjo/llvm-mingw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">mstorsjo/llvm-mingw: An LLVM/Clang/LLD based mingw-w64 toolchain</a></p>\n<p>In short, LLVM is a platform-independent compiler infrastructure capable of compiling any programming language. Clang and LLD are the C compiler and linker for LLVM respectively.</p>\n<p><a href=\"https://github.com/mstorsjo/llvm-mingw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">llvm-mingw</a> is essentially a version of MinGW where the GNU-based binutils have been replaced by LLVM-based binutils.</p>\n<p>This makes it possible to compile for multiple computer architectures (i686, x86_64, armv7, arm64) with a single toolchain, and also enables generating symbol files in PDB format.</p>\n<h2 id=\"preparing-the-llvm-mingw-environment\" style=\"position:relative;\"><a href=\"#preparing-the-llvm-mingw-environment\" aria-label=\"preparing the llvm mingw environment permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Preparing the llvm-mingw Environment</h2>\n<p>The easiest way to get an environment with LLVM-based MinGW is to use the official <a href=\"https://hub.docker.com/r/mstorsjo/llvm-mingw/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Docker image</a>.</p>\n<p>In most cases, simply pulling this image from Docker Hub is all you need.</p>\n<p>If you need to set up the llvm-mingw environment directly on a Linux host rather than in a Docker container, you can refer to the scripts in the following Dockerfile:</p>\n<p>Reference: <a href=\"https://github.com/mstorsjo/llvm-mingw/blob/master/Dockerfile.cross\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">llvm-mingw/Dockerfile.cross at master · mstorsjo/llvm-mingw</a></p>\n<h2 id=\"compiling-a-c-file-with-a-pdb-file-using-llvm-mingw\" style=\"position:relative;\"><a href=\"#compiling-a-c-file-with-a-pdb-file-using-llvm-mingw\" aria-label=\"compiling a c file with a pdb file using llvm mingw permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Compiling a C++ File with a PDB File Using llvm-mingw</h2>\n<p>Here is how to use llvm-mingw.</p>\n<p>In the official Docker image, the LLVM-based MinGW compiler is already on the PATH as <code class=\"language-text\">x86_64-w64-mingw32-g++</code>.</p>\n<p>Pass the <code class=\"language-text\">-Wl,-pdb=&lt;filename>.pdb</code> option when compiling to generate both an EXE file and a symbol file at the same time:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">x86_64-w64-mingw32-g++ -Wl,-pdb<span class=\"token operator\">=</span>sample.pdb sample.cpp -o sample.exe</code></pre></div>\n<h2 id=\"wrap-up\" style=\"position:relative;\"><a href=\"#wrap-up\" aria-label=\"wrap up permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Wrap-up</h2>\n<p>In this article, I summarized how to generate a debug symbol file when cross-compiling a Windows EXE in a Linux environment.</p>","fields":{"slug":"/windows-windbg-006-symbol-en","tagSlugs":["/tag/win-dbg-en/","/tag/kernel-en/","/tag/reversing-en/","/tag/english/"]},"frontmatter":{"date":"2021-10-18","description":"","tags":["WinDbg (en)","Kernel (en)","Reversing (en)","English"],"title":"How to Generate Symbol Files (.pdb) in a Linux Environment Using llvm-mingw","socialImage":{"publicURL":"/static/362a371d01282684c0ad5266d80a86e8/windows-windbg-006-symbol.png"}}}},"pageContext":{"slug":"/windows-windbg-006-symbol-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}