Debian Codegraph @

llvm-project/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp

raw
1 //===------ LinkGraphTests.cpp - Unit tests for core JITLink classes ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/STLExtras.h"
10 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
11 #include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
12 #include "llvm/Support/Endian.h"
13 #include "llvm/Support/Memory.h"
14
15 #include "llvm/Testing/Support/Error.h"
16 #include "gtest/gtest.h"
17
18 using namespace llvm;
19 using namespace llvm::jitlink;
20
21 static const char BlockContentBytes[] = {
22 0x54, 0x68, 0x65, 0x72, 0x65, 0x20, 0x77, 0x61, 0x73, 0x20, 0x6d, 0x6f,
23 0x76, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x61, 0x74, 0x20, 0x74, 0x68,
24 0x65, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x66,
25 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x6f, 0x72, 0x64, 0x20,
26 0x68, 0x61, 0x64, 0x20, 0x70, 0x61, 0x73, 0x73, 0x65, 0x64, 0x20, 0x61,
27 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x0a, 0x54, 0x68, 0x61, 0x74, 0x20, 0x74,
28 0x68, 0x65, 0x20, 0x63, 0x6f, 0x6c, 0x74, 0x20, 0x66, 0x72, 0x6f, 0x6d,
29 0x20, 0x4f, 0x6c, 0x64, 0x20, 0x52, 0x65, 0x67, 0x72, 0x65, 0x74, 0x20,
30 0x68, 0x61, 0x64, 0x20, 0x67, 0x6f, 0x74, 0x20, 0x61, 0x77, 0x61, 0x79,
31 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20, 0x68, 0x61, 0x64, 0x20, 0x6a, 0x6f,
32 0x69, 0x6e, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x69, 0x6c,
33 0x64, 0x20, 0x62, 0x75, 0x73, 0x68, 0x20, 0x68, 0x6f, 0x72, 0x73, 0x65,
34 0x73, 0x20, 0x2d, 0x2d, 0x20, 0x68, 0x65, 0x20, 0x77, 0x61, 0x73, 0x20,
35 0x77, 0x6f, 0x72, 0x74, 0x68, 0x20, 0x61, 0x20, 0x74, 0x68, 0x6f, 0x75,
36 0x73, 0x61, 0x6e, 0x64, 0x20, 0x70, 0x6f, 0x75, 0x6e, 0x64, 0x2c, 0x0a,
37 0x53, 0x6f, 0x20, 0x61, 0x6c, 0x6c, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63,
38 0x72, 0x61, 0x63, 0x6b, 0x73, 0x20, 0x68, 0x61, 0x64, 0x20, 0x67, 0x61,
39 0x74, 0x68, 0x65, 0x72, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x74, 0x68,
40 0x65, 0x20, 0x66, 0x72, 0x61, 0x79, 0x2e, 0x0a, 0x41, 0x6c, 0x6c, 0x20,
41 0x74, 0x68, 0x65, 0x20, 0x74, 0x72, 0x69, 0x65, 0x64, 0x20, 0x61, 0x6e,
42 0x64, 0x20, 0x6e, 0x6f, 0x74, 0x65, 0x64, 0x20, 0x72, 0x69, 0x64, 0x65,
43 0x72, 0x73, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x74, 0x68, 0x65, 0x20,
44 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x6e, 0x65, 0x61,
45 0x72, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x61, 0x72, 0x0a, 0x48, 0x61,
46 0x64, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x65, 0x72, 0x65, 0x64, 0x20, 0x61,
47 0x74, 0x20, 0x74, 0x68, 0x65, 0x20, 0x68, 0x6f, 0x6d, 0x65, 0x73, 0x74,
48 0x65, 0x61, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x6e, 0x69, 0x67, 0x68,
49 0x74, 0x2c, 0x0a, 0x46, 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62,
50 0x75, 0x73, 0x68, 0x6d, 0x65, 0x6e, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x20,
51 0x68, 0x61, 0x72, 0x64, 0x20, 0x72, 0x69, 0x64, 0x69, 0x6e, 0x67, 0x20,
52 0x77, 0x68, 0x65, 0x72, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x69,
53 0x6c, 0x64, 0x20, 0x62, 0x75, 0x73, 0x68, 0x20, 0x68, 0x6f, 0x72, 0x73,
54 0x65, 0x73, 0x20, 0x61, 0x72, 0x65, 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20,
55 0x74, 0x68, 0x65, 0x20, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2d, 0x68, 0x6f,
56 0x72, 0x73, 0x65, 0x20, 0x73, 0x6e, 0x75, 0x66, 0x66, 0x73, 0x20, 0x74,
57 0x68, 0x65, 0x20, 0x62, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x20, 0x77, 0x69,
58 0x74, 0x68, 0x20, 0x64, 0x65, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x2e, 0x00};
59
60 static ArrayRef<char> BlockContent(BlockContentBytes);
61
62 TEST(LinkGraphTest, Construction) {
63 // Check that LinkGraph construction works as expected.
64 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
65 getGenericEdgeKindName);
66 EXPECT_EQ(G.getName(), "foo");
67 EXPECT_EQ(G.getTargetTriple().str(), "x86_64-apple-darwin");
68 EXPECT_EQ(G.getPointerSize(), 8U);
69 EXPECT_EQ(G.getEndianness(), support::little);
70 EXPECT_TRUE(G.external_symbols().empty());
71 EXPECT_TRUE(G.absolute_symbols().empty());
72 EXPECT_TRUE(G.defined_symbols().empty());
73 EXPECT_TRUE(G.blocks().empty());
74 }
75
76 TEST(LinkGraphTest, AddressAccess) {
77 // Check that we can get addresses for blocks, symbols, and edges.
78 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
79 getGenericEdgeKindName);
80
81 auto &Sec1 =
82 G.createSection("__data.1", orc::MemProt::Read | orc::MemProt::Write);
83 orc::ExecutorAddr B1Addr(0x1000);
84 auto &B1 = G.createContentBlock(Sec1, BlockContent, B1Addr, 8, 0);
85 auto &S1 = G.addDefinedSymbol(B1, 4, "S1", 4, Linkage::Strong, Scope::Default,
86 false, false);
87 B1.addEdge(Edge::FirstRelocation, 8, S1, 0);
88 auto &E1 = *B1.edges().begin();
89
90 EXPECT_EQ(B1.getAddress(), B1Addr) << "Incorrect block address";
91 EXPECT_EQ(S1.getAddress(), B1Addr + 4) << "Incorrect symbol address";
92 EXPECT_EQ(B1.getFixupAddress(E1), B1Addr + 8) << "Incorrect fixup address";
93 }
94
95 TEST(LinkGraphTest, BlockAndSymbolIteration) {
96 // Check that we can iterate over blocks within Sections and across sections.
97 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
98 getGenericEdgeKindName);
99 auto &Sec1 =
100 G.createSection("__data.1", orc::MemProt::Read | orc::MemProt::Write);
101 orc::ExecutorAddr B1Addr(0x1000);
102 auto &B1 = G.createContentBlock(Sec1, BlockContent, B1Addr, 8, 0);
103 orc::ExecutorAddr B2Addr(0x2000);
104 auto &B2 = G.createContentBlock(Sec1, BlockContent, B2Addr, 8, 0);
105 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", 4, Linkage::Strong, Scope::Default,
106 false, false);
107 auto &S2 = G.addDefinedSymbol(B2, 4, "S2", 4, Linkage::Strong, Scope::Default,
108 false, false);
109
110 auto &Sec2 =
111 G.createSection("__data.2", orc::MemProt::Read | orc::MemProt::Write);
112 orc::ExecutorAddr B3Addr(0x3000);
113 auto &B3 = G.createContentBlock(Sec2, BlockContent, B3Addr, 8, 0);
114 orc::ExecutorAddr B4Addr(0x4000);
115 auto &B4 = G.createContentBlock(Sec2, BlockContent, B4Addr, 8, 0);
116 auto &S3 = G.addDefinedSymbol(B3, 0, "S3", 4, Linkage::Strong, Scope::Default,
117 false, false);
118 auto &S4 = G.addDefinedSymbol(B4, 4, "S4", 4, Linkage::Strong, Scope::Default,
119 false, false);
120
121 // Check that iteration of blocks within a section behaves as expected.
122 EXPECT_EQ(std::distance(Sec1.blocks().begin(), Sec1.blocks().end()), 2);
123 EXPECT_TRUE(llvm::count(Sec1.blocks(), &B1));
124 EXPECT_TRUE(llvm::count(Sec1.blocks(), &B2));
125
126 // Check that iteration of symbols within a section behaves as expected.
127 EXPECT_EQ(std::distance(Sec1.symbols().begin(), Sec1.symbols().end()), 2);
128 EXPECT_TRUE(llvm::count(Sec1.symbols(), &S1));
129 EXPECT_TRUE(llvm::count(Sec1.symbols(), &S2));
130
131 // Check that iteration of blocks across sections behaves as expected.
132 EXPECT_EQ(std::distance(G.blocks().begin(), G.blocks().end()), 4);
133 EXPECT_TRUE(llvm::count(G.blocks(), &B1));
134 EXPECT_TRUE(llvm::count(G.blocks(), &B2));
135 EXPECT_TRUE(llvm::count(G.blocks(), &B3));
136 EXPECT_TRUE(llvm::count(G.blocks(), &B4));
137
138 // Check that iteration of defined symbols across sections behaves as
139 // expected.
140 EXPECT_EQ(
141 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 4);
142 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S1));
143 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S2));
144 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S3));
145 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S4));
146 }
147
148 TEST(LinkGraphTest, ContentAccessAndUpdate) {
149 // Check that we can make a defined symbol external.
150 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
151 getGenericEdgeKindName);
152 auto &Sec =
153 G.createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
154
155 // Create an initial block.
156 orc::ExecutorAddr BAddr(0x1000);
157 auto &B = G.createContentBlock(Sec, BlockContent, BAddr, 8, 0);
158
159 EXPECT_FALSE(B.isContentMutable()) << "Content unexpectedly mutable";
160 EXPECT_EQ(B.getContent().data(), BlockContent.data())
161 << "Unexpected block content data pointer";
162 EXPECT_EQ(B.getContent().size(), BlockContent.size())
163 << "Unexpected block content size";
164
165 // Expect that attempting to get already-mutable content fails if the
166 // content is not yet mutable (debug builds only).
167 #ifndef NDEBUG
168 EXPECT_DEATH({ (void)B.getAlreadyMutableContent(); },
169 "Content is not mutable")
170 << "Unexpected mutable access allowed to immutable data";
171 #endif
172
173 // Check that mutable content is copied on request as expected.
174 auto MutableContent = B.getMutableContent(G);
175 EXPECT_TRUE(B.isContentMutable()) << "Content unexpectedly immutable";
176 EXPECT_NE(MutableContent.data(), BlockContent.data())
177 << "Unexpected mutable content data pointer";
178 EXPECT_EQ(MutableContent.size(), BlockContent.size())
179 << "Unexpected mutable content size";
180 EXPECT_TRUE(std::equal(MutableContent.begin(), MutableContent.end(),
181 BlockContent.begin()))
182 << "Unexpected mutable content value";
183
184 // Check that already-mutable content behaves as expected, with no
185 // further copies.
186 auto MutableContent2 = B.getMutableContent(G);
187 EXPECT_TRUE(B.isContentMutable()) << "Content unexpectedly immutable";
188 EXPECT_EQ(MutableContent2.data(), MutableContent.data())
189 << "Unexpected mutable content 2 data pointer";
190 EXPECT_EQ(MutableContent2.size(), MutableContent.size())
191 << "Unexpected mutable content 2 size";
192
193 // Check that getAlreadyMutableContent behaves as expected, with no
194 // further copies.
195 auto MutableContent3 = B.getMutableContent(G);
196 EXPECT_TRUE(B.isContentMutable()) << "Content unexpectedly immutable";
197 EXPECT_EQ(MutableContent3.data(), MutableContent.data())
198 << "Unexpected mutable content 2 data pointer";
199 EXPECT_EQ(MutableContent3.size(), MutableContent.size())
200 << "Unexpected mutable content 2 size";
201
202 // Check that we can obtain a writer and reader over the content.
203 // Check that we can get a BinaryStreamReader for B.
204 auto Writer = G.getBlockContentWriter(B);
205 EXPECT_THAT_ERROR(Writer.writeInteger((uint32_t)0xcafef00d), Succeeded());
206
207 auto Reader = G.getBlockContentReader(B);
208 uint32_t Initial32Bits = 0;
209 EXPECT_THAT_ERROR(Reader.readInteger(Initial32Bits), Succeeded());
210 EXPECT_EQ(Initial32Bits, (uint32_t)0xcafef00d);
211
212 // Set content back to immutable and check that everything behaves as
213 // expected again.
214 B.setContent(BlockContent);
215 EXPECT_FALSE(B.isContentMutable()) << "Content unexpectedly mutable";
216 EXPECT_EQ(B.getContent().data(), BlockContent.data())
217 << "Unexpected block content data pointer";
218 EXPECT_EQ(B.getContent().size(), BlockContent.size())
219 << "Unexpected block content size";
220
221 // Create an initially mutable block.
222 auto &B2 = G.createMutableContentBlock(Sec, MutableContent,
223 orc::ExecutorAddr(0x10000), 8, 0);
224
225 EXPECT_TRUE(B2.isContentMutable()) << "Expected B2 content to be mutable";
226 }
227
228 TEST(LinkGraphTest, MakeExternal) {
229 // Check that we can make defined and absolute symbols external.
230 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
231 getGenericEdgeKindName);
232 auto &Sec =
233 G.createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
234
235 // Create an initial block.
236 auto &B1 =
237 G.createContentBlock(Sec, BlockContent, orc::ExecutorAddr(0x1000), 8, 0);
238
239 // Add a symbol to the block.
240 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", 4, Linkage::Strong, Scope::Default,
241 false, false);
242
243 EXPECT_TRUE(S1.isDefined()) << "Symbol should be defined";
244 EXPECT_FALSE(S1.isExternal()) << "Symbol should not be external";
245 EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute";
246 EXPECT_TRUE(&S1.getBlock()) << "Symbol should have a non-null block";
247 EXPECT_EQ(S1.getAddress(), orc::ExecutorAddr(0x1000))
248 << "Unexpected symbol address";
249
250 EXPECT_EQ(
251 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 1U)
252 << "Unexpected number of defined symbols";
253 EXPECT_EQ(
254 std::distance(G.external_symbols().begin(), G.external_symbols().end()),
255 0U)
256 << "Unexpected number of external symbols";
257
258 // Add an absolute symbol.
259 auto &S2 = G.addAbsoluteSymbol("S2", orc::ExecutorAddr(0x2000), 0,
260 Linkage::Strong, Scope::Default, true);
261
262 EXPECT_TRUE(S2.isAbsolute()) << "Symbol should be absolute";
263 EXPECT_EQ(
264 std::distance(G.absolute_symbols().begin(), G.absolute_symbols().end()),
265 1U)
266 << "Unexpected number of symbols";
267
268 // Make S1 and S2 external, confirm that the its flags are updated and that it
269 // is moved from the defined/absolute symbols lists to the externals list.
270 G.makeExternal(S1);
271 G.makeExternal(S2);
272
273 EXPECT_FALSE(S1.isDefined()) << "Symbol should not be defined";
274 EXPECT_TRUE(S1.isExternal()) << "Symbol should be external";
275 EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute";
276 EXPECT_FALSE(S2.isDefined()) << "Symbol should not be defined";
277 EXPECT_TRUE(S2.isExternal()) << "Symbol should be external";
278 EXPECT_FALSE(S2.isAbsolute()) << "Symbol should not be absolute";
279
280 EXPECT_EQ(S1.getAddress(), orc::ExecutorAddr())
281 << "Unexpected symbol address";
282 EXPECT_EQ(S2.getAddress(), orc::ExecutorAddr())
283 << "Unexpected symbol address";
284
285 EXPECT_EQ(
286 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 0U)
287 << "Unexpected number of defined symbols";
288 EXPECT_EQ(
289 std::distance(G.external_symbols().begin(), G.external_symbols().end()),
290 2U)
291 << "Unexpected number of external symbols";
292 EXPECT_EQ(
293 std::distance(G.absolute_symbols().begin(), G.absolute_symbols().end()),
294 0U)
295 << "Unexpected number of external symbols";
296 }
297
298 TEST(LinkGraphTest, MakeAbsolute) {
299 // Check that we can make defined and external symbols absolute.
300 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
301 getGenericEdgeKindName);
302 auto &Sec =
303 G.createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
304
305 // Create an initial block.
306 auto &B1 =
307 G.createContentBlock(Sec, BlockContent, orc::ExecutorAddr(0x1000), 8, 0);
308
309 // Add a symbol to the block.
310 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", 4, Linkage::Strong, Scope::Default,
311 false, false);
312
313 EXPECT_TRUE(S1.isDefined()) << "Symbol should be defined";
314 EXPECT_FALSE(S1.isExternal()) << "Symbol should not be external";
315 EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute";
316 EXPECT_TRUE(&S1.getBlock()) << "Symbol should have a non-null block";
317 EXPECT_EQ(S1.getAddress(), orc::ExecutorAddr(0x1000))
318 << "Unexpected symbol address";
319
320 EXPECT_EQ(
321 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 1U)
322 << "Unexpected number of defined symbols";
323 EXPECT_EQ(
324 std::distance(G.external_symbols().begin(), G.external_symbols().end()),
325 0U)
326 << "Unexpected number of external symbols";
327
328 // Add an external symbol.
329 auto &S2 = G.addExternalSymbol("S2", 0, true);
330
331 EXPECT_TRUE(S2.isExternal()) << "Symbol should be external";
332 EXPECT_EQ(
333 std::distance(G.external_symbols().begin(), G.external_symbols().end()),
334 1U)
335 << "Unexpected number of symbols";
336
337 // Make S1 and S2 absolute, confirm that the its flags are updated and that it
338 // is moved from the defined/external symbols lists to the absolutes list.
339 orc::ExecutorAddr S1AbsAddr(0xA000);
340 orc::ExecutorAddr S2AbsAddr(0xB000);
341 G.makeAbsolute(S1, S1AbsAddr);
342 G.makeAbsolute(S2, S2AbsAddr);
343
344 EXPECT_FALSE(S1.isDefined()) << "Symbol should not be defined";
345 EXPECT_FALSE(S1.isExternal()) << "Symbol should not be external";
346 EXPECT_TRUE(S1.isAbsolute()) << "Symbol should be absolute";
347 EXPECT_FALSE(S2.isDefined()) << "Symbol should not be defined";
348 EXPECT_FALSE(S2.isExternal()) << "Symbol should not be absolute";
349 EXPECT_TRUE(S2.isAbsolute()) << "Symbol should be absolute";
350
351 EXPECT_EQ(S1.getAddress(), S1AbsAddr) << "Unexpected symbol address";
352 EXPECT_EQ(S2.getAddress(), S2AbsAddr) << "Unexpected symbol address";
353
354 EXPECT_EQ(
355 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 0U)
356 << "Unexpected number of defined symbols";
357 EXPECT_EQ(
358 std::distance(G.external_symbols().begin(), G.external_symbols().end()),
359 0U)
360 << "Unexpected number of external symbols";
361 EXPECT_EQ(
362 std::distance(G.absolute_symbols().begin(), G.absolute_symbols().end()),
363 2U)
364 << "Unexpected number of external symbols";
365 }
366
367 TEST(LinkGraphTest, MakeDefined) {
368 // Check that we can make an external symbol defined.
369 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
370 getGenericEdgeKindName);
371 auto &Sec =
372 G.createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
373
374 // Create an initial block.
375 orc::ExecutorAddr B1Addr(0x1000);
376 auto &B1 = G.createContentBlock(Sec, BlockContent, B1Addr, 8, 0);
377
378 // Add an external symbol.
379 auto &S1 = G.addExternalSymbol("S1", 4, true);
380
381 EXPECT_FALSE(S1.isDefined()) << "Symbol should not be defined";
382 EXPECT_TRUE(S1.isExternal()) << "Symbol should be external";
383 EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute";
384 EXPECT_EQ(S1.getAddress(), orc::ExecutorAddr())
385 << "Unexpected symbol address";
386
387 EXPECT_EQ(
388 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 0U)
389 << "Unexpected number of defined symbols";
390 EXPECT_EQ(
391 std::distance(G.external_symbols().begin(), G.external_symbols().end()),
392 1U)
393 << "Unexpected number of external symbols";
394
395 // Make S1 defined, confirm that its flags are updated and that it is
396 // moved from the defined symbols to the externals list.
397 G.makeDefined(S1, B1, 0, 4, Linkage::Strong, Scope::Default, false);
398
399 EXPECT_TRUE(S1.isDefined()) << "Symbol should be defined";
400 EXPECT_FALSE(S1.isExternal()) << "Symbol should not be external";
401 EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute";
402 EXPECT_TRUE(&S1.getBlock()) << "Symbol should have a non-null block";
403 EXPECT_EQ(S1.getAddress(), orc::ExecutorAddr(0x1000U))
404 << "Unexpected symbol address";
405
406 EXPECT_EQ(
407 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 1U)
408 << "Unexpected number of defined symbols";
409 EXPECT_EQ(
410 std::distance(G.external_symbols().begin(), G.external_symbols().end()),
411 0U)
412 << "Unexpected number of external symbols";
413 }
414
415 TEST(LinkGraphTest, TransferDefinedSymbol) {
416 // Check that we can transfer a defined symbol from one block to another.
417 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
418 getGenericEdgeKindName);
419 auto &Sec =
420 G.createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
421
422 // Create initial blocks.
423 orc::ExecutorAddr B1Addr(0x1000);
424 auto &B1 = G.createContentBlock(Sec, BlockContent, B1Addr, 8, 0);
425 orc::ExecutorAddr B2Addr(0x2000);
426 auto &B2 = G.createContentBlock(Sec, BlockContent, B2Addr, 8, 0);
427 orc::ExecutorAddr B3Addr(0x3000);
428 auto &B3 = G.createContentBlock(Sec, BlockContent.slice(0, 32), B3Addr, 8, 0);
429
430 // Add a symbol.
431 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", B1.getSize(), Linkage::Strong,
432 Scope::Default, false, false);
433
434 // Transfer with zero offset, explicit size.
435 G.transferDefinedSymbol(S1, B2, 0, 64);
436
437 EXPECT_EQ(&S1.getBlock(), &B2) << "Block was not updated";
438 EXPECT_EQ(S1.getOffset(), 0U) << "Unexpected offset";
439 EXPECT_EQ(S1.getSize(), 64U) << "Size was not updated";
440
441 // Transfer with non-zero offset, implicit truncation.
442 G.transferDefinedSymbol(S1, B3, 16, std::nullopt);
443
444 EXPECT_EQ(&S1.getBlock(), &B3) << "Block was not updated";
445 EXPECT_EQ(S1.getOffset(), 16U) << "Offset was not updated";
446 EXPECT_EQ(S1.getSize(), 16U) << "Size was not updated";
447 }
448
449 TEST(LinkGraphTest, TransferDefinedSymbolAcrossSections) {
450 // Check that we can transfer a defined symbol from an existing block in one
451 // section to another.
452 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
453 getGenericEdgeKindName);
454 auto &Sec1 =
455 G.createSection("__data.1", orc::MemProt::Read | orc::MemProt::Write);
456 auto &Sec2 =
457 G.createSection("__data.2", orc::MemProt::Read | orc::MemProt::Write);
458
459 // Create blocks in each section.
460 orc::ExecutorAddr B1Addr(0x1000);
461 auto &B1 = G.createContentBlock(Sec1, BlockContent, B1Addr, 8, 0);
462 orc::ExecutorAddr B2Addr(0x2000);
463 auto &B2 = G.createContentBlock(Sec2, BlockContent, B2Addr, 8, 0);
464
465 // Add a symbol to section 1.
466 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", B1.getSize(), Linkage::Strong,
467 Scope::Default, false, false);
468
469 // Transfer with zero offset, explicit size to section 2.
470 G.transferDefinedSymbol(S1, B2, 0, 64);
471
472 EXPECT_EQ(&S1.getBlock(), &B2) << "Block was not updated";
473 EXPECT_EQ(S1.getOffset(), 0U) << "Unexpected offset";
474 EXPECT_EQ(S1.getSize(), 64U) << "Size was not updated";
475
476 EXPECT_EQ(Sec1.symbols_size(), 0u) << "Symbol was not removed from Sec1";
477 EXPECT_EQ(Sec2.symbols_size(), 1u) << "Symbol was not added to Sec2";
478 if (Sec2.symbols_size() == 1) {
479 EXPECT_EQ(*Sec2.symbols().begin(), &S1) << "Unexpected symbol";
480 }
481 }
482
483 TEST(LinkGraphTest, TransferBlock) {
484 // Check that we can transfer a block (and all associated symbols) from one
485 // section to another.
486 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
487 getGenericEdgeKindName);
488 auto &Sec1 =
489 G.createSection("__data.1", orc::MemProt::Read | orc::MemProt::Write);
490 auto &Sec2 =
491 G.createSection("__data.2", orc::MemProt::Read | orc::MemProt::Write);
492
493 // Create an initial block.
494 orc::ExecutorAddr B1Addr(0x1000);
495 auto &B1 = G.createContentBlock(Sec1, BlockContent, B1Addr, 8, 0);
496 orc::ExecutorAddr B2Addr(0x2000);
497 auto &B2 = G.createContentBlock(Sec1, BlockContent, B2Addr, 8, 0);
498
499 // Add some symbols on B1...
500 G.addDefinedSymbol(B1, 0, "S1", B1.getSize(), Linkage::Strong, Scope::Default,
501 false, false);
502 G.addDefinedSymbol(B1, 1, "S2", B1.getSize() - 1, Linkage::Strong,
503 Scope::Default, false, false);
504
505 // ... and on B2.
506 G.addDefinedSymbol(B2, 0, "S3", B2.getSize(), Linkage::Strong, Scope::Default,
507 false, false);
508 G.addDefinedSymbol(B2, 1, "S4", B2.getSize() - 1, Linkage::Strong,
509 Scope::Default, false, false);
510
511 EXPECT_EQ(Sec1.blocks_size(), 2U) << "Expected two blocks in Sec1 initially";
512 EXPECT_EQ(Sec1.symbols_size(), 4U)
513 << "Expected four symbols in Sec1 initially";
514 EXPECT_EQ(Sec2.blocks_size(), 0U) << "Expected zero blocks in Sec2 initially";
515 EXPECT_EQ(Sec2.symbols_size(), 0U)
516 << "Expected zero symbols in Sec2 initially";
517
518 // Transfer with zero offset, explicit size.
519 G.transferBlock(B1, Sec2);
520
521 EXPECT_EQ(Sec1.blocks_size(), 1U)
522 << "Expected one blocks in Sec1 after transfer";
523 EXPECT_EQ(Sec1.symbols_size(), 2U)
524 << "Expected two symbols in Sec1 after transfer";
525 EXPECT_EQ(Sec2.blocks_size(), 1U)
526 << "Expected one blocks in Sec2 after transfer";
527 EXPECT_EQ(Sec2.symbols_size(), 2U)
528 << "Expected two symbols in Sec2 after transfer";
529 }
530
531 TEST(LinkGraphTest, MergeSections) {
532 // Check that we can transfer a block (and all associated symbols) from one
533 // section to another.
534 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
535 getGenericEdgeKindName);
536 auto &Sec1 =
537 G.createSection("__data.1", orc::MemProt::Read | orc::MemProt::Write);
538 auto &Sec2 =
539 G.createSection("__data.2", orc::MemProt::Read | orc::MemProt::Write);
540 auto &Sec3 =
541 G.createSection("__data.3", orc::MemProt::Read | orc::MemProt::Write);
542
543 // Create an initial block.
544 orc::ExecutorAddr B1Addr(0x1000);
545 auto &B1 = G.createContentBlock(Sec1, BlockContent, B1Addr, 8, 0);
546 orc::ExecutorAddr B2Addr(0x2000);
547 auto &B2 = G.createContentBlock(Sec2, BlockContent, B2Addr, 8, 0);
548 orc::ExecutorAddr B3Addr(0x3000);
549 auto &B3 = G.createContentBlock(Sec3, BlockContent, B3Addr, 8, 0);
550
551 // Add a symbols for each block.
552 G.addDefinedSymbol(B1, 0, "S1", B1.getSize(), Linkage::Strong, Scope::Default,
553 false, false);
554 G.addDefinedSymbol(B2, 0, "S2", B2.getSize(), Linkage::Strong, Scope::Default,
555 false, false);
556 G.addDefinedSymbol(B3, 0, "S3", B2.getSize(), Linkage::Strong, Scope::Default,
557 false, false);
558
559 EXPECT_EQ(&B1.getSection(), &Sec1);
560 EXPECT_EQ(&B2.getSection(), &Sec2);
561 EXPECT_EQ(G.sections_size(), 3U) << "Expected three sections initially";
562 EXPECT_EQ(Sec1.blocks_size(), 1U) << "Expected one block in Sec1 initially";
563 EXPECT_EQ(Sec1.symbols_size(), 1U) << "Expected one symbol in Sec1 initially";
564 EXPECT_EQ(Sec2.blocks_size(), 1U) << "Expected one block in Sec2 initially";
565 EXPECT_EQ(Sec2.symbols_size(), 1U) << "Expected one symbol in Sec2 initially";
566 EXPECT_EQ(Sec3.blocks_size(), 1U) << "Expected one block in Sec3 initially";
567 EXPECT_EQ(Sec3.symbols_size(), 1U) << "Expected one symbol in Sec3 initially";
568
569 // Check that self-merge is a no-op.
570 G.mergeSections(Sec1, Sec1);
571
572 EXPECT_EQ(&B1.getSection(), &Sec1)
573 << "Expected B1.getSection() to remain unchanged";
574 EXPECT_EQ(G.sections_size(), 3U)
575 << "Expected three sections after first merge";
576 EXPECT_EQ(Sec1.blocks_size(), 1U)
577 << "Expected one block in Sec1 after first merge";
578 EXPECT_EQ(Sec1.symbols_size(), 1U)
579 << "Expected one symbol in Sec1 after first merge";
580 EXPECT_EQ(Sec2.blocks_size(), 1U)
581 << "Expected one block in Sec2 after first merge";
582 EXPECT_EQ(Sec2.symbols_size(), 1U)
583 << "Expected one symbol in Sec2 after first merge";
584 EXPECT_EQ(Sec3.blocks_size(), 1U)
585 << "Expected one block in Sec3 after first merge";
586 EXPECT_EQ(Sec3.symbols_size(), 1U)
587 << "Expected one symbol in Sec3 after first merge";
588
589 // Merge Sec2 into Sec1, removing Sec2.
590 G.mergeSections(Sec1, Sec2);
591
592 EXPECT_EQ(&B2.getSection(), &Sec1)
593 << "Expected B2.getSection() to have been changed to &Sec1";
594 EXPECT_EQ(G.sections_size(), 2U)
595 << "Expected two sections after section merge";
596 EXPECT_EQ(Sec1.blocks_size(), 2U)
597 << "Expected two blocks in Sec1 after section merge";
598 EXPECT_EQ(Sec1.symbols_size(), 2U)
599 << "Expected two symbols in Sec1 after section merge";
600 EXPECT_EQ(Sec3.blocks_size(), 1U)
601 << "Expected one block in Sec3 after section merge";
602 EXPECT_EQ(Sec3.symbols_size(), 1U)
603 << "Expected one symbol in Sec3 after section merge";
604
605 G.mergeSections(Sec1, Sec3, true);
606
607 EXPECT_EQ(G.sections_size(), 2U) << "Expected two sections after third merge";
608 EXPECT_EQ(Sec1.blocks_size(), 3U)
609 << "Expected three blocks in Sec1 after third merge";
610 EXPECT_EQ(Sec1.symbols_size(), 3U)
611 << "Expected three symbols in Sec1 after third merge";
612 EXPECT_EQ(Sec3.blocks_size(), 0U)
613 << "Expected one block in Sec3 after third merge";
614 EXPECT_EQ(Sec3.symbols_size(), 0U)
615 << "Expected one symbol in Sec3 after third merge";
616 }
617
618 TEST(LinkGraphTest, SplitBlock) {
619 // Check that the LinkGraph::splitBlock test works as expected.
620 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little,
621 getGenericEdgeKindName);
622 auto &Sec =
623 G.createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
624
625 // Create the block to split.
626 orc::ExecutorAddr B1Addr(0x1000);
627 auto &B1 = G.createContentBlock(Sec, BlockContent, B1Addr, 8, 0);
628
629 // Add some symbols to the block.
630 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", 4, Linkage::Strong, Scope::Default,
631 false, false);
632 auto &S2 = G.addDefinedSymbol(B1, 4, "S2", 4, Linkage::Strong, Scope::Default,
633 false, false);
634 auto &S3 = G.addDefinedSymbol(B1, 8, "S3", 4, Linkage::Strong, Scope::Default,
635 false, false);
636 auto &S4 = G.addDefinedSymbol(B1, 12, "S4", 4, Linkage::Strong,
637 Scope::Default, false, false);
638 // Add a symbol that extends beyond the split.
639 auto &S5 = G.addDefinedSymbol(B1, 0, "S5", 16, Linkage::Strong,
640 Scope::Default, false, false);
641
642 // Add an extra block, EB, and target symbols, and use these to add edges
643 // from B1 to EB.
644 orc::ExecutorAddr EBAddr(0x2000);
645 auto &EB = G.createContentBlock(Sec, BlockContent, EBAddr, 8, 0);
646 auto &ES1 = G.addDefinedSymbol(EB, 0, "TS1", 4, Linkage::Strong,
647 Scope::Default, false, false);
648 auto &ES2 = G.addDefinedSymbol(EB, 4, "TS2", 4, Linkage::Strong,
649 Scope::Default, false, false);
650 auto &ES3 = G.addDefinedSymbol(EB, 8, "TS3", 4, Linkage::Strong,
651 Scope::Default, false, false);
652 auto &ES4 = G.addDefinedSymbol(EB, 12, "TS4", 4, Linkage::Strong,
653 Scope::Default, false, false);
654
655 // Add edges from B1 to EB.
656 B1.addEdge(Edge::FirstRelocation, 0, ES1, 0);
657 B1.addEdge(Edge::FirstRelocation, 4, ES2, 0);
658 B1.addEdge(Edge::FirstRelocation, 8, ES3, 0);
659 B1.addEdge(Edge::FirstRelocation, 12, ES4, 0);
660
661 // Split B1.
662 auto &B2 = G.splitBlock(B1, 8);
663
664 // Check that the block addresses and content matches what we would expect.
665 EXPECT_EQ(B1.getAddress(), B1Addr + 8);
666 EXPECT_EQ(B1.getContent(), BlockContent.slice(8));
667
668 EXPECT_EQ(B2.getAddress(), B1Addr);
669 EXPECT_EQ(B2.getContent(), BlockContent.slice(0, 8));
670
671 // Check that symbols in B1 were transferred as expected:
672 // We expect S1 and S2 to have been transferred to B2, and S3 and S4 to have
673 // remained attached to B1. Symbols S3 and S4 should have had their offsets
674 // slid to account for the change in address of B2.
675 EXPECT_EQ(&S1.getBlock(), &B2);
676 EXPECT_EQ(S1.getOffset(), 0U);
677
678 EXPECT_EQ(&S2.getBlock(), &B2);
679 EXPECT_EQ(S2.getOffset(), 4U);
680
681 EXPECT_EQ(&S3.getBlock(), &B1);
682 EXPECT_EQ(S3.getOffset(), 0U);
683
684 EXPECT_EQ(&S4.getBlock(), &B1);
685 EXPECT_EQ(S4.getOffset(), 4U);
686
687 EXPECT_EQ(&S5.getBlock(), &B2);
688 EXPECT_EQ(S5.getOffset(), 0U);
689 // Size shrinks to fit.
690 EXPECT_EQ(S5.getSize(), 8U);
691
692 // Check that edges in B1 have been transferred as expected:
693 // Both blocks should now have two edges each at offsets 0 and 4.
694 EXPECT_EQ(llvm::size(B1.edges()), 2);
695 if (size(B1.edges()) == 2) {
696 auto *E1 = &*B1.edges().begin();
697 auto *E2 = &*(B1.edges().begin() + 1);
698 if (E2->getOffset() < E1->getOffset())
699 std::swap(E1, E2);
700 EXPECT_EQ(E1->getOffset(), 0U);
701 EXPECT_EQ(E2->getOffset(), 4U);
702 }
703
704 EXPECT_EQ(llvm::size(B2.edges()), 2);
705 if (size(B2.edges()) == 2) {
706 auto *E1 = &*B2.edges().begin();
707 auto *E2 = &*(B2.edges().begin() + 1);
708 if (E2->getOffset() < E1->getOffset())
709 std::swap(E1, E2);
710 EXPECT_EQ(E1->getOffset(), 0U);
711 EXPECT_EQ(E2->getOffset(), 4U);
712 }
713 }
714
715 struct InitSymbolsTestParams {
716 InitSymbolsTestParams(StringRef Triple, StringRef Section,
717 bool ExpectedHasInitializerSection)
718 : Triple(Triple), Section(Section),
719 ExpectedHasInitializerSection(ExpectedHasInitializerSection) {}
720
721 StringRef Triple;
722 StringRef Section;
723 bool ExpectedHasInitializerSection;
724 };
725
726 class InitSymbolsTestFixture
727 : public ::testing::TestWithParam<InitSymbolsTestParams> {};
728
729 TEST_P(InitSymbolsTestFixture, InitSymbolSections) {
730 InitSymbolsTestParams Params = GetParam();
731 auto Graph = std::make_unique<LinkGraph>(
732 "foo", Triple(Params.Triple), 8, support::little, getGenericEdgeKindName);
733 Graph->createSection(Params.Section,
734 orc::MemProt::Read | orc::MemProt::Write);
735 EXPECT_EQ(orc::hasInitializerSection(*Graph),
736 Params.ExpectedHasInitializerSection);
737 }
738
739 INSTANTIATE_TEST_SUITE_P(
740 InitSymbolsTests, InitSymbolsTestFixture,
741 ::testing::Values(
742 InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__objc_selrefs",
743 true),
744 InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__mod_init_func",
745 true),
746 InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__objc_classlist",
747 true),
748 InitSymbolsTestParams("x86_64-apple-darwin", "__TEXT,__swift5_proto",
749 true),
750 InitSymbolsTestParams("x86_64-apple-darwin", "__TEXT,__swift5_protos",
751 true),
752 InitSymbolsTestParams("x86_64-apple-darwin", "__TEXT,__swift5_types",
753 true),
754 InitSymbolsTestParams("x86_64-apple-darwin", "__DATA,__not_an_init_sec",
755 false),
756 InitSymbolsTestParams("x86_64-unknown-linux", ".init_array", true),
757 InitSymbolsTestParams("x86_64-unknown-linux", ".init_array.0", true),
758 InitSymbolsTestParams("x86_64-unknown-linux", ".text", false)));