Skip to content

[JITLink][AArch32] Fix Unaligned Data Symbol Address Resolution #97030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 29, 2024

Conversation

eymay
Copy link
Contributor

@eymay eymay commented Jun 28, 2024

The ARM architecture uses the LSB bit for ARM/Thumb mode switch
flagging. This is true for alignments of 2 and 4 but in data
relocations the alignment is 1 allowing the LSB bit to be set.
ELF::STT_OBJECT typed symbols are made to bypass the TargetFlag
mechanism assuming they are the only symbols affected by data
relocations.

The test is a minimal example of the issue mentioned below.

Fixes #95911 "Orc global constructor order test fails on 32
bit ARM".

The ARM architecture uses the LSB bit for ARM/Thumb mode switch
flagging. This is true for alignments of 2 and 4 but in data
relocations the alignment is 1 allowing the LSB bit to be set.
ELF::STT_OBJECT typed symbols are made to bypass the TargetFlag
mechanism assuming they are the only symbols affected by data
relocations.

The test is a minimal example of the issue mentioned below.

Fixes the issue "Orc global constructor order test fails on 32
bit ARM llvm#95911".
@eymay eymay requested review from DavidSpickett and weliveindetail and removed request for DavidSpickett June 28, 2024 09:49
Copy link
Member

@weliveindetail weliveindetail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's great. Thanks for investigating! As you already proposed, maybe turn the condition around and only emit the Thumb target flag for callable symbols which should be ELF::STT_FUNC indeed.

@@ -200,6 +200,9 @@ class ELFLinkGraphBuilder_aarch32

