GCC Code Coverage Report


Directory: ../
File: src/linker/ExternalLinkerInterface.cpp
Date: 2025-11-06 00:20:37
Coverage Exec Excl Total
Lines: 70.0% 35 12 62
Functions: 100.0% 5 0 5
Branches: 34.6% 37 56 163

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "ExternalLinkerInterface.h"
4 #include "util/Timer.h"
5
6 #include <iostream>
7
8 #include <exception/CompilerError.h>
9 #include <exception/LinkerError.h>
10 #include <util/FileUtil.h>
11
12 namespace spice::compiler {
13
14 197 void ExternalLinkerInterface::prepare() {
15 // Set target to linker
16
2/4
✓ Branch 3 → 4 taken 197 times.
✗ Branch 3 → 86 not taken.
✓ Branch 4 → 5 taken 197 times.
✗ Branch 4 → 84 not taken.
197 addLinkerFlag("--target=" + cliOptions.targetTriple.str());
17
18 // Static linking
19
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 14 taken 197 times.
197 if (cliOptions.staticLinking)
20 addLinkerFlag("-static");
21
22 // Stripping symbols
23
2/2
✓ Branch 14 → 15 taken 195 times.
✓ Branch 14 → 22 taken 2 times.
197 if (!cliOptions.instrumentation.generateDebugInfo)
24
2/4
✓ Branch 17 → 18 taken 195 times.
✗ Branch 17 → 95 not taken.
✓ Branch 18 → 19 taken 195 times.
✗ Branch 18 → 93 not taken.
390 addLinkerFlag("-Wl,-s");
25
26 // Sanitizers
27
1/5
✓ Branch 22 → 23 taken 197 times.
✗ Branch 22 → 24 not taken.
✗ Branch 22 → 31 not taken.
✗ Branch 22 → 38 not taken.
✗ Branch 22 → 57 not taken.
197 switch (cliOptions.instrumentation.sanitizer) {
28 197 case Sanitizer::NONE:
29 197 break;
30 case Sanitizer::ADDRESS:
31 addLinkerFlag("-lasan");
32 break;
33 case Sanitizer::THREAD:
34 addLinkerFlag("-ltsan");
35 break;
36 case Sanitizer::MEMORY:
37 addLinkerFlag("-L/usr/local/lib/clang/21/lib/x86_64-unknown-linux-gnu");
38 addLinkerFlag("-lclang_rt.msan");
39 addLinkerFlag("-lm");
40 break;
41 }
42
43 // Web Assembly
44
3/6
✓ Branch 58 → 59 taken 197 times.
✗ Branch 58 → 61 not taken.
✗ Branch 60 → 61 not taken.
✓ Branch 60 → 62 taken 197 times.
✗ Branch 63 → 64 not taken.
✓ Branch 63 → 83 taken 197 times.
197 if (cliOptions.targetArch == TARGET_WASM32 || cliOptions.targetArch == TARGET_WASM64) {
45 addLinkerFlag("-nostdlib");
46 addLinkerFlag("-Wl,--no-entry");
47 addLinkerFlag("-Wl,--export-all");
48 }
49 197 }
50
51 /**
52 * Start the linking process
53 */
54 197 void ExternalLinkerInterface::link() const {
55
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 197 times.
197 assert(!outputPath.empty());
56
57 // Build the linker command
58
1/2
✓ Branch 5 → 6 taken 197 times.
✗ Branch 5 → 104 not taken.
197 std::stringstream linkerCommandBuilder;
59
1/2
✓ Branch 6 → 7 taken 197 times.
✗ Branch 6 → 102 not taken.
197 const auto [linkerInvokerName, linkerInvokerPath] = FileUtil::findLinkerInvoker();
60
1/2
✓ Branch 7 → 8 taken 197 times.
✗ Branch 7 → 100 not taken.
197 linkerCommandBuilder << linkerInvokerPath;
61
1/2
✓ Branch 8 → 9 taken 197 times.
✗ Branch 8 → 100 not taken.
197 const auto [linkerName, linkerPath] = FileUtil::findLinker(cliOptions);
62
2/4
✓ Branch 9 → 10 taken 197 times.
✗ Branch 9 → 98 not taken.
✓ Branch 10 → 11 taken 197 times.
✗ Branch 10 → 98 not taken.
197 linkerCommandBuilder << " -fuse-ld=" << linkerPath;
63 // Append linker flags
64
2/2
✓ Branch 18 → 13 taken 820 times.
✓ Branch 18 → 19 taken 197 times.
1017 for (const std::string &linkerFlag : linkerFlags)
65
2/4
✓ Branch 14 → 15 taken 820 times.
✗ Branch 14 → 77 not taken.
✓ Branch 15 → 16 taken 820 times.
✗ Branch 15 → 77 not taken.
820 linkerCommandBuilder << " " << linkerFlag;
66 // Append output path
67
3/6
✓ Branch 19 → 20 taken 197 times.
✗ Branch 19 → 98 not taken.
✓ Branch 20 → 21 taken 197 times.
✗ Branch 20 → 80 not taken.
✓ Branch 21 → 22 taken 197 times.
✗ Branch 21 → 78 not taken.
197 linkerCommandBuilder << " -o " << outputPath.string();
68 // Append object files
69
2/2
✓ Branch 30 → 25 taken 821 times.
✓ Branch 30 → 31 taken 197 times.
1018 for (const std::string &objectFilePath : objectFilePaths)
70
2/4
✓ Branch 26 → 27 taken 821 times.
✗ Branch 26 → 81 not taken.
✓ Branch 27 → 28 taken 821 times.
✗ Branch 27 → 81 not taken.
821 linkerCommandBuilder << " " << objectFilePath;
71
72 // Print status message
73
1/2
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 43 taken 197 times.
197 if (cliOptions.printDebugOutput) {
74 std::cout << "\nLinking with: " << linkerInvokerName << " (invoker) / " << linkerName << " (linker)"; // GCOV_EXCL_LINE
75 std::cout << "\nEmitting executable to path: " << outputPath.string() << "\n"; // GCOV_EXCL_LINE
76 }
77
78 // Call the linker
79
1/2
✓ Branch 43 → 44 taken 197 times.
✗ Branch 43 → 98 not taken.
197 Timer timer;
80
1/2
✓ Branch 44 → 45 taken 197 times.
✗ Branch 44 → 98 not taken.
197 timer.start();
81
1/2
✓ Branch 45 → 46 taken 197 times.
✗ Branch 45 → 98 not taken.
197 const std::string linkerCommand = linkerCommandBuilder.str();
82
1/2
✓ Branch 46 → 47 taken 197 times.
✗ Branch 46 → 96 not taken.
197 const auto [output, exitCode] = FileUtil::exec(linkerCommand);
83
1/2
✓ Branch 47 → 48 taken 197 times.
✗ Branch 47 → 94 not taken.
197 timer.stop();
84
85 // Check for linker error
86 if (exitCode != 0) // GCOV_EXCL_LINE
87 throw LinkerError(LINKER_ERROR, "Linker exited with non-zero exit code"); // GCOV_EXCL_LINE
88
89 // Print linker result if appropriate
90 if (cliOptions.printDebugOutput && !output.empty()) // GCOV_EXCL_LINE
91 std::cout << "Linking result: " << output << "\n\n"; // GCOV_EXCL_LINE
92
93 // Print link time
94 if (cliOptions.printDebugOutput) // GCOV_EXCL_LINE
95 std::cout << "Total link time: " << timer.getDurationMilliseconds() << " ms\n\n"; // GCOV_EXCL_LINE
96 197 }
97
98 /**
99 * Add another object file to be linked when calling 'link()'
100 *
101 * @param objectFilePath Path to the object file
102 */
103 864 void ExternalLinkerInterface::addObjectFilePath(const std::string &objectFilePath) { objectFilePaths.push_back(objectFilePath); }
104
105 /**
106 * Add another linker flag for the call to the linker executable
107 *
108 * @param flag Linker flag
109 */
110 823 void ExternalLinkerInterface::addLinkerFlag(const std::string &flag) { linkerFlags.push_back(flag); }
111
112 /**
113 * Add another source file to compile and link in (C or C++)
114 *
115 * @param additionalSource Additional source file
116 */
117 2 void ExternalLinkerInterface::addAdditionalSourcePath(std::filesystem::path additionalSource) {
118 // Check if the file exists
119 if (!exists(additionalSource)) { // GCOV_EXCL_LINE
120 const std::string msg = "The additional source file '" + additionalSource.string() + "' does not exist"; // GCOV_EXCL_LINE
121 throw CompilerError(IO_ERROR, msg); // GCOV_EXCL_LINE
122 } // GCOV_EXCL_LINE
123
124 // Add the file to the linker
125 2 additionalSource.make_preferred();
126
2/4
✓ Branch 13 → 14 taken 2 times.
✗ Branch 13 → 31 not taken.
✓ Branch 14 → 15 taken 2 times.
✗ Branch 14 → 29 not taken.
2 addObjectFilePath(additionalSource.string());
127 2 }
128
129 } // namespace spice::compiler
130