`
byytj
  • 浏览: 48536 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

Introduction to x64 debugging, part 2

 
阅读更多

Introduction to x64 debugging, part 2

Last time, I talked about some of the basic differences you’ll see when switching to an x64 system if you are doing debugging using the Debugging Tools for Windows package.  In this installment, I’ll run through some of the other differences with debugging that you’ll likely run into – in particular, how changes to the x64 calling convention will make your life much easier when debugging.

Although the x64 architecture is in many respects very similar to x86, many of the conventions of x86-Win32 that you might be familiar with have changed.  Microsoft took the opportunity to “clean house” with many aspects of Win64, since for native x64 programs, there is no concern of backwards binary compatibility.

One of the major changes that you will quickly discover is that the calling conventions that x86 used (__fastcall, __cdecl, __stdcall) are not applicable to x64.  Instead of many different calling conventions, x64 unifies everything into a single calling conention that all functions use.  You can read the full details of the new calling convention on MSDN, but I’ll give you the executive summary as it applies to debugging programs here.

  •  The first four arguments of a function are passed as registers; rcx, rdx, r8, and r9 respectively.  Subsequent arguments are passed on the stack.
  • The caller allocates the space on the stack for parameter passing, like for __stdcall on x86.  However, the caller must allocate at least 32 bytes of stack space for the callee to use a “register home space” the first four parameters (or scratch space).  This must be done even if the callee has no arguments or less than four arguments.
  • The caller always cleans the stack of arguments passed (like __cdecl on x86) if necessary.
  • Stack unwinding and exception handling are significantly different on x64; more details on that later.  The new stack unwinding model is data-driven rather than code-driven (like on x86).
  • Except for dynamic stack adjustments (like _alloca), all stack space must be allocated in the prologue.  Effectively, for most functions, the stack pointer will remain constant throughout the execution process.
  • The rax register is used for return values.  For return values larger than 64 bits, a hidden pointer argument is used.  There is no more spillover into a second register for large return values (like edx:eax, on x86).
  • The rax, rcx, rdx, r8, r9, r10, r11 registers are volatile, all other registers must be preserved.  For floating point usage, the xmm0, xmm1, xmml2, xmm3, xmm4, xmm5 registers are volatile, and the other registers must be preserved.
  • For floating point arguments, the xmm0 through xmm3 registers are used for the first four arguments, after which stack spillover is performed.
  • The instructions permitted in function prologues and epilogues are highly restricted to a very small subset of the instruction set to facilitate unwinding operations.

The main takeaways here from a debugging pespective are thus:

  • Even though a register calling convention like __fastcall is used, the register arguments are often spilled to the “home area” and so are typically visible in call stacks, especially in debug builds.
  • Due to the nature of parameter passing on x64, the “push” instruction is seldom used for setting up arguments.  Instead, the compiler allocates all space up front (like for local variables on x86) and uses the “mov” instruction to write stack parameters onto the stack for function calls.  This also means that you typically will not see an “add rsp” (or equivalent) after each function call, despite the fact that the caller cleans the stack space.
  • The first stack arguments (argument 5, etc) will appear at [rsp+28h] instead of [rsp+08h], because of the mandatory register home area.  This is a departure from how __fastcall worked on x86, where the first stack argument would be at [esp+04h].
  • Because of the data driven unwind semantics, you will see perfect stack unwinding even without symbols.  This means that even if you don’t have any symbols at all for a third party binary, you should always get a complete stack trace all the way back to the thread start routine.  As a side effect, this means that the stack traces captured by PageHeap or handle traces will be much more reliable than on x86, where they tended break at the first function that did not use ebp (because those stack traces never used symbols).
  • Because of the restrictions on the prologue and epilogue instruction usage, it is very easy to recognize where the actual important function code begins and the boilerplate prologue/epilogue code ends.

If you’ve been debugging on x86 for a long time, then you are probably pretty excited about the features of the new calling convention.  Because of the perfect unwind semantics and constant stack pointer throughout function execution model, debugging code that you don’t have symbols for (and using the built-in heap and handle verification utilities) is much more reliable than x86.  Additionally, compiler generated code is usually easier to understand, because you don’t have to manually track the value of the stack pointer changing throughout the function call like you often did on x86 functions compiled with frame pointer omission (FPO) optimizations.

 There are some exceptions to the rules I laid out above for the x64 calling convention.  For functions that do not call any other functions (called “leaf” functions), it is permissible to utilize custom calling conventions so long as the stack pointer (rsp) is not modified.  If the stack pointer is modified then regular calling convention semantics are required.

Next time, I’ll go into more detail on how exception handling and unwinding is different on x64 from the perspective of what the changes mean to you if you are debugging programs, and how you can access some of the metadata associated with unwinding/exception handling and use it to your advantage within the debugger.

分享到:
评论

相关推荐

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    As alluded to in the introduction to this book, debugging is not entirely about just fixing stuff. Debugging is the process of gaining a better understanding of what’s happening behind the scenes. In...

    debugging 1

    Cambridge大学信息技术中心的教程 debugging part 1, introduction and general principles

    DSP程序员指南

    Part I: Introduction includes a brief description of the ’C6000 architecture and code development flow. It also includes a tutorial that introduces you to the tools you will use in each phase of ...

    C.in.a.Nutshell.2nd.Edition.1491904755

    New edition includes multithreading and an introduction to IDEs Covers building and debugging Table of Contents Part I. Language Chapter 1. Language Basics Chapter 2. Types Chapter 3. Literals Chapter...

    Visual-Studio-Code-End-To-End--editing and debuging for web developer.pdf

    It starts with an introduction to the editing features of the workspace and continues with the more advanced functionality (including refactoring and key bindings). The book finishes with an ...

    C in a nutshell 2nd English

    ultithreading and an introduction to IDEs Covers building and debugging Table of Contents Part I. Language Chapter 1. Language Basics Chapter 2. Types Chapter 3. Literals Chapter 4. Type Conversions ...

    beginning VisualBasic2010(英文版)入门必读

    You′ll also work with events, create toolbars, buttons, forms, display dialog boxes, and create menus.Chapter 10 provides an introduction to application debugging and error handling.Chapters 11 ...

    Wiley.Publishing.Professional.C++.2005.pdf

    Part I: Introduction to Professional C++ Chapter 1: A Crash Course in C++ Chapter 2: Designing Professional C++ Programs Chapter 3: Designing with Objects Chapter 4: Designing with Libraries and ...

    Beginning.Ruby.From.Novice.to.Professional.3rd.Edition.1484212797

    The new edition of this book provides the same excellent introduction to Ruby as the previous editions plus updates for the newest version of Ruby 2.3. This book can also be used as a textbook or ...

    [Python] Python 编程实践 第1版 (英文版)

    Computers are used in every part of science from ecology to particle physics. This introduction to computer science continually reinforces those ties by using real-world science problems as examples. ...

    不错的调试工具WinDBG

    Introduction to Debugging This documentation describes the use of four Microsoft® debuggers: CDB, NTSD, KD, and WinDbg. It also describes the use of several other debugging tools. These debugging ...

    Docker in Practice

    machines, is used as the basis for chapter 3 to provide an introduction to Docker use. Chapter 4 then details a number of Docker techniques we’ve found ourselves using every day. The final chapter in...

    Ajax for Web Application Developers(Ajax网站开发)

    An Introduction to the Book Samples Part I: Getting Started Chapter 1. Introduction to Ajax The XML DOM Measuring the Benefits Chapter 2. The Request An In-Depth Look at XMLHttpRequest ...

    vmware unlocker 2.0.6最新版(vm虚拟机破解安装Mac OS)

    an upgrade or patch to ESXi as local.sh is part of the persisted local state. 8. Thanks Thanks to Zenith432 for originally building the C++ unlocker and Mac Son of Knife (MSoK) for all the testing ...

    Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner.part2

    Chapter 1 - An Introduction to Visual Basic 2005 Express Edition Chapter 2 - Navigating the Visual Basic 2005 Express Edition Development Environment Chapter 3 - Creating an ...

    Mastering+Resin.rar

    Part I - Introduction to Resin Chapter 1 - An Overview of Resin Chapter 2 - Setting up the Resin Server Part II - Application Development with Resin Chapter 3 - Using JSP and Resin Chapter ...

    Beginning.Csharp.6.0.Programming.with.Visual.Studio.2015.11190

    After your introduction to each of the chapters, you are invited to apply your newfound knowledge in Try it Out sections, which reinforce learning and help you understand the practical applications ...

    BlazeDS开发者指南

    Chapter 1: Introduction to BlazeDS BlazeDS overview 2 BlazeDS features 3 Example BlazeDS applications 5 Chapter 2: Building and deploying BlazeDS applications Setting up your development environment ...

    GNULinuxApplicationProgramming

    also licenses, and a brief introduction to open source development and licenses. Linux virtualization is also explored, including models and options in Linux. Part II, “GNU Tools,” concentrates on ...

Global site tag (gtag.js) - Google Analytics