<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/93633>93633</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Functions generated by Function::createWithDefaultAttr should respect -target-features
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
pcc
</td>
</tr>
</table>
<pre>
The following example usage of coverage + HWASan + LTO will fail:
```
$ cat test.cc
__attribute__((weak)) bool foo = false;
__attribute__((weak)) void bar() {}
int main() {
if (foo) bar();
}
$ clang test.cc -O3 --target=aarch64-linux-android30 -flto -fsanitize=hwaddress -coverage --sysroot=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot --gcc-toolchain=$NDK/toolchains/llvm/prebuilt/linux-x86_64
ld.lld: error: a.out.lto.o:(function __llvm_gcov_writeout: .text.__llvm_gcov_writeout+0x10): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 9079256848778895360 is not in [-4294967296, 4294967295]; references section '.rodata..L.hwasan'
>>> referenced by ld-temp.o
ld.lld: error: a.out.lto.o:(function __llvm_gcov_writeout: .text.__llvm_gcov_writeout+0x3c): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 8935141660703096832 is not in [-4294967296, 4294967295]; references section '.data..L__llvm_gcov_ctr.hwasan'
>>> referenced by ld-temp.o
ld.lld: error: a.out.lto.o:(function __llvm_gcov_writeout: .text.__llvm_gcov_writeout+0x64): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 9007199254741024768 is not in [-4294967296, 4294967295]; references section '.bss..L__llvm_gcov_ctr.1.hwasan'
>>> referenced by ld-temp.o
ld.lld: error: a.out.lto.o:(function __llvm_gcov_reset: .text.__llvm_gcov_reset+0x0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 8935141660703096832 is not in [-4294967296, 4294967295]; references section '.data..L__llvm_gcov_ctr.hwasan'
>>> referenced by ld-temp.o
ld.lld: error: a.out.lto.o:(function __llvm_gcov_reset: .text.__llvm_gcov_reset+0xc): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 9007199254741024768 is not in [-4294967296, 4294967295]; references section '.bss..L__llvm_gcov_ctr.1.hwasan'
>>> referenced by ld-temp.o
```
The cause is that the compiler-generated functions `__llvm_gcov_writeout` and `__llvm_gcov_reset` do not have `target-features=+tagged-globals` attributes. These functions are created by `Function::createWithDefaultAttr`. The general form of this problem is known, and as such there's already an attempt in that function to copy certain attributes from the module flags onto the function. But for target features there's no module flag since the whole point of target features is that they're per-function.
I feel like this is probably a bug in `Function::createWithDefaultAttr` (which GCOV uses to create its compiler generated functions). It should probably set the target-features attribute to a list that it reads from the LLVMContext, or something like that (which Clang would fill in before running the pass pipeline). As things are, these generated functions won't respect things like `-ffixed-x*` flags either. That would imply that `Function::createWithDefaultAttr` shouldn't be used during LTO or in the backend. If looks like that's mostly already the case except for a couple of the GPU backends that are calling this function to create constructors.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkV02P2zgS_TX0pSBBpmTJOvhgt-Oksb2boDebHA2KKkncpkmBpNr2_PoBKX-l0QM0poP5wAA-2KTr8dWrV1KRWStahbggsxWZrSdscJ02i57zSaXr4-Jrh9BoKfVeqBbwwHa9RBgsaxF0A1w_o_HfCV3Bp-_L_zIVvj58_Qx7ISU0TEiSLkmyJsmS5MnpM_6kGXDmwKF1Mefj4nbLnDOiGhxut4TOCZ3vkT0RWhJaQqW1hEZrIOkaGiYtknR1QntD-LMWNVTMhI0SSLEixfo2XCgHOybUzR_COoBogNB5o3WgcYa4nn7B8UlJptpzWhB9TiGKHDMtOpKuGTO8y7NICjUcIqZqo0WdJhA10mmIGsuUcOIXJOm627O6NmgtRBeho8gerdHaQxGa_Wf9L0I3TmvJOyaUJXQj5fOO0E1vsBqEdH4lHHWY59s8I3RzAoAoajmPLrG_FzCkLetYypqkS0BjtPFfWKwHF0unY-0dQOfNo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