protected:
TargetFlagsType makeTargetFlags(const typename ELFT::Sym &Sym) override {
// Data symbols do not have Arm or Thumb flags.
if (Sym.getType() == ELF::STT_OBJECT)
return TargetFlagsType{};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your fix in getRawOffset() this check shouldn't be necessary anymore, isn't it? Instead we might want to assert that all symbols with the ThumbSymbol target flag are ELF::STT_FUNC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree but I had to keep it because of a failed assert in

assert(Sym.isCallable() && "Only callable symbols can have thumb flag");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your fix in getRawOffset() this check shouldn't be necessary anymore, isn't it? Instead we might want to assert that all symbols with the ThumbSymbol target flag are ELF::STT_FUNC.

Updated the condition to check for STT_FUNC. I found a similar assert to what you mentioned in the previous comment, should I still add one?

.asciz "H1"
.size Lstr.H1, 3

# Not 0x324800
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything special here compared to the other two cases? Or is this a left-over comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is actually, this 'H2' string is the critical part which caused the errors. Before the fix we were getting 0x324800 as its address was indexed by 0x2 instead of 0x3. I wanted something like CHECK-NOT though we do not have it in Checker infra currently. So I left it as comment there.

@DavidSpickett
Copy link
Collaborator

Nice work, I would have had 0 chance of finding this.

I've tested this natively. The new test and the existing test are now passing.

Copy link
Member

@weliveindetail weliveindetail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small note on your FIXME. Otherwise LGTM!

# FIXME: The expression we want is either *{3}(Lstr.H1) = ...
# or *{4}(Lstr.H1) & 0x00ffffff = ...
# The first is not supported and the latter segfaults.
# Also, whitespaces are not recognized and not consumed by the checker.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could write either (*{4}(Lstr.H1))[23:0] or (*{4}(Lstr.H1)) & 0x00ffffff. Does that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does! Thanks for the tip.

@eymay eymay merged commit cd0f891 into llvm:main Jun 29, 2024
6 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 29, 2024

LLVM Buildbot has detected a new failure on builder fuchsia-x86_64-linux running on fuchsia-debian-64-us-central1-a-1 while building llvm at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/11/builds/849

Here is the relevant piece of the build log for the reference:

Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/fuchsia-linux.py ...' (failure)
...
[1409/1414] Building CXX object unittests/Transforms/Scalar/CMakeFiles/ScalarTests.dir/LoopPassManagerTest.cpp.o
clang++: warning: optimization flag '-ffat-lto-objects' is not supported [-Wignored-optimization-argument]
[1410/1414] Linking CXX executable unittests/Transforms/Scalar/ScalarTests
[1410/1414] Running the LLVM regression tests
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:508: note: using ld.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/ld.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:508: note: using lld-link: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/lld-link
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:508: note: using ld64.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/ld64.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:508: note: using wasm-ld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/wasm-ld
-- Testing: 54603 tests, 60 workers --
Testing:  0.. 10.. 20.. 30.. 40.. 50
FAIL: LLVM :: ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s (28866 of 54603)
******************** TEST 'LLVM :: ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-mc -triple=armv7-linux-gnueabi -arm-add-build-attributes -filetype=obj -o /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-mc -triple=armv7-linux-gnueabi -arm-add-build-attributes -filetype=obj -o /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
RUN: at line 2: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-objdump -s --section=.rodata /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o | /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/FileCheck --check-prefix=CHECK-OBJ /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-objdump -s --section=.rodata /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/FileCheck --check-prefix=CHECK-OBJ /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
RUN: at line 3: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-jitlink -noexec -slab-address 0x76ff0000 -slab-allocate 10Kb               -slab-page-size 4096 /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o -debug-only=jitlink 2>&1               | /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/FileCheck --check-prefix=CHECK-LG /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-jitlink -noexec -slab-address 0x76ff0000 -slab-allocate 10Kb -slab-page-size 4096 /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o -debug-only=jitlink
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/FileCheck --check-prefix=CHECK-LG /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
/var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s:22:13: error: CHECK-LG: expected string not found in input
# CHECK-LG: Starting link phase 1 for graph
            ^
<stdin>:1:1: note: scanning from here
llvm-jitlink: Unknown command line argument '-debug-only=jitlink'. Try: '/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-jitlink --help'
^
<stdin>:1:144: note: possible intended match here
llvm-jitlink: Unknown command line argument '-debug-only=jitlink'. Try: '/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-jitlink --help'
                                                                                                                                               ^

Input file: <stdin>
Check file: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s

-dump-input=help explains the following input dump.

Input was:
<<<<<<
            1: llvm-jitlink: Unknown command line argument '-debug-only=jitlink'. Try: '/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-jitlink --help' 
check:22'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
check:22'1                                                                                                                                                    ?                    possible intended match
            2: llvm-jitlink: Did you mean '--entry=jitlink'? 
check:22'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>>>>

--
Step 7 (check) failure: check (failure)
...
[1409/1414] Building CXX object unittests/Transforms/Scalar/CMakeFiles/ScalarTests.dir/LoopPassManagerTest.cpp.o
clang++: warning: optimization flag '-ffat-lto-objects' is not supported [-Wignored-optimization-argument]
[1410/1414] Linking CXX executable unittests/Transforms/Scalar/ScalarTests
[1410/1414] Running the LLVM regression tests
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:508: note: using ld.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/ld.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:508: note: using lld-link: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/lld-link
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:508: note: using ld64.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/ld64.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:508: note: using wasm-ld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/wasm-ld
-- Testing: 54603 tests, 60 workers --
Testing:  0.. 10.. 20.. 30.. 40.. 50
FAIL: LLVM :: ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s (28866 of 54603)
******************** TEST 'LLVM :: ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-mc -triple=armv7-linux-gnueabi -arm-add-build-attributes -filetype=obj -o /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-mc -triple=armv7-linux-gnueabi -arm-add-build-attributes -filetype=obj -o /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
RUN: at line 2: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-objdump -s --section=.rodata /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o | /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/FileCheck --check-prefix=CHECK-OBJ /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-objdump -s --section=.rodata /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/FileCheck --check-prefix=CHECK-OBJ /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
RUN: at line 3: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-jitlink -noexec -slab-address 0x76ff0000 -slab-allocate 10Kb               -slab-page-size 4096 /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o -debug-only=jitlink 2>&1               | /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/FileCheck --check-prefix=CHECK-LG /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-jitlink -noexec -slab-address 0x76ff0000 -slab-allocate 10Kb -slab-page-size 4096 /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/test/ExecutionEngine/JITLink/AArch32/Output/ELF_data_alignment.s.tmp_armv7.o -debug-only=jitlink
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/FileCheck --check-prefix=CHECK-LG /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s
/var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s:22:13: error: CHECK-LG: expected string not found in input
# CHECK-LG: Starting link phase 1 for graph
            ^
<stdin>:1:1: note: scanning from here
llvm-jitlink: Unknown command line argument '-debug-only=jitlink'. Try: '/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-jitlink --help'
^
<stdin>:1:144: note: possible intended match here
llvm-jitlink: Unknown command line argument '-debug-only=jitlink'. Try: '/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-jitlink --help'
                                                                                                                                               ^

Input file: <stdin>
Check file: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_data_alignment.s

-dump-input=help explains the following input dump.

Input was:
<<<<<<
            1: llvm-jitlink: Unknown command line argument '-debug-only=jitlink'. Try: '/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-pfr_blbl/bin/llvm-jitlink --help' 
check:22'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
check:22'1                                                                                                                                                    ?                    possible intended match
            2: llvm-jitlink: Did you mean '--entry=jitlink'? 
check:22'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>>>>

--

eymay added a commit that referenced this pull request Jun 29, 2024
… build

The expressive test added in PR #97030 requires debug option in cli.
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
…#97030)

The ARM architecture uses the LSB bit for ARM/Thumb mode switch
flagging. This is true for alignments of 2 and 4 but in data
relocations the alignment is 1 allowing the LSB bit to be set.
Now only `ELF::STT_FUNC` typed symbols are used in the 
TargetFlag mechanism.

The test is a minimal example of the issue mentioned below.

Fixes llvm#95911 "Orc global constructor order test fails on 32
bit ARM".
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
… build

The expressive test added in PR llvm#97030 requires debug option in cli.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Orc global constructor order test fails on 32 bit ARM
4 participants