You are not logged in.

#1 2024-06-05 03:58:36

TimeOrange
Member
Registered: 2021-10-01
Posts: 6

Clang create '__stack_chk_guard' reference given '-nostdlib'

EDIT: host x86 target has the similar problem, Clang generates relocations as

Relocation section '.rela.text' at offset 0x340 contains 15 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
00000000002b  000400000004 R_X86_64_PLT32    0000000000000000 getarray - 4
00000000003a  000500000004 R_X86_64_PLT32    0000000000000000 putarray - 4
000000000041  000600000004 R_X86_64_PLT32    0000000000000000 getint - 4
00000000004c  000700000004 R_X86_64_PLT32    0000000000000000 putint - 4
000000000056  000800000004 R_X86_64_PLT32    0000000000000000 putch - 4
00000000005d  000900000004 R_X86_64_PLT32    0000000000000000 starttime - 4
0000000000e8  000a00000004 R_X86_64_PLT32    0000000000000000 stoptime - 4
0000000000f0  000700000004 R_X86_64_PLT32    0000000000000000 putint - 4
0000000000fa  000800000004 R_X86_64_PLT32    0000000000000000 putch - 4
000000000101  000600000004 R_X86_64_PLT32    0000000000000000 getint - 4
00000000010b  000b00000004 R_X86_64_PLT32    0000000000000000 getch - 4
000000000125  000b00000004 R_X86_64_PLT32    0000000000000000 getch - 4
00000000012c  000800000004 R_X86_64_PLT32    0000000000000000 putch - 4
000000000144  000800000004 R_X86_64_PLT32    0000000000000000 putch - 4
00000000016a  000c00000004 R_X86_64_PLT32    0000000000000000 __stack_chk_fail - 4

Relocation section '.rela.eh_frame' at offset 0x4a8 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000000020  000200000002 R_X86_64_PC32     0000000000000000 .text + 0

I am writing a baremetal RISC-V user mode application, and use Clang for cross compiling.
Clang is installed by pacman from arch linux offical repo.

clang version 17.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

The program declares some IO functions which will be implemented by linux syscall elsewhere.

#ifndef LIBSYSY_SYSY_H_
#define LIBSYSY_SYSY_H_

// SysY runtime library.
// Reference: https://bit.ly/3tzTFks
// Modified by MaxXing.

// Input & output functions
int getint(), getch(), getarray(int a[]);
void putint(int num), putch(int ch), putarray(int n, int a[]);

// Timing functions
void starttime();
void stoptime();

#endif  // LIBSYSY_SYSY_H_

int main() {
  // sum of array
  int arr[20], n, sum = 0;
  n = getarray(arr);
  putarray(n, arr);
  int count = getint();
  putint(count);
  putch(10);
  starttime();
  for (int j = 0; j < count; ++j) {
    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) sum += arr[i] * arr[k];
    }
  }
  stoptime();
  putint(sum);
  putch(10);

  // read characters
  n = getint();
  getch();
  for (int i = 0; i < n; ++i) putch(getch());
  putch(10);

  return 0;
}

and the code is compiled with following commands.

clang -Wall -Werror -DNO_LIBC -nostdlib -nostdinc -static -target riscv64-unknown-linux-elf -march=rv64im -mabi=lp64 test.c -c -o test.o

The lld linker later will complain about undefined symbol '__stack_chk_guard' and  '__stack_chk_fail' which is not expected to happen when '-nostdlib' is specified.
'readelf -r' tells:

Relocation section '.rela.text' at offset 0x608 contains 58 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000000010  001900000014 R_RISCV_GOT_HI20  0000000000000000 __stack_chk_guard + 0
000000000014  000300000018 R_RISCV_PCREL_LO1 0000000000000010 .Lpcrel_hi0 + 0
000000000014  000000000033 R_RISCV_RELAX                        0
000000000038  001a00000013 R_RISCV_CALL_PLT  0000000000000000 getarray + 0
000000000038  000000000033 R_RISCV_RELAX                        0
00000000004c  001b00000013 R_RISCV_CALL_PLT  0000000000000000 putarray + 0
00000000004c  000000000033 R_RISCV_RELAX                        0
000000000054  001c00000013 R_RISCV_CALL_PLT  0000000000000000 getint + 0
000000000054  000000000033 R_RISCV_RELAX                        0
000000000064  001d00000013 R_RISCV_CALL_PLT  0000000000000000 putint + 0
000000000064  000000000033 R_RISCV_RELAX                        0
000000000070  001e00000013 R_RISCV_CALL_PLT  0000000000000000 putch + 0
000000000070  000000000033 R_RISCV_RELAX                        0
000000000078  001f00000013 R_RISCV_CALL_PLT  0000000000000000 starttime + 0
000000000078  000000000033 R_RISCV_RELAX                        0
000000000088  000400000011 R_RISCV_JAL       000000000000008c .LBB0_1 + 0
000000000098  000500000011 R_RISCV_JAL       0000000000000150 .LBB0_12 + 0
00000000009c  000600000011 R_RISCV_JAL       00000000000000a0 .LBB0_2 + 0
0000000000a8  000700000011 R_RISCV_JAL       00000000000000ac .LBB0_3 + 0
0000000000b8  000800000011 R_RISCV_JAL       000000000000013c .LBB0_10 + 0
0000000000bc  000900000011 R_RISCV_JAL       00000000000000c0 .LBB0_4 + 0
0000000000c8  000a00000011 R_RISCV_JAL       00000000000000cc .LBB0_5 + 0
0000000000d8  000b00000011 R_RISCV_JAL       0000000000000128 .LBB0_8 + 0
0000000000dc  000c00000011 R_RISCV_JAL       00000000000000e0 .LBB0_6 + 0
000000000114  000d00000011 R_RISCV_JAL       0000000000000118 .LBB0_7 + 0
000000000124  000a00000011 R_RISCV_JAL       00000000000000cc .LBB0_5 + 0
000000000128  000e00000011 R_RISCV_JAL       000000000000012c .LBB0_9 + 0
000000000138  000700000011 R_RISCV_JAL       00000000000000ac .LBB0_3 + 0
00000000013c  000f00000011 R_RISCV_JAL       0000000000000140 .LBB0_11 + 0
00000000014c  000400000011 R_RISCV_JAL       000000000000008c .LBB0_1 + 0
000000000150  002000000013 R_RISCV_CALL_PLT  0000000000000000 stoptime + 0
000000000150  000000000033 R_RISCV_RELAX                        0
00000000015c  001d00000013 R_RISCV_CALL_PLT  0000000000000000 putint + 0
00000000015c  000000000033 R_RISCV_RELAX                        0
000000000168  001e00000013 R_RISCV_CALL_PLT  0000000000000000 putch + 0
000000000168  000000000033 R_RISCV_RELAX                        0
000000000170  001c00000013 R_RISCV_CALL_PLT  0000000000000000 getint + 0
000000000170  000000000033 R_RISCV_RELAX                        0
00000000017c  002100000013 R_RISCV_CALL_PLT  0000000000000000 getch + 0
00000000017c  000000000033 R_RISCV_RELAX                        0
00000000018c  001000000011 R_RISCV_JAL       0000000000000190 .LBB0_13 + 0
00000000019c  001100000011 R_RISCV_JAL       00000000000001c8 .LBB0_16 + 0
0000000001a0  001200000011 R_RISCV_JAL       00000000000001a4 .LBB0_14 + 0
0000000001a4  002100000013 R_RISCV_CALL_PLT  0000000000000000 getch + 0
0000000001a4  000000000033 R_RISCV_RELAX                        0
0000000001ac  001e00000013 R_RISCV_CALL_PLT  0000000000000000 putch + 0
0000000001ac  000000000033 R_RISCV_RELAX                        0
0000000001b4  001300000011 R_RISCV_JAL       00000000000001b8 .LBB0_15 + 0
0000000001c4  001000000011 R_RISCV_JAL       0000000000000190 .LBB0_13 + 0
0000000001cc  001e00000013 R_RISCV_CALL_PLT  0000000000000000 putch + 0
0000000001cc  000000000033 R_RISCV_RELAX                        0
0000000001d4  001900000014 R_RISCV_GOT_HI20  0000000000000000 __stack_chk_guard + 0
0000000001d8  001400000018 R_RISCV_PCREL_LO1 00000000000001d4 .Lpcrel_hi1 + 0
0000000001d8  000000000033 R_RISCV_RELAX                        0
0000000001e8  001500000011 R_RISCV_JAL       0000000000000204 .LBB0_18 + 0
0000000001ec  001600000011 R_RISCV_JAL       00000000000001f0 .LBB0_17 + 0
000000000204  002200000013 R_RISCV_CALL_PLT  0000000000000000 __stack_chk_fail + 0
000000000204  000000000033 R_RISCV_RELAX                        0

Relocation section '.rela.eh_frame' at offset 0xb78 contains 3 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
00000000001c  000200000039 R_RISCV_32_PCREL  0000000000000000 <null> + 0
000000000020  001700000023 R_RISCV_ADD32     000000000000020c <null> + 0
000000000020  000200000027 R_RISCV_SUB32     0000000000000000 <null> + 0

It shows that Clang generate the relocation of  '__stack_chk_guard' and  '__stack_chk_fail'.

It seems to me a bug of Clang packaging because if '-fno-stack-protector' and '-S -emit-llvm' are specified, the diff shows `sspstrong` attributes in LLVM IR:

