llvm / llvm/llvm-project

When evaluating an expression, LLDB may call an overridden weak function

Open
#189,539 2 comments 0 reactions 0 assignees View on GitHub
lldb
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

### Reproducer
`main.cpp`:
```cpp
#include

__attribute__((__weak__)) const char *bar() {
return "weak bar() from main.cpp";
}

const char *foo() {
return "strong foo() from main.cpp";
}

int main() {
std::cout << bar() << "\n";
std::cout << foo() << "\n";
return 0; // Break here
}
```

`lib.cpp`:
```cpp
const char *bar() {
return "strong bar() from lib.cpp";
}

__attribute__((__weak__)) const char *foo() {
return "weak foo() from lib.cpp";
}
```

* Compile & debug
```sh
> clang++ -g main.cpp lib.cpp -o test.out
> lldb test.out
(lldb) br set -p "Break here"
(lldb) run
strong bar() from lib.cpp
strong foo() from main.cpp
...
(lldb) print bar()
(const char *) ... "strong bar() from lib.cpp"
(lldb) print foo()
(const char *) ... "weak foo() from lib.cpp"
```

* Change the order of the source files. It looks like LLDB always prefers the latest instance of a function, regardless of whether it is weak or strong:
```sh
> clang++ -g lib.cpp main.cpp -o test.out
> lldb test.out
(lldb) br set -p "Break here"
(lldb) run
strong bar() from lib.cpp
strong foo() from main.cpp
Process 22375 stopped
...
(lldb) print bar()
(const char *) ... "weak bar() from main.cpp"
(lldb) print foo()
(const char *) ... "strong foo() from main.cpp"
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior with the provided main.cpp and lib.cpp using both source-file orders, then run the LLDB expression evaluation commands at the breakpoint. Trace the function lookup used for bar() and foo() during expression evaluation. Done means LLDB selects the same strong definitions used by the running program regardless of compilation order.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.