lld_runner.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/driver/lld_runner.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <numeric>
  8. #include <optional>
  9. #include <string>
  10. #include "common/string_helpers.h"
  11. #include "common/vlog.h"
  12. #include "llvm/ADT/ArrayRef.h"
  13. #include "llvm/ADT/StringRef.h"
  14. // Declare the supported driver flavor entry points.
  15. //
  16. // TODO: Currently, just ELF and MachO, but eventually we should support all of
  17. // the LLD platforms.
  18. //
  19. // NOLINTBEGIN(readability-identifier-naming): External library name.
  20. LLD_HAS_DRIVER(elf)
  21. LLD_HAS_DRIVER(macho)
  22. // NOLINTEND(readability-identifier-naming)
  23. namespace Carbon {
  24. auto LldRunner::LinkHelper(llvm::StringLiteral label,
  25. llvm::ArrayRef<llvm::StringRef> args,
  26. const std::string& path, lld::DriverDef driver_def)
  27. -> bool {
  28. // Allocate one chunk of storage for the actual C-strings and a vector of
  29. // pointers into the storage.
  30. llvm::OwningArrayRef<char> cstr_arg_storage;
  31. llvm::SmallVector<const char*, 64> cstr_args =
  32. BuildCStrArgs(path, args, cstr_arg_storage);
  33. CARBON_VLOG("Running LLD {0}-platform link with args:\n", label);
  34. for (const char* cstr_arg : cstr_args) {
  35. CARBON_VLOG(" '{0}'\n", cstr_arg);
  36. }
  37. lld::Result result =
  38. lld::lldMain(cstr_args, llvm::outs(), llvm::errs(), {driver_def});
  39. // Check for an unrecoverable error.
  40. CARBON_CHECK(result.canRunAgain, "LLD encountered an unrecoverable error!");
  41. // TODO: Should this be forwarding the full exit code?
  42. return result.retCode == 0;
  43. }
  44. auto LldRunner::ElfLink(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  45. return LinkHelper("GNU", args, installation_->ld_lld_path(),
  46. {.f = lld::Gnu, .d = &lld::elf::link});
  47. }
  48. auto LldRunner::MachOLink(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  49. return LinkHelper("Darwin", args, installation_->ld64_lld_path(),
  50. {.f = lld::Darwin, .d = &lld::macho::link});
  51. }
  52. } // namespace Carbon