Issue 240619
Summary [BOLT] Dynamically linked PIEs without PT_INTERP are misclassified as static PIEs after #204475
Labels BOLT
Assignees
Reporter yabinc
    ### Description

After commit 5f3e70ca97a835605ee54f01e00d883a3be0d220 ("[BOLT] Instrument static PIEs through entry point", #204475), `RewriteInstance::readELFDynamic()` classifies any ELF binary with `DF_1_PIE` and no `PT_INTERP` header as a static PIE:

```cpp
    case ELF::DT_FLAGS_1: {
      auto Flags = Dyn.getVal();
      if (Flags & ELF::DF_1_PIE && !BC->HasInterpHeader) {
        BC->outs() << "BOLT-INFO: static pie executable detected\n";
        BC->IsStaticExecutable = true;
      }
      break;
    }
```

However, dynamically linked PIEs can legitimately omit `PT_INTERP`. For example, in the Android LLVM toolchain build, host `clang` on musl is built as a relocatable dynamically linked PIE (`DF_1_PIE` with `DT_NEEDED` dependencies on `libc_musl.so`, `libLLVM.so`, `libclang-cpp.so`, etc.) without a hardcoded `PT_INTERP` path so the toolchain archive can be unpacked at any path: a custom startup stub in `Scrt1.o` (`relinterp`, which discards `.interp`) locates and hands off to the bundled musl dynamic loader relative to the executable at runtime, and the loader then processes `DT_NEEDED` and `.init_array`/`.fini_array` (`DT_INIT`/`DT_FINI`) normally.

Prior to 5f3e70ca97a8, `llvm-bolt --instrument` handled these binaries when BOLT-instrumenting `clang` by adjusting `--runtime-lib-init-hook` to `init` and hooking initialization/finalization via `DT_INIT`/`DT_INIT_ARRAY` and `DT_FINI`/`DT_FINI_ARRAY`.

With `BC->IsStaticExecutable = true`:
1. `adjustCommandLineOptions()` no longer switches `RuntimeLibInitHook` to `RLIH_INIT` when `!BC->HasInterpHeader`.
2. `discoverRtInitAddress()`, `discoverRtFiniAddress()`, `updateRtInitReloc()`, and `updateRtFiniReloc()` are all skipped because they are gated on `!BC->IsStaticExecutable`.
3. `InstrumentationRuntimeLibrary::adjustCommandLineOptions()` errors out with:
   ```text
   BOLT-ERROR: instrumentation of static binary currently does not support profile output on binary finalization, so it requires -instrumentation-sleep-time=N (N>0) usage
   ```
   Passing `-instrumentation-sleep-time=N` or `--runtime-lib-init-hook=init` does not work around this for short-lived processes like `clang`, because `BC->IsStaticExecutable` still skips hooking finalization (`discoverRtFiniAddress()` / `updateRtFiniReloc()`), so profiles are never written on process exit.

### Minimal Reproducer

```bash
cat > foo.c << 'EOF'
int foo(void) { return 42; }
EOF

cat > main.c << 'EOF'
int foo(void);
int main(void) { return foo(); }
EOF

clang -fPIC -shared foo.c -o libfoo.so
clang -fPIE -pie -fuse-ld=lld -Wl,-q -Wl,--no-dynamic-linker main.c ./libfoo.so -o main

llvm-bolt --instrument main -o main.bolt
```

`llvm-readelf -d main` shows `NEEDED` (`./libfoo.so`, `libc.so.6`), `FLAGS_1: PIE`, `INIT`/`FINI`, and `INIT_ARRAY`/`FINI_ARRAY`, with no `INTERP` program header.

**Before 5f3e70ca97a8:**
```text
BOLT-WARNING: adjusted runtime-lib-init-hook to 'init' due to absence of INTERP header
...
BOLT-INFO: runtime library initialization was hooked via DT_INIT, set to 0x606340
BOLT-INFO: runtime library finalization was hooked via DT_FINI, set to 0x606380
```

**After 5f3e70ca97a8:**
```text
BOLT-INFO: static pie executable detected
...
BOLT-ERROR: instrumentation of static binary currently does not support profile output on binary finalization, so it requires -instrumentation-sleep-time=N (N>0) usage
```

### Suggested Fix

1. In `RewriteInstance::readELFDynamic()`, check whether `PT_DYNAMIC` contains any `DT_NEEDED` entries before classifying a `DF_1_PIE && !BC->HasInterpHeader` binary as `IsStaticExecutable = true` (a true static PIE has no `DT_NEEDED` shared library dependencies, whereas a dynamically linked PIE without `PT_INTERP` has `DT_NEEDED` entries).
2. Optionally, also provide an explicit command-line flag to override static-executable detection if needed.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to