lld_runner.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/vlog.h"
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/ADT/StringRef.h"
  13. // Declare the supported driver flavor entry points.
  14. //
  15. // TODO: Currently, just ELF and MachO, but eventually we should support all of
  16. // the LLD platforms.
  17. //
  18. // NOLINTBEGIN(readability-identifier-naming): External library name.
  19. LLD_HAS_DRIVER(elf)
  20. LLD_HAS_DRIVER(macho)
  21. // NOLINTEND(readability-identifier-naming)
  22. namespace Carbon {
  23. auto LldRunner::LinkHelper(llvm::StringLiteral label,
  24. llvm::ArrayRef<llvm::StringRef> args,
  25. const std::string& path, lld::DriverDef driver_def)
  26. -> bool {
  27. // Allocate one chunk of storage for the actual C-strings and a vector of
  28. // pointers into the storage.
  29. llvm::OwningArrayRef<char> cstr_arg_storage;
  30. llvm::SmallVector<const char*, 64> cstr_args =
  31. BuildCStrArgs("LLD", path, "-v", args, cstr_arg_storage);
  32. CARBON_VLOG("Running LLD {0}-platform link...\n", label);
  33. lld::Result result =
  34. lld::lldMain(cstr_args, llvm::outs(), llvm::errs(), {driver_def});
  35. // Check for an unrecoverable error.
  36. CARBON_CHECK(result.canRunAgain, "LLD encountered an unrecoverable error!");
  37. // TODO: Should this be forwarding the full exit code?
  38. return result.retCode == 0;
  39. }
  40. auto LldRunner::ElfLink(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  41. return LinkHelper("GNU", args, installation_->ld_lld_path(),
  42. {.f = lld::Gnu, .d = &lld::elf::link});
  43. }
  44. auto LldRunner::MachOLink(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  45. return LinkHelper("Darwin", args, installation_->ld64_lld_path(),
  46. {.f = lld::Darwin, .d = &lld::macho::link});
  47. }
  48. } // namespace Carbon