-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.html
371 lines (307 loc) · 11.9 KB
/
7.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="../static/css/base.css" rel="stylesheet">
<link href="../static/css/tailwind.min.css" rel="stylesheet">
<link href="https://fonts.font.im/css?family=Source+Serif+Pro%7CLato%7CInconsolata" rel="stylesheet"
type="text/css">
<title>CSAPP 讨论会</title>
<!-- browser favicon-->
<link rel="shortcut icon" href="https://cdn-0x4f.do3ob.cn/assets/favicon-gggj2dbz.png">
<style>
.font-sans {
font-family: 'Lato', 'SimHei', 'STHeiti', 'SimHei', 'Serif';
}
.font-serif {
font-family: 'Source Serif Pro', 'Songti SC', 'SimSun', 'Serif', serif;
}
</style>
<link rel="stylesheet" href="../static/katex/katex.min.css">
<script defer src="../static/katex/katex.min.js"></script>
<!-- <script defer src="../static/katex/auto-render.min.js"-->
<!-- integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05"-->
<!-- crossorigin="anonymous"></script>-->
<script defer src="../static/katex/auto-render.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
// customised options
// • auto-render specific keys, e.g.:
delimiters: [
{ left: '$$', right: '$$', display: true },
{ left: '$', right: '$', display: false },
{ left: '\\(', right: '\\)', display: false },
{ left: '\\[', right: '\\]', display: true }
],
// • rendering keys, e.g.:
throwOnError: false
});
});
</script>
<link rel="stylesheet" href="../static/reveal/reveal.css">
<link rel="stylesheet" href="../static/reveal/theme/simple.css" id="theme">
<style>
.reveal .slide-number {
font-size: 26px;
border-radius: 5px;
background-color: rgba(0, 0, 0, .3);
}
.reveal .slides {
border: 1.5px #ddd solid;
border-radius: 7px;
text-align: left;
font-weight: 300;
}
.reveal h1,
.reveal h2,
.reveal h3,
.reveal h4 {
font-family: 'Lato', 'SimHei', 'STXihei', 'Sans Serif';
font-weight: 400;
}
.reveal p,
.reveal li,
.reveal center {
font-size: 32px;
font-family: 'Lato', 'STHeiti', 'SimHei', 'Sans Serif';
}
.reveal li+li {
margin-top: 10px;
}
.reveal ul {
display: block;
margin-right: 15px;
}
.reveal p,
.reveal h1,
.reveal h2,
.reveal h3,
.reveal h4,
.reveal h5 {
padding: 0 25px 0 25px;
}
.reveal table {
font-size: 32px;
font-family: 'Lato', 'STHeiti', 'SimHei', 'Sans Serif';
margin-top: 15px;
margin-bottom: 15px;
}
.reveal th {
background-color: #eee;
}
.reveal tr:nth-child(even) {
background-color: #efffff;
}
.reveal h1,
.reveal h2,
.reveal h3,
.reveal h4,
.reveal h5,
.reveal h6 {
text-align: left;
margin: 0 0 20px 0;
color: #222;
font-weight: 400;
line-height: 1.2;
letter-spacing: normal;
}
.reveal h1 {
margin: 0 10 0 10;
font-size: 60px;
}
.reveal .middle h1 {
text-align: center;
}
.reveal h2 {
font-size: 48px;
border-bottom: 2px solid rgb(106, 0, 95);
padding-bottom: 5px;
}
.reveal h3 {
font-size: 1.15em;
}
.reveal h4 {
font-size: 1.05em;
}
.reveal .center {
text-align: center;
}
.reveal .middle {
height: 728px;
display: flex;
align-items: center;
width: 100%;
}
.reveal pre {
font-size: 28px;
background-color: #eee;
border-radius: 3mm;
padding: 10px 10px 10px 10px;
}
.reveal pre code {
max-height: none;
}
.reveal code {
font-family: 'Inconsolata', 'STKaiti', 'KaiTi', 'Sans Serif', Monospace;
}
.reveal .middle blockquote {
text-align: center;
color: rgb(106, 0, 95);
background: none;
box-shadow: none;
}
.reveal .middle blockquote a {
color: inherit;
}
.reveal blockquote p,
.reveal blockquote li {
font-family: 'Lato', 'STKaiti', 'KaiTi', 'Sans Serif';
}
section .center {
display: block;
margin-left: auto;
margin-right: auto;
}
.reveal .author-block {
margin: 75px 0 35px 0;
}
.reveal .author-affiliation img {
margin: 15px 0 0 0;
}
.reveal .author-block p,
.reveal .author-affiliation p {
font-family: 'Kaiti', 'STKaiti', 'Serif', 'Times', 'Times New Roman';
margin-block-start: 0em;
margin-block-end: 0em;
}
.reveal .author-affiliation {
display: inline-block;
font-size: 90%;
}
.reveal hr {
border: 10px solid rgba(0, 0, 0, 0);
}
.reveal li {
margin-top: 10px;
}
</style>
</head>
<body class="d-flex flex-column h-100">
<div class="reveal">
<div class="slides">
<section>
<div class="center middle"><div style="width:100%"><div>
<h1 class=" text-2xl mt-2 font-sans">CSAPP 讨论会</h1>
<p class=" font-serif my-1">Week5 链接 <br/>
2022/12/31</p>
</div>
</div>
</div>
</section>
<section>
<section> <div>
<h2 class=" text-xl mt-2 pb-2 font-sans">编译链接加载</h2>
<p class=" font-serif my-1">变量有两重信息: <red>符号</red>和<red>类型</red><br/>
<red>类型</red>限制<red>编译器</red>该用哪些汇编指令<br/>
<red>符号</red>限制<red>链接器</red>引用哪个地址 </p>
<p class=" font-serif my-1">链接:将定义符号的地址赋值给引用符号地方, 动态链接将符号处理推迟到加载过程中 </p>
<p class=" font-serif my-1">加载:将代码和数据复制进内在,然后跳转到函数入口执行 </p>
<p class=" font-serif my-1"><a href="https://www.bilibili.com/video/BV1oS4y1T7Uf/">九曲阑干-静态库的链接过程</a></p>
</div> </section>
<section> <div>
<h2 class=" text-xl mt-2 pb-2 font-sans">手动进行静态链接</h2>
<div class="codehilite"><pre class=" bg-gray-100 overflow-x-auto rounded p-2 mb-2 mt-2"><span/><code>gcc -c main.c
gcc -static main.c -Wl,--trace
<span class="nv">link_files</span><span class="o">=(</span>/lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/11/crtbeginT.o /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a /usr/lib/gcc/x86_64-linux-gnu/11/libgcc_eh.a /usr/lib/x86_64-linux-gnu/libc.a /usr/lib/gcc/x86_64-linux-gnu/11/crtend.o /usr/lib/x86_64-linux-gnu/crtn.o<span class="o">)</span>
ld main.o <span class="nv">$link_files</span>
<span class="k">for</span> file <span class="k">in</span> <span class="nv">$link_files</span><span class="p">;</span> <span class="k">do</span> <span class="nb">echo</span> <span class="nv">$file</span><span class="p">;</span> readelf -s <span class="nv">$file</span> <span class="p">|</span> grep <span class="s2">"_Unwind_Resume"</span><span class="p">;</span> <span class="nb">echo</span> <span class="nv">$file</span><span class="p">;</span> <span class="k">done</span>
ld main.o <span class="nv">$link_files</span> /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a /usr/lib/gcc/x86_64-linux-gnu/11/libgcc_eh.a /usr/lib/x86_64-linux-gnu/libc.a
<span class="c1"># 静态链接需要加 start-group</span>
ld main.o /lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/11/crtbeginT.o --start-group /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a /usr/lib/gcc/x86_64-linux-gnu/11/libgcc_eh.a /usr/lib/x86_64-linux-gnu/libc.a --end-group /usr/lib/gcc/x86_64-linux-gnu/11/crtend.o /usr/lib/x86_64-linux-gnu/crtn.o
</code></pre></div>
</div> </section>
<section> <div>
<h2 class=" text-xl mt-2 pb-2 font-sans">手动进行动态链接</h2>
<div class="codehilite"><pre class=" bg-gray-100 overflow-x-auto rounded p-2 mb-2 mt-2"><span/><code>gcc main.o -v
ld main.o /usr/lib/x86_64-linux-gnu/libc.so --dynamic-linker /lib64/ld-linux-x86-64.so.2 /lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o /lib/x86_64-linux-gnu/crtn.o
</code></pre></div>
<blockquote>
<p class=" font-serif my-1">If the linker cannot recognize the format of an object file, it will assume that it is a linker script. <p align="right" class=" font-serif my-1">-- man ld</p></p>
</blockquote>
</div>
<div class="fragment" data-fragment-index="0"> <div>
<p class=" font-serif my-1">链接将符号的地址赋值给引用符号地方</p>
<p class=" font-serif my-1">指针与数组(symbol)</p>
<ul class=" list-disc font-serif">
<li class=" ml-8">extern char foo[]</li>
<li class=" ml-8">extern char* foo</li>
</ul>
</div> </div> </section>
</section>
<section>
<div>
<h2 class=" text-xl mt-2 pb-2 font-sans">链接和加载库的地址是从哪里来的</h2>
<p class=" font-serif my-1">链接</p>
<div class="codehilite"><pre class=" bg-gray-100 overflow-x-auto rounded p-2 mb-2 mt-2"><span/><code>ld --verbose
/usr/lib/x86_64-linux-gnu/ldscripts/
gcc -L. main.c -ladd
</code></pre></div>
<p class=" font-serif my-1">加载</p>
<ul class=" list-disc font-serif">
<li class=" ml-8">LD_PRELOAD</li>
<li class=" ml-8">LD_LIBRARY_PATH</li>
<li class=" ml-8">/etc/ld.so.conf<ul class=" list-disc font-serif">
<li class=" ml-8">ldconfig</li>
</ul>
</li>
</ul>
</div>
</section>
<section>
<section> <div>
<h2 class=" text-xl mt-2 pb-2 font-sans">也会有坏处</h2>
<p class=" font-serif my-1">由此带来的重名符号失效,</p>
<ul class=" list-disc font-serif">
<li class=" ml-8">静态链接符号冲突</li>
<li class=" ml-8">动态链接符号覆盖</li>
</ul>
<blockquote>
<p class=" font-serif my-1">If a symbol is defined in two different libraries gcc will use the first one it finds and ignore the second one unless the second one is included in an object file which gets included for some other reason. <p align="right" class=" font-serif my-1"> --<a href="http://mirrors.zoreil.com/webclub.kcom.ne.jp/ma/colinp/win32/tools/link.html">link</a> </p></p>
</blockquote>
<p class=" font-serif my-1">基于以上几点</p>
<ul class=" list-disc font-serif">
<li class=" ml-8">尽可能的使用 static </li>
<li class=" ml-8">library design:prefix + symbol</li>
</ul>
</div> </section>
<section> <div>
<h2 class=" text-xl mt-2 pb-2 font-sans">解决方法</h2>
<ul class=" list-disc font-serif">
<li class=" ml-8">源码 rename</li>
<li class=" ml-8"><a href="https://stackoverflow.com/questions/27864450/ios-duplicate-symbols-from-different-versions-of-the-same-library">二进制 hack rename</a></li>
</ul>
</div> </section>
</section>
</div>
</div>
<script src="../static/reveal/reveal.js"></script>
<script>
Reveal.initialize({
width: 1024, height: 768,
slideNumber: 'c/t',
controlsTutorial: false,
progress: false,
hash: true,
center: false,
autoAnimateUnmatched: true,
autoAnimateEasing: 'ease-out',
autoAnimateDuration: 0.3,
transitionSpeed: 'fast'
});
</script>
</body>
</html>