This patch provides two major changes:
- Add getRelocationInfo to check if a constant will have static, dynamic, or no relocations. (Also rename the original needsRelocation to needsDynamicRelocation.)
- Only allow a constant with no relocations (static or dynamic) to be placed in a mergeable section.
This will allow unused symbols that contain static relocations and happen to fit in mergeable constant sections (.rodata.cstN) to instead be placed in unique-named sections if -fdata-sections is used and subsequently garbage collected by --gc-sections.
See https://lists.llvm.org/pipermail/llvm-dev/2021-February/148281.html.
There are only a handful of needsRelocation uses throughout LLVM so you could even avoid introducing needsDynamicRelocation and instead change needsRelocations return value and update all uses to compare the return value to be more explicit, but I'm fine with either solution.