Bug 29349 - ADR instructions generate wrong offsets in Thumb code for armv7-m
Summary: ADR instructions generate wrong offsets in Thumb code for armv7-m
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: gas (show other bugs)
Version: 2.37
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-07-10 05:40 UTC by Jesse Taube
Modified: 2022-07-11 19:27 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2022-07-11 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jesse Taube 2022-07-10 05:40:50 UTC
In U-Boot after this commit, armv7-m fails to boot because of this commit.
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=d3e52e120b68bf19552743fbc078e0a759f48cb7

The bug is demonstrated below in a cleaner format:
```
.syntax unified
.global bug;
.align 4
bugs:
         adr     r3, bugs
.size bugs, .-bugs
.type bugs 2; // This changes offset from 4 to 3 in
include/linux/linkage.h:ENDPROC
//arm-linux-gnueabi-as  -march=armv7-m -c -o bug.o bug.S &&
arm-linux-gnueabi-objdump --disassemble=bug bug.o
```
what is more confusing is https://sourceware.org/binutils/docs/as/ARM-Opcodes.html conflicts with the arm docs about the ADR instruction giving a multiple of 4 on thumb.
Comment 1 Tamar Christina 2022-07-11 12:59:11 UTC
(In reply to Jesse Taube from comment #0)
> In U-Boot after this commit, armv7-m fails to boot because of this commit.
> https://sourceware.org/git/?p=binutils-gdb.git;a=commit;
> h=d3e52e120b68bf19552743fbc078e0a759f48cb7
> 

Are you sure? That commit is only in binutils 2.37 and newer. The supplied testcase goes wrong way before that https://godbolt.org/z/xfdd9Mf5Y and reverting
the commit does not fix the offset.

My own bisect results in this commit https://github.com/bminor/binutils-gdb/commit/52a86f843b6dee1de9977293da9786649b146b05 in binutils 2.29.
Comment 2 Jesse Taube 2022-07-11 16:44:19 UTC
it changes when the type is set to function before vs after.

In U-Boot and Linux it is set after so I kept it the same.

https://godbolt.org/z/hncMz7rb6

I checked against llvm and they are always 4.
Comment 3 Tamar Christina 2022-07-11 16:54:01 UTC
I'm not saying that there isn't a bug. I'm just asking if you're sure it's my commit that caused it.  As the error exists way before it.

> ./gas/as-new -march=armv7-m -c -o bug.o ../bug2.s; ./binutils/objdump -Dr bug.o | grep subw
   0:   f2af 0303       subw    r3, pc, #3
  10:   f2af 0304       subw    r3, pc, #4

> git checkout HEAD~1
Previous HEAD position was 52a86f843b6 Fix use of ARM ADR and ADRl pseudo-instructions with thumb function symbols.
HEAD is now at b32465c97c1 MIPS16e2: Add new MIPS16e2 relaxation GAS and LD tests

> ./gas/as-new -march=armv7-m -c -o bug.o ../bug2.s; ./binutils/objdump -Dr bug.o | grep subw
   0:   f2af 0304       subw    r3, pc, #4
  10:   f2af 0304       subw    r3, pc, #4

So I don't believe https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=d3e52e120b68bf19552743fbc078e0a759f48cb7 caused it.

But, confirmed.
Comment 4 Jesse Taube 2022-07-11 19:27:07 UTC
I'm sure when I tested it, that commit broke u-boot. It seems like there are two bugs causing the same problem. The way U-boot has it written is like bugB notice the `type` is after. Which does seem to change after d3e52e120. Which I noticed a while ago but didn't look into it. If it was written as you wrote it, the bug would have appeared at 52a86f843b6. To be fair I'm not very familiar with Assembly, I just know it's broken if you would like to see U-Boots side.
https://lore.kernel.org/all/20220710070953.933688-1-Mr.Bossman075@gmail.com/t/
  
Thanks for looking into this much appreciated.

.syntax unified
.global bugA;
.type bugA, %function
.align 4
bugA:
         adr     r3, bugA
.size bugA, .-bugA

.global bugB;
.align 4
bugB:
         adr     r3, bugB
.size bugB, .-bugB
.type bugB, %function