6c6
< ; Function Attrs: noinline nounwind optnone sspstrong uwtable
---
> ; Function Attrs: noinline nounwind optnone uwtable
148c148
< attributes #0 = { noinline nounwind optnone sspstrong uwtable "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic-rv64" "target-features"="+64bit,+m,+relax,-a,-c,-d,-e,-experimental-smaia,-experimental-ssaia,-experimental-zacas,-experimental-zfa,-experimental-zfbfmin,-experimental-zicond,-experimental-zihintntl,-experimental-ztso,-experimental-zvbb,-experimental-zvbc,-experimental-zvfbfmin,-experimental-zvfbfwma,-experimental-zvkg,-experimental-zvkn,-experimental-zvknc,-experimental-zvkned,-experimental-zvkng,-experimental-zvknha,-experimental-zvknhb,-experimental-zvks,-experimental-zvksc,-experimental-zvksed,-experimental-zvksg,-experimental-zvksh,-experimental-zvkt,-f,-h,-save-restore,-svinval,-svnapot,-svpbmt,-v,-xcvbitmanip,-xcvmac,-xsfcie,-xsfvcp,-xtheadba,-xtheadbb,-xtheadbs,-xtheadcmo,-xtheadcondmov,-xtheadfmemidx,-xtheadmac,-xtheadmemidx,-xtheadmempair,-xtheadsync,-xtheadvdot,-xventanacondops,-zawrs,-zba,-zbb,-zbc,-zbkb,-zbkc,-zbkx,-zbs,-zca,-zcb,-zcd,-zce,-zcf,-zcmp,-zcmt,-zdinx,-zfh,-zfhmin,-zfinx,-zhinx,-zhinxmin,-zicbom,-zicbop,-zicboz,-zicntr,-zicsr,-zifencei,-zihintpause,-zihpm,-zk,-zkn,-zknd,-zkne,-zknh,-zkr,-zks,-zksed,-zksh,-zkt,-zmmul,-zve32f,-zve32x,-zve64d,-zve64f,-zve64x,-zvfh,-zvl1024b,-zvl128b,-zvl16384b,-zvl2048b,-zvl256b,-zvl32768b,-zvl32b,-zvl4096b,-zvl512b,-zvl64b,-zvl65536b,-zvl8192b" }
---
> attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic-rv64" "target-features"="+64bit,+m,+relax,-a,-c,-d,-e,-experimental-smaia,-experimental-ssaia,-experimental-zacas,-experimental-zfa,-experimental-zfbfmin,-experimental-zicond,-experimental-zihintntl,-experimental-ztso,-experimental-zvbb,-experimental-zvbc,-experimental-zvfbfmin,-experimental-zvfbfwma,-experimental-zvkg,-experimental-zvkn,-experimental-zvknc,-experimental-zvkned,-experimental-zvkng,-experimental-zvknha,-experimental-zvknhb,-experimental-zvks,-experimental-zvksc,-experimental-zvksed,-experimental-zvksg,-experimental-zvksh,-experimental-zvkt,-f,-h,-save-restore,-svinval,-svnapot,-svpbmt,-v,-xcvbitmanip,-xcvmac,-xsfcie,-xsfvcp,-xtheadba,-xtheadbb,-xtheadbs,-xtheadcmo,-xtheadcondmov,-xtheadfmemidx,-xtheadmac,-xtheadmemidx,-xtheadmempair,-xtheadsync,-xtheadvdot,-xventanacondops,-zawrs,-zba,-zbb,-zbc,-zbkb,-zbkc,-zbkx,-zbs,-zca,-zcb,-zcd,-zce,-zcf,-zcmp,-zcmt,-zdinx,-zfh,-zfhmin,-zfinx,-zhinx,-zhinxmin,-zicbom,-zicbop,-zicboz,-zicntr,-zicsr,-zifencei,-zihintpause,-zihpm,-zk,-zkn,-zknd,-zkne,-zknh,-zkr,-zks,-zksed,-zksh,-zkt,-zmmul,-zve32f,-zve32x,-zve64d,-zve64f,-zve64x,-zvfh,-zvl1024b,-zvl128b,-zvl16384b,-zvl2048b,-zvl256b,-zvl32768b,-zvl32b,-zvl4096b,-zvl512b,-zvl64b,-zvl65536b,-zvl8192b" }

I also compiled Clang from llvm-project release 17.0.6/18.1.0/18.1.6 with default compile configurations, and I failed to reproduce the problem above with these Clang in my machine.

Last edited by TimeOrange (2024-06-05 05:49:01)

Offline

#2 2024-10-20 00:27:23

operadicmaniac
Member
Registered: 2022-06-10
Posts: 7

Re: Clang create '__stack_chk_guard' reference given '-nostdlib'

This was a nice question, sad to see no response from "experts" and "trustees"

Offline

#3 2024-10-20 11:41:12

Lone_Wolf
Administrator
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 12,903

Re: Clang create '__stack_chk_guard' reference given '-nostdlib'

cross compiling to risc-v is a highly specialised topic, and using clang with nostdlib nostdinc makes it even more specialised.

I expect the number of people with experience related to cross-compiling risc-v using clang among archlinux forum users to be close to zero .
The number of experts will be lower.

OP hasn't visited the forum in over 3 months, closing this topic.


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.

clean chroot building not flexible enough ?
Try clean chroot manager by graysky

Offline

Board footer

Powered by FluxBB