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

    <tr>
        <th>Summary</th>
        <td>
            `error: relocation refers to a discarded section` when creating a shared library with `-fsanitize=fuzzer-no-link`
        </td>
    </tr>

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

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

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

<pre>
    ### Environment
clang version 18.1.8
Target: x86_64-unknown-linux-gnu
OS: SUSE Linux Enterprise Server 15 SP2

### Problem Description
I'm seeing the below linker error while trying to upgrade from clang 14.0.6 to clang 18.1.8. The error is seen when the `-fsanitize=fuzzer-no-link` option is specified to the compiler and linker. Removing the `-fsanitize=fuzzer-no-link` fixes the problem.

```
ld.lld: error: relocation refers to a discarded section: .rodata..str
>>> defined in b_fuzz.o
>>> section group signature: .str.4d6d471c262fa333656703edb5fd20e2
>>> prevailing definition is in a_fuzz.o
>>> referenced by b.c
>>> b_fuzz.o:(funcb)
```

### Minimal Reproduction Steps
Contents of file `a.c`
```
int global_var;

int funca()
{
        const char *who = "funca";
        return 0;
}
```

Contents of file `b.c`
```
int global_var;

int funcb()
{
        const char *who = "funcb";
        return 0;
}
```

Compilation and linking commands
```
clang -O0 -fPIC -m64 -fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address -fsanitize=fuzzer-no-link -o a_fuzz.o -c a.c
clang -O0 -fPIC -m64 -fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address -fsanitize=fuzzer-no-link -o b_fuzz.o -c b.c
clang -shared -O0 -fPIC -m64 -Xlinker --allow-multiple-definition -fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address -fuse-ld=lld -fsanitize=fuzzer-no-link -o lib_ab_fuzz.so a_fuzz.o b_fuzz.o
```

### Observations
- If I remove `-fsanitize=fuzzer-no-link` from the commands listed above, the error disappears.
- I ran `objdump -x -s a_fuzz.o` and `objdump -x -s b_fuzz.o` and noticed that the same section group signature `.str.4d6d471c262fa333656703edb5fd20e2` is present in both .o files when they are compiled with `-fsanitize=fuzzer-no-link`. If I compile without `-fsanitize=fuzzer-no-link`, the section group signature is different for both .o files and this linker error is not seen.
- A workaround is to add a new global variable to one of the files. This causes the signature for the two files to become different. For example, adding `int dummy;` at the top of file `a.c` fixes the problem. 
- This error was not seen with clang 14.0.6. I have not tried other clang versions between 14 and 18 but I can try specific versions if you'd like me to.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVl9r47oS_zTKy2Djf3GShzxkty0U7mWX7V64b0WyxrG2smRGctLupz9Idtqk3dLdw4EDhjbS_PnNaOY3w51Te4O4ZctPbHm14KPvLG17rjvrie8XwsqnLSvK6YNrc1BkTY_Gs2zXaG72cEByyhrI12merlm2-85pj56VO3hc1_d1lYzmwdijSbQy42OyNyPLdl_ugsDd_-6u4T_hGK6NRxpIOYQ7pAMS5Eu4-1qwbBe-ZwhfyQqNPVyha0gNXlnDst0tK1Y9OERl9uA7BIHaHkEr84AESGQJjp3SCJ6eooyFcdgTlwgt2R6mWPIqzdI6XM6_Y0wpfO9wNqJc8GLg2KGJjlidJa3jRnn1E1l51Y4_fyIlxoZwH1idgY0go-aAjWoVyuAhKDe2H5RGAm7kDDaFb9jbwymOj8236hFdlB2m1KRzyups_rKdlqnWMmQ8RhH-IdS24REZYYvkAiYOUrmGk0QJDpuY3HIHKVnJPU9T5ylYLq-nDyS2yqAEZUDcB2ipvbifbcCe7DhAqDXuR8Jo03lKK1nLapU3RV20vCzLelmvshKlWLayyLC4MDYQHrjSITPRrzqlVRngv_Ie40LToATxBCJtLm6fAZc7Vqzb0TSCFZtXibsovf8qo3qu4RsOZOU4hXbncXAs2322xqPxDmwLbSg0Vmc8bWYzZyaV8bDXVnB9f-DEyk-Tm3AcQHBWrGccq-lq01jjPDQdJ2DF7thZYOUVsKKY5Y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