-
Notifications
You must be signed in to change notification settings - Fork 0
/
missingkeyfunction.html
201 lines (188 loc) · 11.8 KB
/
missingkeyfunction.html
1
2
3
4
5
6
7
8
9
10
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Missing Key Function — lld 11 documentation</title>
<link rel="stylesheet" href="_static/llvm.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/language_data.js"></script>
<link rel="shortcut icon" href="_static/favicon.ico"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="ATOM-based lld" href="AtomLLD.html" />
<link rel="prev" title="The ELF, COFF and Wasm Linkers" href="NewLLD.html" />
<style type="text/css">
table.right { float: right; margin-left: 20px; }
table.right td { border: 1px solid #ccc; }
</style>
</head><body>
<div class="logo">
<a href="index.html"><img src="_static/logo.png" alt="LLVM Documentation"/></a>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="AtomLLD.html" title="ATOM-based lld"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="NewLLD.html" title="The ELF, COFF and Wasm Linkers"
accesskey="P">previous</a> |</li>
<li><a href="index.html">lld Home</a> | </li>
<li class="nav-item nav-item-1"><a href="NewLLD.html" accesskey="U">The ELF, COFF and Wasm Linkers</a> »</li>
</ul>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Missing Key Function</a><ul>
<li><a class="reference internal" href="#failing-to-define-a-virtual-destructor">Failing to define a virtual destructor</a></li>
<li><a class="reference internal" href="#forgetting-to-declare-a-virtual-function-in-an-abstract-class-as-pure">Forgetting to declare a virtual function in an abstract class as pure</a></li>
<li><a class="reference internal" href="#key-function-is-defined-but-the-linker-doesn-t-see-it">Key function is defined, but the linker doesn’t see it</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="NewLLD.html"
title="previous chapter">The ELF, COFF and Wasm Linkers</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="AtomLLD.html"
title="next chapter">ATOM-based lld</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/missingkeyfunction.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="missing-key-function">
<h1>Missing Key Function<a class="headerlink" href="#missing-key-function" title="Permalink to this headline">¶</a></h1>
<p>If your build failed with a linker error something like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">foo</span><span class="o">.</span><span class="n">cc</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span> <span class="n">error</span><span class="p">:</span> <span class="n">undefined</span> <span class="n">reference</span> <span class="n">to</span> <span class="s1">'vtable for C'</span>
<span class="n">the</span> <span class="n">vtable</span> <span class="n">symbol</span> <span class="n">may</span> <span class="n">be</span> <span class="n">undefined</span> <span class="n">because</span> <span class="n">the</span> <span class="k">class</span> <span class="nc">is</span> <span class="n">missing</span> <span class="n">its</span> <span class="n">key</span> <span class="n">function</span>
<span class="p">(</span><span class="n">see</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">lld</span><span class="o">.</span><span class="n">llvm</span><span class="o">.</span><span class="n">org</span><span class="o">/</span><span class="n">missingkeyfunction</span><span class="p">)</span>
</pre></div>
</div>
<p>it’s likely that your class C has a key function (defined by the ABI as the first
non-pure, non-inline, virtual function), but you haven’t actually defined it.</p>
<p>When a class has a key function, the compiler emits the vtable (and some other
things as well) only in the translation unit that defines that key function. Thus,
if you’re missing the key function, you’ll also be missing the vtable. If no other
function calls your missing function, you won’t see any undefined reference errors
for it, but you will see undefined references to the vtable symbol.</p>
<p>When a class has no non-pure, non-inline, virtual functions, there is no key
function, and the compiler is forced to emit the vtable in every translation unit
that references the class. In this case, it is emitted in a COMDAT section,
which allows the linker to eliminate all duplicate copies. This is still
wasteful in terms of object file size and link time, so it’s always advisable to
ensure there is at least one eligible function that can serve as the key function.</p>
<p>Here are the most common mistakes that lead to this error:</p>
<div class="section" id="failing-to-define-a-virtual-destructor">
<h2>Failing to define a virtual destructor<a class="headerlink" href="#failing-to-define-a-virtual-destructor" title="Permalink to this headline">¶</a></h2>
<p>Say you have a base class declared in a header file:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">B</span> <span class="p">{</span>
<span class="n">public</span><span class="p">:</span>
<span class="n">B</span><span class="p">();</span>
<span class="n">virtual</span> <span class="o">~</span><span class="n">B</span><span class="p">();</span>
<span class="o">...</span>
<span class="p">};</span>
</pre></div>
</div>
<p>Here, <code class="docutils literal notranslate"><span class="pre">~B</span></code> is the first non-pure, non-inline, virtual function, so it is the key
function. If you forget to define <code class="docutils literal notranslate"><span class="pre">B::~B</span></code> in your source file, the compiler will
not emit the vtable for <code class="docutils literal notranslate"><span class="pre">B</span></code>, and you’ll get an undefined reference to “vtable
for B”.</p>
<p>This is just an example of the more general mistake of forgetting to define the
key function, but it’s quite common because virtual destructors are likely to be
the first eligible key function and it’s easy to forget to implement them. It’s
also more likely that you won’t have any direct references to the destructor, so
you won’t see any undefined reference errors that point directly to the problem.</p>
<p>The solution in this case is to implement the missing function.</p>
</div>
<div class="section" id="forgetting-to-declare-a-virtual-function-in-an-abstract-class-as-pure">
<h2>Forgetting to declare a virtual function in an abstract class as pure<a class="headerlink" href="#forgetting-to-declare-a-virtual-function-in-an-abstract-class-as-pure" title="Permalink to this headline">¶</a></h2>
<p>Say you have an abstract base class declared in a header file:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">A</span> <span class="p">{</span>
<span class="n">public</span><span class="p">:</span>
<span class="n">A</span><span class="p">();</span>
<span class="n">virtual</span> <span class="o">~</span><span class="n">A</span><span class="p">()</span> <span class="p">{}</span>
<span class="n">virtual</span> <span class="nb">int</span> <span class="n">foo</span><span class="p">()</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="o">...</span>
<span class="n">virtual</span> <span class="nb">int</span> <span class="n">bar</span><span class="p">();</span>
<span class="o">...</span>
<span class="p">};</span>
</pre></div>
</div>
<p>This base class is intended to be abstract, but you forgot to mark one of the
functions pure. Here, <code class="docutils literal notranslate"><span class="pre">A::bar</span></code>, being non-pure, is nominated as the key function,
and as a result, the vtable for <code class="docutils literal notranslate"><span class="pre">A</span></code> is not emitted, because the compiler is
waiting for a translation unit that defines <code class="docutils literal notranslate"><span class="pre">A::bar</span></code>.</p>
<p>The solution in this case is to add the missing <code class="docutils literal notranslate"><span class="pre">=</span> <span class="pre">0</span></code> to the declaration of
<code class="docutils literal notranslate"><span class="pre">A::bar</span></code>.</p>
</div>
<div class="section" id="key-function-is-defined-but-the-linker-doesn-t-see-it">
<h2>Key function is defined, but the linker doesn’t see it<a class="headerlink" href="#key-function-is-defined-but-the-linker-doesn-t-see-it" title="Permalink to this headline">¶</a></h2>
<p>It’s also possible that you have defined the key function somewhere, but the
object file containing the definition of that function isn’t being linked into
your application.</p>
<p>The solution in this case is to check your dependencies to make sure that
the object file or the library file containing the key function is given to
the linker.</p>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="AtomLLD.html" title="ATOM-based lld"
>next</a> |</li>
<li class="right" >
<a href="NewLLD.html" title="The ELF, COFF and Wasm Linkers"
>previous</a> |</li>
<li><a href="index.html">lld Home</a> | </li>
<li class="nav-item nav-item-1"><a href="NewLLD.html" >The ELF, COFF and Wasm Linkers</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2011-2020, LLVM Project.
Last updated on 2020-02-14.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.5.
</div>
</body>
</html>