This is an archive of the discontinued LLVM Phabricator instance.

[ARM] add Thumb-1 8-bit movs/adds relocations to LLVM
ClosedPublic

Authored by stuij on Apr 28 2023, 6:37 AM.

Details

Summary

This patch adds the LLVM-side plumbing for the following relocations:

  • R_ARM_THM_ALU_ABS_G0_NC
  • R_ARM_THM_ALU_ABS_G1_NC
  • R_ARM_THM_ALU_ABS_G2_NC
  • R_ARM_THM_ALU_ABS_G3

(see section 5.6.1.5, Static Thumb16 relocations, of the AArch32 ELF Arm ABI:
https://github.com/ARM-software/abi-aa/blob/844a79fd4c77252a11342709e3b27b2c9f590cf1/aaelf32/aaelf32.rst#5615static-thumb16-relocations)

Which can respectivly be generated by prefixing assembly symbols with:

  • :lower0_7:
  • :lower8_15:
  • :upper0_7:
  • :upper8_15:

LLD support for these relocations will be added in a follow-up patch

Diff Detail

Event Timeline

stuij created this revision.Apr 28 2023, 6:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2023, 6:37 AM
stuij requested review of this revision.Apr 28 2023, 6:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2023, 6:37 AM
stuij edited the summary of this revision. (Show Details)Apr 28 2023, 6:44 AM
stuij added subscribers: john.brawn, simonwallis2.
simonwallis2 added inline comments.May 2 2023, 7:40 AM
llvm/lib/Target/ARM/ARMInstrThumb.td
998–1000

{ let DecoderMethod = "DecodeI8RelocInstruction"; }
applies to tADDi8, not tADDrr,
so it should be moved 10 lines earlier.

stuij updated this revision to Diff 525507.May 25 2023, 2:59 AM

Still WIP, but fixed failing tests, and removed decoder method from add for now (do we actually need it?) so this patch is friendlier for other work that depends on it.

simonwallis2 added inline comments.May 25 2023, 7:54 AM
llvm/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp
1247
llvm_unreachable(":upper_8_15: not supported in ARM state");
1252

Following the architectural definitions,
the term "state" describes an instruction set state (ARM state or Thumb state);
and the term "mode" or "processor mode" relates to execution privilege level.

1257

ARM state

1262

ARM state

stuij updated this revision to Diff 526601.May 30 2023, 7:25 AM

added testing, addressed review comment

stuij edited the summary of this revision. (Show Details)May 30 2023, 7:25 AM
stuij added reviewers: john.brawn, simonwallis2.
stuij updated this revision to Diff 526993.May 31 2023, 4:14 AM

add GNU syntax

stuij marked 4 inline comments as done.May 31 2023, 4:17 AM

A few comments:

  • Please run clang-format on the patch (pre-merge is showing as failed due to this)
  • We should have a test to check that we're emitting the right relocation (probably something similar to test/MC/ARM/thumb-movwt-reloc.s)
  • There should be something in ARMAsmParser::validateInstruction to check that the expression is valid, like we do for movw/movt, as currently you can do movs r0, :lower16:some_symbol and it's accepted and results in a 16-bit mov with a movw relocation.
  • Something weird is happening when I try to use these expressions in movs on v7m. If I try to assemble movs r0, :upper8_15:some_symbol I get "error: expected relocatable expression", and looking at the llvm-mc --show-encoding --show-inst-operands it's selected movs.w instead of 16-bit mov for some reason.
john.brawn added inline comments.Jun 12 2023, 9:24 AM
llvm/include/llvm/BinaryFormat/ELFRelocs/ARM.def
141

According to the ABI document this is R_ARM_THM_ALU_ABS_G3, i.e. without the _NC.

stuij updated this revision to Diff 530915.Jun 13 2023, 8:07 AM

addressed review comment

stuij marked an inline comment as done.Jun 13 2023, 8:10 AM
stuij added inline comments.
llvm/include/llvm/BinaryFormat/ELFRelocs/ARM.def
141

Yes, I noticed that as well, but must have forgotten to update it. Done!

This is still missing a test that the correct relocations are emitted, and checking in ARMAsmParser::validateInstruction that expression is valid.

llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp
498–505

The calculation here isn't correct when IsResolved is false. In that case we're calculating the addend for a relocation, and the value of that will be Value&0xff for all four of these fixups (movw/movt has similar behaviour).

stuij marked an inline comment as done.Jun 13 2023, 11:09 PM

This is still missing a test that the correct relocations are emitted, and checking in ARMAsmParser::validateInstruction that expression is valid.

Yes, I just wanted to possibly unblock you for the LLD work. Still need to address the rest of the comments.

stuij edited the summary of this revision. (Show Details)Jun 13 2023, 11:12 PM
stuij updated this revision to Diff 531792.Jun 15 2023, 9:24 AM
stuij marked 2 inline comments as done.

addressed review comments

Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2023, 9:24 AM
stuij added a comment.Jun 15 2023, 9:28 AM

A few comments:

  • Please run clang-format on the patch (pre-merge is showing as failed due to this)
  • We should have a test to check that we're emitting the right relocation (probably something similar to test/MC/ARM/thumb-movwt-reloc.s)
  • There should be something in ARMAsmParser::validateInstruction to check that the expression is valid, like we do for movw/movt, as currently you can do movs r0, :lower16:some_symbol and it's accepted and results in a 16-bit mov with a movw relocation.
  • Something weird is happening when I try to use these expressions in movs on v7m. If I try to assemble movs r0, :upper8_15:some_symbol I get "error: expected relocatable expression", and looking at the llvm-mc --show-encoding --show-inst-operands it's selected movs.w instead of 16-bit mov for some reason.

the recent changes should have addressed all of these

llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp
498–505

done

This revision is now accepted and ready to land.Jun 16 2023, 4:13 AM
MaskRay added inline comments.Jun 16 2023, 9:32 AM
llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
6422

Use parseOptionalToken.

MaskRay added inline comments.Jun 16 2023, 9:34 AM
llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
1244

isUInt<8>(Value).

Can the limit be tested?

llvm/test/MC/ARM/thumb-8-bit-relocs.s
2

-filetype asm can be omitted.

32

Add -NEXT whenever applicable to test that there is no other relocation.

stuij updated this revision to Diff 532685.Jun 19 2023, 9:01 AM
stuij marked 3 inline comments as done.

addressed review comments

stuij marked an inline comment as done.Jun 19 2023, 9:04 AM
MaskRay accepted this revision.Jun 20 2023, 5:04 PM
MaskRay added inline comments.
llvm/test/MC/ARM/thumb-fixups.s
2

We normally just pipe the output to FileCheck, e.g. | FileCheck %s

10
stuij updated this revision to Diff 533190.Jun 21 2023, 2:33 AM

address review comments

stuij marked 2 inline comments as done.Jun 21 2023, 2:34 AM
This revision was landed with ongoing or failed builds.Jun 22 2023, 8:36 AM
This revision was automatically updated to reflect the committed changes.