src/linker/ExternalLinkerInterface.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "ExternalLinkerInterface.h" | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <iostream> | ||
| 7 | |||
| 8 | #include <driver/Driver.h> | ||
| 9 | #include <exception/CompilerError.h> | ||
| 10 | #include <exception/LinkerError.h> | ||
| 11 | #include <util/GlobalDefinitions.h> | ||
| 12 | #include <util/SystemUtil.h> | ||
| 13 | #include <util/Timer.h> | ||
| 14 | |||
| 15 | namespace spice::compiler { | ||
| 16 | |||
| 17 | 580 | ExternalLinkerInterface::ExternalLinkerInterface(const CliOptions &cliOptions) | |
| 18 | 580 | : outputPath(cliOptions.outputPath), cliOptions(cliOptions) {} | |
| 19 | |||
| 20 | 333 | void ExternalLinkerInterface::prepare() { | |
| 21 | // Static linking | ||
| 22 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 10 taken 333 times.
|
333 | if (cliOptions.outputContainer == OutputContainer::SHARED_LIBRARY) { |
| 23 | ✗ | addLinkerFlag("-shared"); | |
| 24 |
1/2✗ Branch 10 → 11 not taken.
✓ Branch 10 → 18 taken 333 times.
|
333 | } else if (cliOptions.staticLinking) { |
| 25 | ✗ | addLinkerFlag("-static"); | |
| 26 | } | ||
| 27 | |||
| 28 | // The following flags only make sense if we want to emit an executable | ||
| 29 |
1/2✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 333 times.
|
333 | if (cliOptions.outputContainer != OutputContainer::EXECUTABLE) |
| 30 | ✗ | return; | |
| 31 | |||
| 32 | // Stripping symbols | ||
| 33 |
5/6✓ Branch 20 → 21 taken 329 times.
✓ Branch 20 → 24 taken 4 times.
✓ Branch 22 → 23 taken 329 times.
✗ Branch 22 → 24 not taken.
✓ Branch 25 → 26 taken 329 times.
✓ Branch 25 → 33 taken 4 times.
|
333 | if (!cliOptions.instrumentation.generateDebugInfo && !cliOptions.targetTriple.isOSDarwin()) |
| 34 |
2/4✓ Branch 28 → 29 taken 329 times.
✗ Branch 28 → 100 not taken.
✓ Branch 29 → 30 taken 329 times.
✗ Branch 29 → 98 not taken.
|
658 | addLinkerFlag("-Wl,-s"); |
| 35 | |||
| 36 | // Sanitizers | ||
| 37 |
3/6✓ Branch 33 → 34 taken 325 times.
✓ Branch 33 → 35 taken 6 times.
✓ Branch 33 → 42 taken 2 times.
✗ Branch 33 → 49 not taken.
✗ Branch 33 → 57 not taken.
✗ Branch 33 → 64 not taken.
|
333 | switch (cliOptions.instrumentation.sanitizer) { |
| 38 | 325 | case Sanitizer::NONE: | |
| 39 | 325 | break; | |
| 40 | 6 | case Sanitizer::ADDRESS: | |
| 41 |
2/4✓ Branch 37 → 38 taken 6 times.
✗ Branch 37 → 106 not taken.
✓ Branch 38 → 39 taken 6 times.
✗ Branch 38 → 104 not taken.
|
6 | addLinkerFlag("-fsanitize=address"); |
| 42 | 6 | break; | |
| 43 | 2 | case Sanitizer::THREAD: | |
| 44 |
2/4✓ Branch 44 → 45 taken 2 times.
✗ Branch 44 → 112 not taken.
✓ Branch 45 → 46 taken 2 times.
✗ Branch 45 → 110 not taken.
|
2 | addLinkerFlag("-fsanitize=thread"); |
| 45 | 2 | break; | |
| 46 | ✗ | case Sanitizer::MEMORY: | |
| 47 | ✗ | addLinkerFlag("-fsanitize=memory"); | |
| 48 | ✗ | requestLibMathLinkage(); | |
| 49 | ✗ | break; | |
| 50 | ✗ | case Sanitizer::TYPE: | |
| 51 | ✗ | addLinkerFlag("-fsanitize=type"); | |
| 52 | ✗ | break; | |
| 53 | } | ||
| 54 | |||
| 55 | // Web Assembly | ||
| 56 |
1/2✗ Branch 65 → 66 not taken.
✓ Branch 65 → 85 taken 333 times.
|
333 | if (cliOptions.targetTriple.isWasm()) { |
| 57 | ✗ | addLinkerFlag("-nostdlib"); | |
| 58 | ✗ | addLinkerFlag("-Wl,--no-entry"); | |
| 59 | ✗ | addLinkerFlag("-Wl,--export-all"); | |
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | 333 | void ExternalLinkerInterface::run() const { | |
| 64 |
1/4✓ Branch 2 → 3 taken 333 times.
✗ Branch 2 → 4 not taken.
✗ Branch 2 → 6 not taken.
✗ Branch 2 → 7 not taken.
|
333 | switch (cliOptions.outputContainer) { |
| 65 | 333 | case OutputContainer::EXECUTABLE: | |
| 66 | case OutputContainer::SHARED_LIBRARY: | ||
| 67 | 333 | link(); | |
| 68 | 333 | break; | |
| 69 | ✗ | case OutputContainer::STATIC_LIBRARY: | |
| 70 | ✗ | archive(); | |
| 71 | ✗ | break; | |
| 72 | ✗ | case OutputContainer::OBJECT_FILE: | |
| 73 | // No linking necessary | ||
| 74 | ✗ | break; | |
| 75 | ✗ | default: | |
| 76 | ✗ | assert_fail("Unknown output container"); | |
| 77 | } | ||
| 78 | 333 | } | |
| 79 | |||
| 80 | /** | ||
| 81 | * Cleanup intermediary object files | ||
| 82 | */ | ||
| 83 | 333 | void ExternalLinkerInterface::cleanup() const { | |
| 84 | // Cleanup intermediary object files | ||
| 85 |
1/2✓ Branch 2 → 3 taken 333 times.
✗ Branch 2 → 33 not taken.
|
333 | const char *objFileExt = SystemUtil::getOutputFileExtension(cliOptions, cliOptions.outputContainer); |
| 86 |
2/4✓ Branch 3 → 4 taken 333 times.
✗ Branch 3 → 27 not taken.
✓ Branch 4 → 5 taken 333 times.
✗ Branch 4 → 27 not taken.
|
333 | if (cliOptions.outputContainer != OutputContainer::OBJECT_FILE && !cliOptions.dump.dumpToFiles) |
| 87 |
2/2✓ Branch 25 → 7 taken 2139 times.
✓ Branch 25 → 26 taken 333 times.
|
2805 | for (const std::filesystem::path &path : linkedFiles) |
| 88 |
3/6✓ Branch 9 → 10 taken 2139 times.
✗ Branch 9 → 31 not taken.
✓ Branch 10 → 11 taken 2139 times.
✗ Branch 10 → 28 not taken.
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 2139 times.
|
2139 | if (path.extension() == objFileExt) |
| 89 | ✗ | std::filesystem::remove(path); | |
| 90 | 333 | } | |
| 91 | |||
| 92 | /** | ||
| 93 | * Link the object files to an executable | ||
| 94 | */ | ||
| 95 | 333 | void ExternalLinkerInterface::link() const { | |
| 96 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 333 times.
|
333 | assert(!outputPath.empty()); |
| 97 | |||
| 98 | // Build the linker command | ||
| 99 |
1/2✓ Branch 5 → 6 taken 333 times.
✗ Branch 5 → 160 not taken.
|
333 | std::stringstream commandBuilder; |
| 100 |
1/2✓ Branch 6 → 7 taken 333 times.
✗ Branch 6 → 158 not taken.
|
333 | const auto [linkerInvokerName, linkerInvokerPath] = SystemUtil::findLinkerInvoker(); |
| 101 |
1/2✓ Branch 7 → 8 taken 333 times.
✗ Branch 7 → 156 not taken.
|
333 | commandBuilder << linkerInvokerPath; |
| 102 |
1/2✓ Branch 8 → 9 taken 333 times.
✗ Branch 8 → 156 not taken.
|
333 | const auto [linkerName, linkerPath] = SystemUtil::findLinker(cliOptions); |
| 103 | 333 | const bool isGccInvoker = std::string_view(linkerInvokerName) == LINKER_INVOKER_NAME_GCC; | |
| 104 | 333 | const bool isClangInvoker = std::string_view(linkerInvokerName) == LINKER_INVOKER_NAME_CLANG; | |
| 105 | // GCC does not implement MemorySanitizer or TypeSanitizer | ||
| 106 | 333 | const Sanitizer sanitizer = cliOptions.instrumentation.sanitizer; | |
| 107 |
1/6✗ Branch 15 → 16 not taken.
✓ Branch 15 → 25 taken 333 times.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 25 not taken.
|
333 | if (!isClangInvoker && (sanitizer == Sanitizer::MEMORY || sanitizer == Sanitizer::TYPE)) { |
| 108 | ✗ | const std::string msg = "Memory and type sanitizers require clang as the linker invoker, but 'gcc' was selected"; | |
| 109 | ✗ | throw LinkerError(SANITIZER_NOT_SUPPORTED_BY_LINKER_INVOKER, msg); | |
| 110 | ✗ | } | |
| 111 | // GCC 16 dropped '-fuse-ld=ld'; skip when using GCC with the default BFD linker | ||
| 112 |
2/6✗ Branch 25 → 26 not taken.
✓ Branch 25 → 30 taken 333 times.
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 31 not taken.
✓ Branch 32 → 33 taken 333 times.
✗ Branch 32 → 35 not taken.
|
333 | if (!isGccInvoker || std::string_view(linkerName) != LINKER_NAME_LD) |
| 113 |
2/4✓ Branch 33 → 34 taken 333 times.
✗ Branch 33 → 154 not taken.
✓ Branch 34 → 35 taken 333 times.
✗ Branch 34 → 154 not taken.
|
333 | commandBuilder << " -fuse-ld=" << linkerPath; |
| 114 | // '--target=' is clang-only; GCC uses target-specific toolchain prefixes instead | ||
| 115 |
1/2✓ Branch 35 → 36 taken 333 times.
✗ Branch 35 → 39 not taken.
|
333 | if (!isGccInvoker) |
| 116 |
2/4✓ Branch 36 → 37 taken 333 times.
✗ Branch 36 → 154 not taken.
✓ Branch 38 → 39 taken 333 times.
✗ Branch 38 → 154 not taken.
|
333 | commandBuilder << " --target=" << cliOptions.targetTriple.str(); |
| 117 | // Append linker flags | ||
| 118 |
2/2✓ Branch 54 → 41 taken 1826 times.
✓ Branch 54 → 55 taken 333 times.
|
2492 | for (const std::string &linkerFlag : linkerFlags) |
| 119 |
2/4✓ Branch 43 → 44 taken 1826 times.
✗ Branch 43 → 133 not taken.
✓ Branch 44 → 45 taken 1826 times.
✗ Branch 44 → 133 not taken.
|
1826 | commandBuilder << " " << linkerFlag; |
| 120 |
2/2✓ Branch 56 → 57 taken 1 time.
✓ Branch 56 → 58 taken 332 times.
|
333 | if (linkLibMath) |
| 121 |
1/2✓ Branch 57 → 58 taken 1 time.
✗ Branch 57 → 154 not taken.
|
1 | commandBuilder << " -lm"; |
| 122 | // Append output path | ||
| 123 |
3/6✓ Branch 58 → 59 taken 333 times.
✗ Branch 58 → 154 not taken.
✓ Branch 59 → 60 taken 333 times.
✗ Branch 59 → 136 not taken.
✓ Branch 60 → 61 taken 333 times.
✗ Branch 60 → 134 not taken.
|
333 | commandBuilder << " -o " << outputPath.string(); |
| 124 | // Append object files | ||
| 125 |
2/2✓ Branch 79 → 64 taken 2139 times.
✓ Branch 79 → 80 taken 333 times.
|
2805 | for (const std::filesystem::path &objectFilePath : linkedFiles) |
| 126 |
3/6✓ Branch 66 → 67 taken 2139 times.
✗ Branch 66 → 140 not taken.
✓ Branch 67 → 68 taken 2139 times.
✗ Branch 67 → 139 not taken.
✓ Branch 68 → 69 taken 2139 times.
✗ Branch 68 → 137 not taken.
|
2139 | commandBuilder << " " << objectFilePath.string(); |
| 127 |
1/2✓ Branch 80 → 81 taken 333 times.
✗ Branch 80 → 154 not taken.
|
333 | const std::string command = commandBuilder.str(); |
| 128 | |||
| 129 | // Print status message | ||
| 130 |
1/2✗ Branch 81 → 82 not taken.
✓ Branch 81 → 95 taken 333 times.
|
333 | if (cliOptions.printDebugOutput) { |
| 131 | − | std::cout << "\nLinking with: " << linkerInvokerName << " (invoker) / " << linkerName << " (linker)"; // GCOV_EXCL_LINE | |
| 132 | − | std::cout << "\nLinker command: " << command; // GCOV_EXCL_LINE | |
| 133 | − | std::cout << "\nEmitting executable to path: " << outputPath.string() << "\n"; // GCOV_EXCL_LINE | |
| 134 | } | ||
| 135 | |||
| 136 | // Call the linker | ||
| 137 |
1/2✓ Branch 95 → 96 taken 333 times.
✗ Branch 95 → 152 not taken.
|
333 | Timer timer; |
| 138 |
1/2✓ Branch 96 → 97 taken 333 times.
✗ Branch 96 → 152 not taken.
|
333 | timer.start(); |
| 139 |
1/2✓ Branch 97 → 98 taken 333 times.
✗ Branch 97 → 152 not taken.
|
333 | const auto [output, exitCode] = SystemUtil::exec(command); |
| 140 |
1/2✓ Branch 98 → 99 taken 333 times.
✗ Branch 98 → 150 not taken.
|
333 | timer.stop(); |
| 141 | |||
| 142 | // Check for linker error | ||
| 143 | − | if (exitCode != 0) { // GCOV_EXCL_LINE | |
| 144 | − | const std::string errorMessage = "Linker exited with non-zero exit code\nLinker command: " + command; // GCOV_EXCL_LINE | |
| 145 | − | throw LinkerError(LINKER_ERROR, errorMessage); // GCOV_EXCL_LINE | |
| 146 | − | } // GCOV_EXCL_LINE | |
| 147 | |||
| 148 | // Print linker result if appropriate | ||
| 149 | − | if (cliOptions.printDebugOutput && !output.empty()) // GCOV_EXCL_LINE | |
| 150 | − | std::cout << "Linking result: " << output << "\n\n"; // GCOV_EXCL_LINE | |
| 151 | |||
| 152 | // Print link time | ||
| 153 | − | if (cliOptions.printDebugOutput) // GCOV_EXCL_LINE | |
| 154 | − | std::cout << "Total link time: " << timer.getDurationMilliseconds() << " ms\n\n"; // GCOV_EXCL_LINE | |
| 155 | 333 | } | |
| 156 | |||
| 157 | /** | ||
| 158 | * Archive the object files to a static library | ||
| 159 | */ | ||
| 160 | ✗ | void ExternalLinkerInterface::archive() const { | |
| 161 | ✗ | assert(!outputPath.empty()); | |
| 162 | |||
| 163 | // Build the archiver command | ||
| 164 | ✗ | std::stringstream commandBuilder; | |
| 165 | ✗ | const auto [archiverName, archiverPath] = SystemUtil::findArchiver(); | |
| 166 | ✗ | commandBuilder << archiverPath; | |
| 167 | ✗ | commandBuilder << " rcs "; // r = insert files into archive; c = create archive if not existing, s = create archive index | |
| 168 | ✗ | commandBuilder << outputPath.string(); | |
| 169 | ✗ | for (const std::filesystem::path &path : linkedFiles) | |
| 170 | ✗ | commandBuilder << " " << path.string(); | |
| 171 | ✗ | const std::string command = commandBuilder.str(); | |
| 172 | |||
| 173 | // Print status message | ||
| 174 | ✗ | if (cliOptions.printDebugOutput) { | |
| 175 | − | std::cout << "\nArchiving with: " << archiverName; // GCOV_EXCL_LINE | |
| 176 | − | std::cout << "\nArchiver command: " << command; // GCOV_EXCL_LINE | |
| 177 | − | std::cout << "\nEmitting static library to path: " << outputPath.string() << "\n"; // GCOV_EXCL_LINE | |
| 178 | } | ||
| 179 | |||
| 180 | // Call the archiver | ||
| 181 | ✗ | Timer timer; | |
| 182 | ✗ | timer.start(); | |
| 183 | ✗ | const auto [output, exitCode] = SystemUtil::exec(command); | |
| 184 | ✗ | timer.stop(); | |
| 185 | |||
| 186 | // Check for linker error | ||
| 187 | − | if (exitCode != 0) { // GCOV_EXCL_LINE | |
| 188 | − | const std::string errorMessage = "Archiver exited with non-zero exit code\nArchiver command: " + command; // GCOV_EXCL_LINE | |
| 189 | − | throw LinkerError(LINKER_ERROR, errorMessage); // GCOV_EXCL_LINE | |
| 190 | − | } // GCOV_EXCL_LINE | |
| 191 | |||
| 192 | // Print linker result if appropriate | ||
| 193 | − | if (cliOptions.printDebugOutput && !output.empty()) // GCOV_EXCL_LINE | |
| 194 | − | std::cout << "Archiving result: " << output << "\n\n"; // GCOV_EXCL_LINE | |
| 195 | |||
| 196 | // Print link time | ||
| 197 | − | if (cliOptions.printDebugOutput) // GCOV_EXCL_LINE | |
| 198 | − | std::cout << "Total archive time: " << timer.getDurationMilliseconds() << " ms\n\n"; // GCOV_EXCL_LINE | |
| 199 | ✗ | } | |
| 200 | |||
| 201 | /** | ||
| 202 | * Add another object file to be linked when calling 'link()' | ||
| 203 | * | ||
| 204 | * @param path Path to the object file | ||
| 205 | */ | ||
| 206 | 2222 | void ExternalLinkerInterface::addFileToLinkage(const std::filesystem::path &path) { | |
| 207 |
2/4✓ Branch 3 → 4 taken 2222 times.
✗ Branch 3 → 13 not taken.
✓ Branch 10 → 11 taken 2222 times.
✗ Branch 10 → 12 not taken.
|
4444 | if (std::ranges::find(linkedFiles, path) == linkedFiles.end()) |
| 208 | 2222 | linkedFiles.push_back(path); | |
| 209 | 2222 | } | |
| 210 | |||
| 211 | /** | ||
| 212 | * Add another linker flag for the call to the linker executable | ||
| 213 | * | ||
| 214 | * @param flag Linker flag | ||
| 215 | */ | ||
| 216 | 1830 | void ExternalLinkerInterface::addLinkerFlag(const std::string &flag) { | |
| 217 |
3/4✓ Branch 3 → 4 taken 1830 times.
✗ Branch 3 → 13 not taken.
✓ Branch 10 → 11 taken 1829 times.
✓ Branch 10 → 12 taken 1 time.
|
3660 | if (std::ranges::find(linkerFlags, flag) == linkerFlags.end()) |
| 218 | 1829 | linkerFlags.push_back(flag); | |
| 219 | 1830 | } | |
| 220 | |||
| 221 | /** | ||
| 222 | * Add another source file to compile and link in (C or C++) | ||
| 223 | * | ||
| 224 | * @param additionalSource Additional source file | ||
| 225 | */ | ||
| 226 | 12 | void ExternalLinkerInterface::addAdditionalSourcePath(std::filesystem::path additionalSource) { | |
| 227 | // Check if the file exists | ||
| 228 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 12 taken 11 times.
|
12 | if (!exists(additionalSource)) { |
| 229 |
3/6✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 22 not taken.
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 20 not taken.
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 18 not taken.
|
1 | const std::string msg = "The additional source file '" + additionalSource.string() + "' does not exist"; |
| 230 |
1/2✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 24 not taken.
|
1 | throw CompilerError(IO_ERROR, msg); |
| 231 | 1 | } | |
| 232 | |||
| 233 | // Add the file to the linker | ||
| 234 |
1/2✓ Branch 12 → 13 taken 11 times.
✗ Branch 12 → 30 not taken.
|
11 | additionalSource = canonical(additionalSource); |
| 235 | 11 | additionalSource.make_preferred(); | |
| 236 | 11 | addFileToLinkage(additionalSource); | |
| 237 | 11 | } | |
| 238 | |||
| 239 | /** | ||
| 240 | * Link against libmath a.k.a. -lm | ||
| 241 | */ | ||
| 242 | 2 | void ExternalLinkerInterface::requestLibMathLinkage() { linkLibMath = true; } | |
| 243 | |||
| 244 | } // namespace spice::compiler | ||
| 245 |