mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
This keeps the allocations cheap and simplifies the code. It was inspired by the need to expand param files, but no functionality changed yet.
67 lines
2.1 KiB
C++
67 lines
2.1 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/driver/lld_runner.h"
|
|
|
|
#include <algorithm>
|
|
#include <memory>
|
|
#include <numeric>
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
#include "common/string_helpers.h"
|
|
#include "common/vlog.h"
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
// Declare the supported driver flavor entry points.
|
|
//
|
|
// TODO: Currently, just ELF and MachO, but eventually we should support all of
|
|
// the LLD platforms.
|
|
//
|
|
// NOLINTBEGIN(readability-identifier-naming): External library name.
|
|
LLD_HAS_DRIVER(elf)
|
|
LLD_HAS_DRIVER(macho)
|
|
// NOLINTEND(readability-identifier-naming)
|
|
|
|
namespace Carbon {
|
|
|
|
auto LldRunner::LinkHelper(llvm::StringLiteral label,
|
|
llvm::ArrayRef<llvm::StringRef> args,
|
|
const std::string& path, lld::DriverDef driver_def)
|
|
-> bool {
|
|
// Allocate one chunk of storage for the actual C-strings and a vector of
|
|
// pointers into the storage.
|
|
llvm::BumpPtrAllocator alloc;
|
|
llvm::SmallVector<const char*, 64> cstr_args =
|
|
BuildCStrArgs(path, args, alloc);
|
|
|
|
CARBON_VLOG("Running LLD {0}-platform link with args:\n", label);
|
|
for (const char* cstr_arg : cstr_args) {
|
|
CARBON_VLOG(" '{0}'\n", cstr_arg);
|
|
}
|
|
|
|
lld::Result result =
|
|
lld::lldMain(cstr_args, llvm::outs(), llvm::errs(), {driver_def});
|
|
|
|
// Check for an unrecoverable error.
|
|
CARBON_CHECK(result.canRunAgain, "LLD encountered an unrecoverable error!");
|
|
|
|
// TODO: Should this be forwarding the full exit code?
|
|
return result.retCode == 0;
|
|
}
|
|
|
|
auto LldRunner::ElfLink(llvm::ArrayRef<llvm::StringRef> args) -> bool {
|
|
return LinkHelper("GNU", args, installation_->ld_lld_path(),
|
|
{.f = lld::Gnu, .d = &lld::elf::link});
|
|
}
|
|
|
|
auto LldRunner::MachOLink(llvm::ArrayRef<llvm::StringRef> args) -> bool {
|
|
return LinkHelper("Darwin", args, installation_->ld64_lld_path(),
|
|
{.f = lld::Darwin, .d = &lld::macho::link});
|
|
}
|
|
|
|
} // namespace Carbon
|