Files
carbon-lang/toolchain/install/busybox_info.h
T
Jon Ross-Perkins 3070e5cfc6 Try using getMainExecutable to address argv[0] limitations (#5643)
When finding an executable, this validates that the returned binary is a
symlink back to the same thing as /proc/self/exe, also using that as a
fallback for different things.

Looking back at #3912, we started using `findProgramByName` in order to
avoid path canonicalization done by `GetMainExecutable`. That created
issues as in #5096, wherein an `argv[0]` that's not explicit enough
(`llvm-symbolizer` instead of the full path, done in [LLVM's
Signals.cpp](https://github.com/llvm/llvm-project/blob/4f60f45130c6bd96c79e468fe9927a29af760f56/llvm/lib/Support/Signals.cpp#L198))
leads to incorrect results (finding an `llvm-symbolizer` in `$PATH`).

One option to fix this would be to patch LLVM to provide an absolute
path for `llvm-symbolizer`. However, I'll suggest that passing a
filename in `argv[0]` is not terribly uncommon, and could be a migration
limitation if we force it. The failure mode is also opaque; for example:

```
$ /bin/sh -c "exec -a llvm-symbolizer ./bazel-bin/toolchain/carbon"
error: expected carbon-busybox symlink at `/usr/lib/llvm-19/bin/llvm-symbolizer`
```

Combined with the `setenv` of `LLVM_SYMBOLIZER_PATH` in
`busybox_main.cpp`, this is intended to fix #5096.
2025-06-12 16:05:11 +00:00

41 lines
1.3 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
#ifndef CARBON_TOOLCHAIN_INSTALL_BUSYBOX_INFO_H_
#define CARBON_TOOLCHAIN_INSTALL_BUSYBOX_INFO_H_
#include <filesystem>
#include <optional>
#include <string>
#include "common/error.h"
namespace Carbon {
// An optional override of argv0, particularly used by `//toolchain/carbon` to
// get desired behavior without further special-casing.
inline constexpr const char* Argv0OverrideEnv = "CARBON_ARGV0_OVERRIDE";
struct BusyboxInfo {
// The path to `carbon-busybox`.
std::filesystem::path bin_path;
// The mode, such as `carbon` or `clang`.
std::optional<std::string> mode;
};
// Returns the busybox information, given argv[0].
//
// Extracts the desired mode for the busybox from the initial command name.
//
// Checks if the path in argv0 is an executable in a valid Carbon install, or a
// symlink to such an executable, and sets `bin_path` to the path of
// `lib/carbon/carbon-busybox` within that install.
//
// If unable to locate a plausible busybox binary, returns an error instead.
auto GetBusyboxInfo(const char* argv0) -> ErrorOr<BusyboxInfo>;
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_INSTALL_BUSYBOX_INFO_H_