Skip to content

Unexpected relocation type R_X86_64_REX_GOTPCRELX when customized stack protector is enabled with -fno-PIE #60116

Closed
@bysui

Description

@bysui

GCC version: 9.3.0
CLANG version: 15.0.7

Hello.

I'm trying to pick the commit in https://lwn.net/ml/linux-kernel/[email protected]/ , which uses
-mstack-protector-guard-reg=gs -mstack-protector-guard-symbol=__stack_chk_guard to implement per-cpu variable for the stack protector instead of fixed location.

But kernel built with LLVM=1 failed due to unexpected relocation type R_X86_64_REX_GOTPCRELX for __stack_chk_guard.
Although, it would be optimized by linker later. However, for GCC, it generates relocation type R_X86_64_PC32 directly.

So I write a test case as following:

#include <err.h>

extern int ttyname_r(int, char *, int);

int test(void)
{
	char name[10];

	if (ttyname_r(0, name, 10))
	    err(1, "capsicum");

        return 0;
}

For gcc, it generates R_X86_64_PC32.

gcc -O2 -fstack-protector-strong -mcmodel=kernel -fno-PIE -mstack-protector-guard-reg=gs -mstack-protector-guard-symbol=__stack_chk_guard -c test.c -o test.o

objdump -r test.o

test.o:     file format elf64-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
000000000000000f R_X86_64_PC32     __stack_chk_guard-0x0000000000000004
0000000000000020 R_X86_64_PLT32    ttyname_r-0x0000000000000004
0000000000000031 R_X86_64_PC32     __stack_chk_guard-0x0000000000000004
0000000000000041 R_X86_64_32S      .rodata.str1.1
000000000000004d R_X86_64_PLT32    err-0x0000000000000004
0000000000000052 R_X86_64_PLT32    __stack_chk_fail-0x0000000000000004


RELOCATION RECORDS FOR [.eh_frame]:
OFFSET           TYPE              VALUE
0000000000000020 R_X86_64_PC32     .text

For clang, it generates R_X86_64_REX_GOTPCRELX.

clang -O2 -fstack-protector-strong -mcmodel=kernel -fno-PIE -mstack-protector-guard-reg=gs -mstack-protector-guard-symbol=__stack_chk_guard -c test.c -o test.o

objdump -r test.o

test.o:     file format elf64-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
0000000000000008 R_X86_64_REX_GOTPCRELX  __stack_chk_guard-0x0000000000000004
0000000000000022 R_X86_64_PLT32    ttyname_r-0x0000000000000004
0000000000000045 R_X86_64_32S      .rodata.str1.1
000000000000004c R_X86_64_PLT32    err-0x0000000000000004
0000000000000051 R_X86_64_PLT32    __stack_chk_fail-0x0000000000000004


RELOCATION RECORDS FOR [.eh_frame]:
OFFSET           TYPE              VALUE
0000000000000020 R_X86_64_PC32     .text

Why clang doesn't generates relocation type R_X86_64_PC32 directly with -fno-PIE ?

Thanks.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions