<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/104670>104670</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            #pragma weak handling is sensitive to macro definitions
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          bhaible
      </td>
    </tr>
</table>

<pre>
    I have two compilation units, rpl.c and main.c.

================= rpl.c ================
```
int rpl_func (void) { return 42; }
```

================= main.c ===============
```
extern int func (void);
#define func rpl_func
extern int rpl_func (void);
#pragma weak func
int main ()
{
  int value = func ();
  return value;
}
```

Build:
```
$ clang -c rpl.c
$ ar rc lib.a rpl.o
$ clang -c main.c
$ clang -o main main.o lib.a
$ ./main; echo $?
Segmentation fault (core dumped)
139
```

I claim that this is a bug, because
1) With "gcc" instead of "clang", it works fine.
2) The documentation at https://gcc.gnu.org/onlinedocs/gcc/Weak-Pragmas.html does not mention effects of macro definitions.
3) If I preprocess main.c before compiling it, it works fine as well:
```
$ clang -c rpl.c
$ ar rc lib.a rpl.o
$ clang -E main.c > main-preprocessed.c
$ clang -c main-preprocessed.c
$ clang -o main main-preprocessed.o lib.a
$ ./main; echo $?
42
```

Seen with
```
$ clang --version
clang version 18.1.4
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/bruno/inst-clang/18.1.4/bin
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VV1vrDYQ_TXmZQSCgf164CHbLVLeKt0r3ccrYwZwY-yVP3bTf18ZSDZNVmlT6UpoPzxzzswZzgB3Tg6aqGabI9ucEh78aGzdjly2ipLWdH_VjzDyC4G_GhBmOkvFvTQagpbeMfwN7FllArjuYOJSZyJj-YnlD-tnefrqtRJ-GbnU2-brNf-V2ke6n33QAhjuL0Z2DA_Adkew5IPVUCErj8B29wn-v4pl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