This is an archive of the discontinued LLVM Phabricator instance.

[llvm][IR] Do not place constants with static relocations in a mergeable section
ClosedPublic

Authored by leonardchan on Feb 3 2021, 10:25 AM.

Details

Summary

This patch provides two major changes:

  1. Add getRelocationInfo to check if a constant will have static, dynamic, or no relocations. (Also rename the original needsRelocation to needsDynamicRelocation.)
  2. 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.

Diff Detail

Event Timeline

leonardchan created this revision.Feb 3 2021, 10:25 AM
leonardchan requested review of this revision.Feb 3 2021, 10:25 AM
pcc added a comment.Feb 9 2021, 6:10 PM

I don't think we want 3 functions here because the distinction between your needsDynamicRelocation and needsRelocation isn't meaningful (a dynamic relocation can always be relaxed to a static one by the linker). There should probably just be needsRelocation() (needs static or dynamic) and needsDynamicRelocation() (needs dynamic).

leonardchan edited the summary of this revision. (Show Details)

Updated to remove needsStaticRelocation.

phosek accepted this revision.Feb 16 2021, 1:23 PM

LGTM

llvm/lib/IR/Constants.cpp
659–663

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.

This revision is now accepted and ready to land.Feb 16 2021, 1:23 PM
pcc added a comment.Feb 16 2021, 1:26 PM

I think you will need to s/needsRelocation/needsDynamicRelocation/g throughout the rest of the codebase.

pcc requested changes to this revision.Feb 16 2021, 1:27 PM
This revision now requires changes to proceed.Feb 16 2021, 1:27 PM
In D95960#2566681, @pcc wrote:

I think you will need to s/needsRelocation/needsDynamicRelocation/g throughout the rest of the codebase.

Updated.

pcc accepted this revision.Feb 18 2021, 1:41 PM

LGTM

This revision is now accepted and ready to land.Feb 18 2021, 1:41 PM