lld_runner.cpp 2.1 KB

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