<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Clarkcox.com &#187; Objective-C</title>
	<atom:link href="http://www.clarkcox.com/blog/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.clarkcox.com/blog</link>
	<description>Clark’s musings</description>
	<lastBuildDate>Wed, 21 Sep 2011 18:18:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Link errors when mixing C++ with C/Objective-C</title>
		<link>http://www.clarkcox.com/blog/2009/02/07/link-errors-when-mixing-c-with-cobjective-c/</link>
		<comments>http://www.clarkcox.com/blog/2009/02/07/link-errors-when-mixing-c-with-cobjective-c/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 02:58:08 +0000</pubDate>
		<dc:creator>Clark Cox</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.clarkcox.com/blog/?p=43</guid>
		<description><![CDATA[Twice in the past week, people have asked questions about getting undefined symbol errors when mixing Objective-C and C++. In both cases, this was a result of C++&#8217;s name-mangling. Anytime you want to have a single function that is callable from both C++ and Objective-C (or C, for that matter), you need to disable name [...]]]></description>
			<content:encoded><![CDATA[<p>Twice in the past week, people have asked questions about getting undefined symbol errors when mixing Objective-C and C++. In both cases, this was a result of C++&#8217;s name-mangling. Anytime you want to have a single function that is callable from both C++ and Objective-C (or C, for that matter), you need to disable name mangling for that function.</p>
<h4>Name Mangling</h4>
<p>Normally, in C, the actual symbol name of a function is simply the function&#8217;s name with an underscore prepended; that is, <code>int Foo(int i, float f)</code> becomes simply <code>_Foo</code>. Nice and straightforward.</p>
<p>In C++, on the other hand, the exact same function will be named <code>__Z3fooif</code><sup>[<a href="http://www.clarkcox.com/blog/2009/02/07/link-errors-when-mixing-c-with-cobjective-c/#footnote_0_43" id="identifier_0_43" class="footnote-link footnote-identifier-link" title="Note, the exact encoding can vary from compiler to compiler, but the basic principle remains the same">1</a>]</sup>. At first glance, this seems like unnecessary complication for complication&#8217;s sake, but all becomes clear when you bring function overloading into the picture. Since C++ allows multiple functions with the same name to exist, as long as they accept different parameters, it makes sense that the types of the parameters themselves must somehow be encoded in the symbol name.</p>
<p>This is precisely what <code>__Z3fooif</code> is:</p>
<ul>
<li><code>__Z</code> means “This is a mangled name”.</li>
<li><code>3</code> means “The next part of this name is 3 characters long”.</li>
<li><code>foo</code> is the actual name of the function.</li>
<li><code>if</code> represents the types of the parameters (i.e. “<code><strong>i</strong>nt</code>” and “<code><strong>f</strong>loat</code>”)</li>
</ul>
<p>This allows <code>foo(int,char)</code> to have a different name than <code>foo(int,float)</code> in the binary.</p>
<h4>Now What?</h4>
<p>So, now that we know why C and C++ encode their function names differently, what can we do about it. The designers of C++ foresaw this situation, after all C++ would have had a long row to hoe were it not compatible with C. They added the <code>extern "C"</code> directive to the language.</p>
<p>There are two ways to apply this directive; either to an entire block of code, or to a single function declaration. Each has it&#8217;s advantages and disadvantages:</p>
<ul>
<li><code>extern "C" int foo(int,float);</code>
<p>This disables name mangling on this single function</p>
</li>
<li><code>extern "C" { ... }</code>
<p>This disables name mangling on <em>all</em> functions between the curly braces.</li>
</ul>
<h4>Final Gotcha</h4>
<p>So, we&#8217;re done, right? Not quite. Since C knows nothing of this “<code>extern "C"</code>” business any of the above declarations will fail to compile when passed to a C or Objective-C compiler. They must be conditionally compiled so that only the C++ compiler sees them.</p>
<p>For the single-function version of the declaration, most people define some kind of macro to hide all of this complexity:<br />
<code><br />
#ifdef __cplusplus<br />
#define MY_EXTERN_C extern "C"<br />
#else<br />
#define MY_EXTERN_C extern<br />
#endif<br />
</code></p>
<p>After this is defined, your function declaration becomes:<br />
<code>MY_EXTERN_C int foo(int i, float f);</code></p>
<p>And, for the block style, just wrap the beginning and end of the block with <code>#ifdef __cplusplus</code>, like so:<br />
<code><br />
#ifdef __cplusplus<br />
extern "C" {<br />
#endif</p>
<p>...</p>
<p>#ifdef __cplusplus<br />
}<br />
#endif<br />
</code></p>
<ol class="footnotes"><li id="footnote_0_43" class="footnote">Note, the exact encoding can vary from compiler to compiler, but the basic principle remains the same</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.clarkcox.com/blog/2009/02/07/link-errors-when-mixing-c-with-cobjective-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Inspecting Obj-C parameters in gdb</title>
		<link>http://www.clarkcox.com/blog/2009/02/04/inspecting-obj-c-parameters-in-gdb/</link>
		<comments>http://www.clarkcox.com/blog/2009/02/04/inspecting-obj-c-parameters-in-gdb/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 19:55:55 +0000</pubDate>
		<dc:creator>Clark Cox</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[GDB]]></category>
		<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.clarkcox.com/blog/?p=12</guid>
		<description><![CDATA[Since the addition of i386 and x86_64 to the Mac OS&#8217;s repertoire several years back, remembering which registers are used for what has become difficult, and this can complicate the debugging of code for which you have no symbols. So here is my cheat-sheet (posted here, mostly so that I can find it again without [...]]]></description>
			<content:encoded><![CDATA[<p>Since the addition of i386 and x86_64 to the Mac OS&#8217;s repertoire several years back, remembering which registers are used for what has become difficult, and this can complicate the debugging of code for which you have no symbols. So here is my cheat-sheet (posted here, mostly so that I can find it again without google-ing for old mailing list posts; but, I figure someone else may find it useful as well):</p>
<h4>arm (before prolog)</h4>
<ul>
<li><code>$r0</code> ➡ arg0 (self)</li>
<li><code>$r1</code> ➡ arg1 (_cmd)</li>
<li><code>$r2</code> ➡ arg2</li>
<li><code>$r3</code> ➡ arg3</li>
<li><code>*($sp)</code> ➡ arg4</li>
<li><code>*($sp+4)</code> ➡ arg5</li>
<li><code>*($sp+8)</code> ➡ arg6</li>
</ul>
<h4>ppc/ppc64</h4>
<ul>
<li><code>$r3</code> ➡ arg0 (self)</li>
<li><code>$r4</code> ➡ arg1 (_cmd)</li>
<li><code>$r5</code> ➡ arg2</li>
<li><code>$r6</code> ➡ arg3</li>
<li><code>$r7</code> ➡ arg4</li>
<li><code>$r8</code> ➡ arg5</li>
</ul>
<h4>i386 (before prolog)</h4>
<ul>
<li><code>*($esp+4n)</code> ➡ arg(n)</li>
<li><code>*($esp)</code> ➡ arg0 (self)</li>
<li><code>*($esp+4)</code> ➡ arg1 (_cmd)</li>
<li><code>*($esp+8)</code> ➡ arg2</li>
<li><code>*($esp+12)</code> ➡ arg3</li>
<li><code>*($esp+16)</code> ➡ arg4</li>
<li><code>*($esp+20)</code> ➡ arg5</li>
</ul>
<h4>i386 (after prolog)</h4>
<ul>
<li><code>*($ebp+8+4n)</code> ➡ arg(n)</li>
<li><code>*($ebp+4)</code> ➡ Return addr</li>
<li><code>*($ebp+8)</code> ➡ arg0 (self)</li>
<li><code>*($ebp+12)</code> ➡ arg1 (_cmd)</li>
<li><code>*($ebp+16)</code> ➡ arg2</li>
<li><code>*($ebp+20)</code> ➡ arg3</li>
<li><code>*($ebp+24)</code> ➡ arg4</li>
<li><code>*($ebp+28)</code> ➡ arg5</li>
<li><code>*($ebp)</code> ➡ Previous $ebp</li>
</ul>
<h4>x86_64</h4>
<ul>
<li><code>$rdi</code> ➡ arg0 (self)</li>
<li><code>$rsi</code> ➡ arg1 (_cmd)</li>
<li><code>$rdx</code> ➡ arg2</li>
<li><code>$rcx</code> ➡ arg3</li>
<li><code>$r8</code> ➡ arg4</li>
<li><code>$r9</code> ➡ arg5</li>
</ul>
<p>So, if you have a method defined as:<br />
<code>-(id)method:(id)foo bar:(id)bar baz:(id)baz</code><br />
you can print each of the parameters with:</p>
<table border="1" style="white-space:nowrap;table-layout:fixed" >
<tbody>
<tr>
<th></th>
<th>arm</th>
<th>ppc/ppc64</th>
<th>x86_64</th>
<th>i386 (before prolog)</th>
<th>i386 (after prolog)</th>
</tr>
<tr>
<th>self</th>
<td><code>po&nbsp;$r0</code></td>
<td><code>po&nbsp;$r3</code></td>
<td><code>po&nbsp;$rdi</code></td>
<td><code>po&nbsp;*(id*)($esp)</code></td>
<td><code>po&nbsp;*(id*)($ebp+8)</code></td>
</tr>
<tr>
<th>_cmd</th>
<td><code>p (SEL)$r1</code></td>
<td><code>p (SEL)$r4</code></td>
<td><code>p (SEL)$rsi</code></td>
<td><code>p *(SEL*)($esp+4)</code></td>
<td><code>p *(SEL*)($ebp+12)</code></td>
</tr>
<tr>
<th>foo</th>
<td><code>po&nbsp;$r2</code></td>
<td><code>po&nbsp;$r5</code></td>
<td><code>po&nbsp;$rdx</code></td>
<td><code>po&nbsp;*(id*)($esp+8)</code></td>
<td><code>po&nbsp;*(id*)($ebp+16)</code></td>
</tr>
<tr>
<th>bar</th>
<td><code>po&nbsp;$r3</code></td>
<td><code>po&nbsp;$r6</code></td>
<td><code>po&nbsp;$rcx</code></td>
<td><code>po&nbsp;*(id*)($esp+12)</code></td>
<td><code>po&nbsp;*(id*)($ebp+20)</code></td>
</tr>
<tr>
<th>baz</th>
<td><code>po&nbsp;*(id*)($sp)</code></td>
<td><code>po&nbsp;$r7</code></td>
<td><code>po&nbsp;$r8</code></td>
<td><code>po&nbsp;*(id*)($esp+16)</code></td>
<td><code>po&nbsp;*(id*)($ebp+24)</code></td>
</tr>
</tbody>
</table>
<p>As Blake mentioned in his comment, on i386, if you&#8217;re at the beginning of a function or method, before the prolog has executed (i.e. the bit of code responsible for saving registers, adjusting the stack pointer, etc.), then ebp won&#8217;t have been set up for you yet.<br />
So, I&#8217;ve amended the above table.</p>
<p>That complexity is another reason I long for the simplicity of PowerPC asm, not to mention M68k asm; at least x86_64 has made the step towards using registers for parameters where possible.</p>
<p>Edited to add: In case it isn&#8217;t obvious, these particular stack offsets and registers assignments only make sense when dealing with pointer and integer parameters and return values. When structures and floating point values come into the mix, things can get more complicated.</p>
<p>Edited to add: I&#8217;ve added registers/stack offsets for arm. But note that these are for before the prolog has executed. Arm code seems much looser about what happens in its function prologs, so there really isn&#8217;t a standard layout post-prolog</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clarkcox.com/blog/2009/02/04/inspecting-obj-c-parameters-in-gdb/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

