How to avoid defined in discarded section symbols that are generated by llvm-link?

Context: I want to analyze and modify some codes of QEMU and the analysis needs to cover several files, so I first compile the files to .ll and use llvm-link to link them together.
Then, I compiled the linked and modified .ll to .o with clang. During QEMU’s linking, I replaced the affected .o with the linked large one.

However, I encountered errors:

`.text.sancov.module_ctor_8bit_counters.106' referenced in section `.init_array.2[sancov.module_ctor_8bit_counters.106]' of libcommon.fa.p/hw_nvme_ctrl.c.o: defined in discarded section `.text.sancov.module_ctor_8bit_counters.106[sancov.module_ctor_8bit_counters]' of libcommon.fa.p/hw_nvme_ctrl.c.o
`.text.sancov.module_ctor_8bit_counters.204' referenced in section `.init_array.2[sancov.module_ctor_8bit_counters.204]' of libcommon.fa.p/hw_nvme_ctrl.c.o: defined in discarded section `.text.sancov.module_ctor_8bit_counters.204[sancov.module_ctor_8bit_counters]' of libcommon.fa.p/hw_nvme_ctrl.c.o
`.text.sancov.module_ctor_8bit_counters.288' referenced in section `.init_array.2[sancov.module_ctor_8bit_counters.288]' of libcommon.fa.p/hw_nvme_ctrl.c.o: defined in discarded section `.text.sancov.module_ctor_8bit_counters.288[sancov.module_ctor_8bit_counters]' of libcommon.fa.p/hw_nvme_ctrl.c.o
`.text.sancov.module_ctor_8bit_counters.364' referenced in section `.init_array.2[sancov.module_ctor_8bit_counters.364]' of libcommon.fa.p/hw_nvme_ctrl.c.o: defined in discarded section `.text.sancov.module_ctor_8bit_counters.364[sancov.module_ctor_8bit_counters]' of libcommon.fa.p/hw_nvme_ctrl.c.o
`.text.sancov.module_ctor_8bit_counters.478' referenced in section `.init_array.2[sancov.module_ctor_8bit_counters.478]' of libcommon.fa.p/hw_nvme_ctrl.c.o: defined in discarded section `.text.sancov.module_ctor_8bit_counters.478[sancov.module_ctor_8bit_counters]' of libcommon.fa.p/hw_nvme_ctrl.c.o
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

After checking, I found that sancov.module_ctor_8bit_counters is a function defined in every file I analyzed and after llvm-link, the large file duplicated the symbol several times so I saw sancov.module_ctor_8bit_counters.xxx.

Therefore, I am thinking of two ways: whether I can avoid the error during linking or avoid generating these duplicated symbols when using llvm-link? Or both may not be feasible?

Thanks!

If variables have internal or private linkage LLVM Language Reference Manual — LLVM 19.0.0git documentation it will rename them so they don’t conflict. I’d need more context for what’s going on here, but just as a guess it seems you have some global constructors. References to these pointers is being places in the .init_array.2 section which contains the list of global constructors for ELF platforms, see .init, .ctors, and .init_array | MaskRay. The .text section containing the actual function is then being removed by the linker for whatever reason, so you now have a function pointer that points to a section that the linker deleted. About all I can glean from the error message.

I see in the linked large .ll file, the symbols are in llvm.used llvm.global_ctors. The function is defined with internal.
I use readelf -g to check the groups in the .o compiled from the linked .ll and I see:

COMDAT group section [  694] `.group' [sancov.module_ctor_8bit_counters] contains 14 sections:
   [Index]    Name
   [  695]   .text.sancov.module_ctor_8bit_counters
   [  696]   .rela.text.sancov.module_ctor_8bit_counters
   [  744]   .text.sancov.module_ctor_8bit_counters.106
   [  745]   .rela.text.sancov.module_ctor_8bit_counters.106
   [  771]   .text.sancov.module_ctor_8bit_counters.204
   [  772]   .rela.text.sancov.module_ctor_8bit_counters.204
   [  806]   .text.sancov.module_ctor_8bit_counters.288
   [  807]   .rela.text.sancov.module_ctor_8bit_counters.288
   [  851]   .text.sancov.module_ctor_8bit_counters.364
   [  852]   .rela.text.sancov.module_ctor_8bit_counters.364
   [  965]   .text.sancov.module_ctor_8bit_counters.478
   [  966]   .rela.text.sancov.module_ctor_8bit_counters.478
   [  973]   .init_array.2
   [  974]   .rela.init_array.2
COMDAT group section [  975] `.group' [sancov.module_ctor_8bit_counters.106] contains 2 sections:
   [Index]    Name
   [  976]   .init_array.2
   [  977]   .rela.init_array.2

COMDAT group section [  978] `.group' [sancov.module_ctor_8bit_counters.204] contains 2 sections:
   [Index]    Name
   [  979]   .init_array.2
   [  980]   .rela.init_array.2

COMDAT group section [  981] `.group' [sancov.module_ctor_8bit_counters.288] contains 2 sections:
   [Index]    Name
   [  982]   .init_array.2
   [  983]   .rela.init_array.2

COMDAT group section [  984] `.group' [sancov.module_ctor_8bit_counters.364] contains 2 sections:
   [Index]    Name
   [  985]   .init_array.2
   [  986]   .rela.init_array.2

COMDAT group section [  987] `.group' [sancov.module_ctor_8bit_counters.478] contains 2 sections:
   [Index]    Name
   [  988]   .init_array.2
   [  989]   .rela.init_array.2