lld_runner.cpp 2.0 KB

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