224 lines
10 KiB
HTML
224 lines
10 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<!-- This file documents the GNU Assembler "as".
|
|
|
|
Copyright (C) 1991-2016 Free Software Foundation, Inc.
|
|
|
|
Permission is granted to copy, distribute and/or modify this document
|
|
under the terms of the GNU Free Documentation License, Version 1.3
|
|
or any later version published by the Free Software Foundation;
|
|
with no Invariant Sections, with no Front-Cover Texts, and with no
|
|
Back-Cover Texts. A copy of the license is included in the
|
|
section entitled "GNU Free Documentation License".
|
|
-->
|
|
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
|
|
<head>
|
|
<title>Using as: ARM Unwinding Tutorial</title>
|
|
|
|
<meta name="description" content="Using as: ARM Unwinding Tutorial">
|
|
<meta name="keywords" content="Using as: ARM Unwinding Tutorial">
|
|
<meta name="resource-type" content="document">
|
|
<meta name="distribution" content="global">
|
|
<meta name="Generator" content="makeinfo">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<link href="index.html#Top" rel="start" title="Top">
|
|
<link href="AS-Index.html#AS-Index" rel="index" title="AS Index">
|
|
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
|
<link href="ARM_002dDependent.html#ARM_002dDependent" rel="up" title="ARM-Dependent">
|
|
<link href="AVR_002dDependent.html#AVR_002dDependent" rel="next" title="AVR-Dependent">
|
|
<link href="ARM-Mapping-Symbols.html#ARM-Mapping-Symbols" rel="prev" title="ARM Mapping Symbols">
|
|
<style type="text/css">
|
|
<!--
|
|
a.summary-letter {text-decoration: none}
|
|
blockquote.smallquotation {font-size: smaller}
|
|
div.display {margin-left: 3.2em}
|
|
div.example {margin-left: 3.2em}
|
|
div.indentedblock {margin-left: 3.2em}
|
|
div.lisp {margin-left: 3.2em}
|
|
div.smalldisplay {margin-left: 3.2em}
|
|
div.smallexample {margin-left: 3.2em}
|
|
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
|
|
div.smalllisp {margin-left: 3.2em}
|
|
kbd {font-style:oblique}
|
|
pre.display {font-family: inherit}
|
|
pre.format {font-family: inherit}
|
|
pre.menu-comment {font-family: serif}
|
|
pre.menu-preformatted {font-family: serif}
|
|
pre.smalldisplay {font-family: inherit; font-size: smaller}
|
|
pre.smallexample {font-size: smaller}
|
|
pre.smallformat {font-family: inherit; font-size: smaller}
|
|
pre.smalllisp {font-size: smaller}
|
|
span.nocodebreak {white-space:nowrap}
|
|
span.nolinebreak {white-space:nowrap}
|
|
span.roman {font-family:serif; font-weight:normal}
|
|
span.sansserif {font-family:sans-serif; font-weight:normal}
|
|
ul.no-bullet {list-style: none}
|
|
-->
|
|
</style>
|
|
|
|
|
|
</head>
|
|
|
|
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
|
|
<a name="ARM-Unwinding-Tutorial"></a>
|
|
<div class="header">
|
|
<p>
|
|
Previous: <a href="ARM-Mapping-Symbols.html#ARM-Mapping-Symbols" accesskey="p" rel="prev">ARM Mapping Symbols</a>, Up: <a href="ARM_002dDependent.html#ARM_002dDependent" accesskey="u" rel="up">ARM-Dependent</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="AS-Index.html#AS-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<hr>
|
|
<a name="Unwinding"></a>
|
|
<h4 class="subsection">9.4.7 Unwinding</h4>
|
|
|
|
<p>The ABI for the ARM Architecture specifies a standard format for
|
|
exception unwind information. This information is used when an
|
|
exception is thrown to determine where control should be transferred.
|
|
In particular, the unwind information is used to determine which
|
|
function called the function that threw the exception, and which
|
|
function called that one, and so forth. This information is also used
|
|
to restore the values of callee-saved registers in the function
|
|
catching the exception.
|
|
</p>
|
|
<p>If you are writing functions in assembly code, and those functions
|
|
call other functions that throw exceptions, you must use assembly
|
|
pseudo ops to ensure that appropriate exception unwind information is
|
|
generated. Otherwise, if one of the functions called by your assembly
|
|
code throws an exception, the run-time library will be unable to
|
|
unwind the stack through your assembly code and your program will not
|
|
behave correctly.
|
|
</p>
|
|
<p>To illustrate the use of these pseudo ops, we will examine the code
|
|
that G++ generates for the following C++ input:
|
|
</p>
|
|
<pre class="verbatim">void callee (int *);
|
|
|
|
int
|
|
caller ()
|
|
{
|
|
int i;
|
|
callee (&i);
|
|
return i;
|
|
}
|
|
</pre>
|
|
<p>This example does not show how to throw or catch an exception from
|
|
assembly code. That is a much more complex operation and should
|
|
always be done in a high-level language, such as C++, that directly
|
|
supports exceptions.
|
|
</p>
|
|
<p>The code generated by one particular version of G++ when compiling the
|
|
example above is:
|
|
</p>
|
|
<pre class="verbatim">_Z6callerv:
|
|
.fnstart
|
|
.LFB2:
|
|
@ Function supports interworking.
|
|
@ args = 0, pretend = 0, frame = 8
|
|
@ frame_needed = 1, uses_anonymous_args = 0
|
|
stmfd sp!, {fp, lr}
|
|
.save {fp, lr}
|
|
.LCFI0:
|
|
.setfp fp, sp, #4
|
|
add fp, sp, #4
|
|
.LCFI1:
|
|
.pad #8
|
|
sub sp, sp, #8
|
|
.LCFI2:
|
|
sub r3, fp, #8
|
|
mov r0, r3
|
|
bl _Z6calleePi
|
|
ldr r3, [fp, #-8]
|
|
mov r0, r3
|
|
sub sp, fp, #4
|
|
ldmfd sp!, {fp, lr}
|
|
bx lr
|
|
.LFE2:
|
|
.fnend
|
|
</pre>
|
|
<p>Of course, the sequence of instructions varies based on the options
|
|
you pass to GCC and on the version of GCC in use. The exact
|
|
instructions are not important since we are focusing on the pseudo ops
|
|
that are used to generate unwind information.
|
|
</p>
|
|
<p>An important assumption made by the unwinder is that the stack frame
|
|
does not change during the body of the function. In particular, since
|
|
we assume that the assembly code does not itself throw an exception,
|
|
the only point where an exception can be thrown is from a call, such
|
|
as the <code>bl</code> instruction above. At each call site, the same saved
|
|
registers (including <code>lr</code>, which indicates the return address)
|
|
must be located in the same locations relative to the frame pointer.
|
|
</p>
|
|
<p>The <code>.fnstart</code> (see <a href="ARM-Directives.html#arm_005ffnstart">.fnstart pseudo op</a>) pseudo
|
|
op appears immediately before the first instruction of the function
|
|
while the <code>.fnend</code> (see <a href="ARM-Directives.html#arm_005ffnend">.fnend pseudo op</a>) pseudo
|
|
op appears immediately after the last instruction of the function.
|
|
These pseudo ops specify the range of the function.
|
|
</p>
|
|
<p>Only the order of the other pseudos ops (e.g., <code>.setfp</code> or
|
|
<code>.pad</code>) matters; their exact locations are irrelevant. In the
|
|
example above, the compiler emits the pseudo ops with particular
|
|
instructions. That makes it easier to understand the code, but it is
|
|
not required for correctness. It would work just as well to emit all
|
|
of the pseudo ops other than <code>.fnend</code> in the same order, but
|
|
immediately after <code>.fnstart</code>.
|
|
</p>
|
|
<p>The <code>.save</code> (see <a href="ARM-Directives.html#arm_005fsave">.save pseudo op</a>) pseudo op
|
|
indicates registers that have been saved to the stack so that they can
|
|
be restored before the function returns. The argument to the
|
|
<code>.save</code> pseudo op is a list of registers to save. If a register
|
|
is “callee-saved” (as specified by the ABI) and is modified by the
|
|
function you are writing, then your code must save the value before it
|
|
is modified and restore the original value before the function
|
|
returns. If an exception is thrown, the run-time library restores the
|
|
values of these registers from their locations on the stack before
|
|
returning control to the exception handler. (Of course, if an
|
|
exception is not thrown, the function that contains the <code>.save</code>
|
|
pseudo op restores these registers in the function epilogue, as is
|
|
done with the <code>ldmfd</code> instruction above.)
|
|
</p>
|
|
<p>You do not have to save callee-saved registers at the very beginning
|
|
of the function and you do not need to use the <code>.save</code> pseudo op
|
|
immediately following the point at which the registers are saved.
|
|
However, if you modify a callee-saved register, you must save it on
|
|
the stack before modifying it and before calling any functions which
|
|
might throw an exception. And, you must use the <code>.save</code> pseudo
|
|
op to indicate that you have done so.
|
|
</p>
|
|
<p>The <code>.pad</code> (see <a href="ARM-Directives.html#arm_005fpad">.pad</a>) pseudo op indicates a
|
|
modification of the stack pointer that does not save any registers.
|
|
The argument is the number of bytes (in decimal) that are subtracted
|
|
from the stack pointer. (On ARM CPUs, the stack grows downwards, so
|
|
subtracting from the stack pointer increases the size of the stack.)
|
|
</p>
|
|
<p>The <code>.setfp</code> (see <a href="ARM-Directives.html#arm_005fsetfp">.setfp pseudo op</a>) pseudo op
|
|
indicates the register that contains the frame pointer. The first
|
|
argument is the register that is set, which is typically <code>fp</code>.
|
|
The second argument indicates the register from which the frame
|
|
pointer takes its value. The third argument, if present, is the value
|
|
(in decimal) added to the register specified by the second argument to
|
|
compute the value of the frame pointer. You should not modify the
|
|
frame pointer in the body of the function.
|
|
</p>
|
|
<p>If you do not use a frame pointer, then you should not use the
|
|
<code>.setfp</code> pseudo op. If you do not use a frame pointer, then you
|
|
should avoid modifying the stack pointer outside of the function
|
|
prologue. Otherwise, the run-time library will be unable to find
|
|
saved registers when it is unwinding the stack.
|
|
</p>
|
|
<p>The pseudo ops described above are sufficient for writing assembly
|
|
code that calls functions which may throw exceptions. If you need to
|
|
know more about the object-file format used to represent unwind
|
|
information, you may consult the <cite>Exception Handling ABI for the
|
|
ARM Architecture</cite> available from <a href="http://infocenter.arm.com">http://infocenter.arm.com</a>.
|
|
</p>
|
|
|
|
|
|
<hr>
|
|
<div class="header">
|
|
<p>
|
|
Previous: <a href="ARM-Mapping-Symbols.html#ARM-Mapping-Symbols" accesskey="p" rel="prev">ARM Mapping Symbols</a>, Up: <a href="ARM_002dDependent.html#ARM_002dDependent" accesskey="u" rel="up">ARM-Dependent</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="AS-Index.html#AS-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
|
|
|
|
|
|
</body>
|
|
</html>
|