This is an archive of the discontinued LLVM Phabricator instance.

[XCOFF] Force recording a relocation for weak symbol label.
ClosedPublic

Authored by Esme on Jun 27 2023, 12:33 AM.

Details

Summary

Currently, if there are multiple definitions of the same symbol declared has weak linkage, the linker may choose the wrong one when they are compiled with integrated-as.
For example

> cat t1.c 
__attribute__((weak))
__attribute__((noinline))
int foo() { return 3; }

int main() { return foo(); }

> cat t2.c
__attribute__((weak))
int foo() {  return 1; }

>  ~/Source/install.opt/bin/ibm-clang t2.c t1.c -fintegrated-as && ./a.out; echo $?
3
> ~/Source/install.opt/bin/ibm-clang t2.c t1.c -fno-integrated-as && ./a.out; echo $?
1

This patch fixes the issue.
If the target symbol is a weak label we must not attempt to resolve the fixup directly. Emit a relocation and leave resolution of the final target address to the linker.

Diff Detail

Event Timeline

Esme created this revision.Jun 27 2023, 12:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2023, 12:33 AM
Esme requested review of this revision.Jun 27 2023, 12:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2023, 12:33 AM
shchenz added inline comments.Jul 2 2023, 7:14 PM
llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
182–185

This seems not equal to what we handle for Linux(where we have local entry). On Linux, for non weak functions, they also need a force relocation for usage like compiling a shared library. I've asked help internally.

shchenz accepted this revision as: shchenz.Jul 4 2023, 10:28 PM

LGTM. Thanks for addressing this bug.

llvm/lib/MC/XCOFFObjectWriter.cpp
670

Hmm, ok, seems before this change, we assume R_RBR type relocation is only applied to function sections, so that we only need the virtual address of the section because the target function offset in the section are always 0.

Now, we will generate R_RBR for functions that has no own csect, so using section virtual address is now not enough, we also need to consider about the offset of the function in the "big" csect.

This change makes sense to me.

llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
182–185

Like what we discussed internally, I think this is enough for AIX.

This revision is now accepted and ready to land.Jul 4 2023, 10:28 PM
This revision was landed with ongoing or failed builds.Jul 4 2023, 10:58 PM
This revision was automatically updated to reflect the committed changes.