GCC Code Coverage Report


Directory: ../
File: src/linker/ExternalLinkerInterface.cpp
Date: 2025-12-12 07:25:08
Coverage Exec Excl Total
Lines: 67.8% 40 15 74
Functions: 100.0% 6 0 6
Branches: 35.3% 41 60 176

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