Issue 177422
Summary Unable to represent relocation for `__stack_chk_guard` _expression_ when targeting `thumb-windows-gnu` with PIC
Labels backend:ARM, llvm:mc, platform:windows
Assignees
Reporter alexrp
    In Zig, we've been having trouble building our module tests for `thumb-windows-gnu` due to this error:

```
error: <unknown>:0: Cannot represent this _expression_
```

I've narrowed this down to here:

https://github.com/llvm/llvm-project/blob/8b59338e9b7bbea9357ae40427f796a6e44d44fc/llvm/lib/Target/ARM/MCTargetDesc/ARMWinCOFFObjectWriter.cpp#L43-L58

It occurs on code such as this:

```
	.def	ubsan_rt.TypeDescriptor.isSigned;
	.scl	3;
	.type	32;
	.endef
	.p2align	1
	.code	16
	.thumb_func
ubsan_rt.TypeDescriptor.isSigned:
.Lfunc_begin5:
	.cv_func_id 6
	.cv_loc	6 5 37 0
.seh_proc ubsan_rt.TypeDescriptor.isSigned
	push.w	{r11, lr}
	.seh_save_regs_w	{r11, lr}
	mov	r11, sp
	.seh_save_sp	r11
	sub	sp, #16
	.seh_stackalloc	16
	.seh_endprologue
	movw	r0, :lower16:(.refptr.__stack_chk_guard-(.LPC5_0+4))
	movt	r0, :upper16:(.refptr.__stack_chk_guard-(.LPC5_0+4))
.LPC5_0:
	...snip...
```

Some details from GDB:

```
(gdb) break ARMWinCOFFObjectWriter.cpp:43 if IsCrossSection
Breakpoint 4 at 0x7fffebe3fc30: file /home/alexrp/Sources/llvm/llvm/lib/Target/ARM/MCTargetDesc/ARMWinCOFFObjectWriter.cpp, line 47.
(gdb) r
...snip...
Thread 1 "zig" hit Breakpoint 4, (anonymous namespace)::ARMWinCOFFObjectWriter::getRelocType (this=this@entry=0x7f2d060, Ctx=...,
    Target=..., Fixup=..., IsCrossSection=IsCrossSection@entry=true, MAB=...)
    at /home/alexrp/Sources/llvm/llvm/lib/Target/ARM/MCTargetDesc/ARMWinCOFFObjectWriter.cpp:47
47                                                    const MCAsmBackend &MAB) const {
(gdb) p Target
$3 = (const llvm::MCValue &) @0x7ffffffe7d40: {SymA = 0x840db50, SymB = 0x840dc08, Cst = -4, Specifier = 0}
(gdb) p Target.SymA->getName()
$4 = {static npos = 18446744073709551615, Data = "" ".refptr.__stack_chk_guard", Length = 25}
(gdb) p Target.SymB->getName()
$5 = {static npos = 18446744073709551615, Data = "" ".LPC5_0", Length = 7}
(gdb) p FixupObject
$6 = (const llvm::MCFixup &) @0x17ceb040: {Value = 0x840dc78, Offset = 8, Kind = 4037, PCRel = false, LinkerRelaxable = false}
```

Reproducer: https://files.alexrp.com/30923/behavior.bc

```
❯ llc -O0 behavior.bc -filetype obj --relocation-model pic
<unknown>:0: error: Cannot represent this _expression_
<unknown>:0: error: Cannot represent this _expression_
<unknown>:0: error: Cannot represent this _expression_
...snip...
```

It's worth noting that it compiles w/o `--relocation-model pic`:

```
❯ llc -O0 behavior.bc -filetype obj
❯ llvm-objdump -Dr behavior.obj | grep -C3 stack_chk_guard
     126: 46eb          mov r11, sp
     128: b0c2          sub sp, #0x108
     12a: f240 0300     movw    r3, #0x0
            0000012a:  IMAGE_REL_ARM_MOV32T .refptr.__stack_chk_guard
     12e: f2c0 0300     movt    r3, #0x0
     132: 681b          ldr r3, [r3]
     134: 681b          ldr r3, [r3]
```

cc @mstorsjo because it seems you did some work in this area some years back.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to