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