Misaligned instruction access -- spike pk hello - riscv

I am receiving a 'Misaligned instruction access' exception when testing my RISC-V toolchain. The content of the hello.c match the example provided in the README file.
Has anyone encountered this issue?
$ riscv-gcc -o hello hello.c
$ spike pk hello
z 0000000000000000 ra 0000000000000000 sp 0000000000000000 gp 0000000000000000
tp 0000000000000000 t0 0000000000000000 t1 0000000000000017 t2 0000000000003514
s0 0000000000000000 s1 000000007f800000 a0 0000000000000000 a1 0000000000000032
a2 ffffffffffffffff a3 0000000080000000 a4 000000007f800000 a5 00000000000037c0
a6 0000000000000017 a7 0000000000003514 s2 0000000000000000 s3 0000000000000000
s4 0000000000000000 s5 0000000000000000 s6 0000000000000000 s7 0000000000000000
s8 0000000000000000 s9 000000000000d028 sA 0000000000800000 sB 0000000000800000
t3 00000000ff800000 t4 0000000000000000 t5 000000007f800000 t6 0000000000003f3c
sr 0000000000000000 pc 00000000ff800000 va 0000000000000000 insn 00000000
Misaligned instruction access!

Tao J answers this question :
http://tao-j.com/2014/12/risc-v-iii-build-tool-sets-and-linux/
Quoting:
The commit of riscv-tools which rocket-chip used, is tested and valid.
Newest commit of riscv-tools may be broken. So it’s recommended that
not to clone a riscv-tools separately. Please use the repository
included in the rocket-chip , which points to the right commit.

An errant commit was made to the riscv-tools super-repository that incorrectly pointed to a new riscv-pk submodule commit that contains an incompatibly calling convention.
Your problem should be resolved now (https://github.com/ucb-bar/riscv-pk/issues/6).

Related

Getting bad map relo error while trying to attach an eBPF program to tc

When trying to run the command,
sudo tc filter add dev veth0 egress bpf direct-action obj a.o sec classifier
I'm getting this error,
libbpf: prog 'pdm_main': bad map relo against '.rodata.str1.1' in section '.rodata.str1.1'
ERROR: opening BPF object file failed
Unable to load program
The sections in my object file,
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000000 0000000000000000 0000000000000000 00000040 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 classifier 00000638 0000000000000000 0000000000000000 00000040 2**3
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
2 .maps 00000020 0000000000000000 0000000000000000 00000678 2**3
CONTENTS, ALLOC, LOAD, DATA
3 .rodata.str1.1 0000002c 0000000000000000 0000000000000000 00000698 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .rodata.str1.16 00000036 0000000000000000 0000000000000000 000006d0 2**4
CONTENTS, ALLOC, LOAD, READONLY, DATA
5 license 00000004 0000000000000000 0000000000000000 00000706 2**0
CONTENTS, ALLOC, LOAD, DATA
6 .debug_loclists 00000105 0000000000000000 0000000000000000 0000070a 2**0
CONTENTS, READONLY, DEBUGGING, OCTETS
7 .debug_abbrev 000001c9 0000000000000000 0000000000000000 0000080f 2**0
CONTENTS, READONLY, DEBUGGING, OCTETS
8 .debug_info 0000080f 0000000000000000 0000000000000000 000009d8 2**0
CONTENTS, RELOC, READONLY, DEBUGGING, OCTETS
9 .debug_str_offsets 000002a4 0000000000000000 0000000000000000 000011e7 2**0
CONTENTS, RELOC, READONLY, DEBUGGING, OCTETS
10 .debug_str 000006ec 0000000000000000 0000000000000000 0000148b 2**0
CONTENTS, READONLY, DEBUGGING, OCTETS
11 .debug_addr 00000030 0000000000000000 0000000000000000 00001b77 2**0
CONTENTS, RELOC, READONLY, DEBUGGING, OCTETS
12 .BTF 00001037 0000000000000000 0000000000000000 00001ba8 2**2
CONTENTS, RELOC, READONLY
13 .BTF.ext 00000470 0000000000000000 0000000000000000 00002be0 2**2
CONTENTS, RELOC, READONLY
14 .eh_frame 00000030 0000000000000000 0000000000000000 00003050 2**3
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
15 .debug_line 00000220 0000000000000000 0000000000000000 00003080 2**0
CONTENTS, RELOC, READONLY, DEBUGGING, OCTETS
16 .debug_line_str 000000f8 0000000000000000 0000000000000000 000032a0 2**0
CONTENTS, READONLY, DEBUGGING, OCTETS
How to fix this? When i don't use eBPF map in my code it doesn't give the same error.

Error Occured while starting pgadmin docker image in Ubuntu

taneesha#TANEESHA:~$ sudo docker start 3e0033eb5b5c
[sudo] password for taneesha:
fatal error: slice bounds out of range
goroutine 0 [idle]:
runtime: unexpected return pc for runtime.sigtramp called from 0x7ff4b65a73c0
stack: frame={sp:0xc000009a68, fp:0xc000009ac0} stack=[0xc000002000,0xc00000a000)
000000c000009968: 0000000000000000 0000000000000000
000000c000009978: 0000000000000101 0000000000000000
000000c000009988: 0000000000000000 0000000000000000
000000c000009998: 0000000000000000 0000000000000000
000000c0000099a8: 000000c0000099f0 0000000000000000
000000c0000099b8: 000000c000000000 0000000000000000
000000c0000099c8: 0000000000000000 000000c000009bf0
000000c0000099d8: 000000c000009ac0 000000c000009a58
000000c0000099e8: 000055aff7014ae5 <runtime.sigtrampgo+421> 000000c000000017
000000c0000099f8: 000000c000009bf0 000000c000009ac0
000000c000009a08: 000000c000000180 0000000000000000
000000c000009a18: 0000000000000000 0000000000000000
000000c000009a28: 0000000000000000 0000000000000000
000000c000009a38: 0000000000000000 000000c000000180
000000c000009a48: 000000c000009bf0 000000c000009ac0
000000c000009a58: 000000c000009ab0 000055aff7037663 <runtime.sigtramp+67>
000000c000009a68: <0000000000000017 000000c000009bf0
000000c000009a78: 000000c000009ac0 0000000000000009
000000c000009a88: 0000000000000010 0000000000000003
000000c000009a98: 000000000000000d 000000c000009ab0
000000c000009aa8: 0000000000000000 000000c00059f108
000000c000009ab8: !00007ff4b65a73c0 >0000000000000007
000000c000009ac8: 0000000000000000 000000c000002000
000000c000009ad8: 0000000000000000 0000000000008000
000000c000009ae8: 0000000000000000 000055aff8864ae0
000000c000009af8: 000000000000352d 000000000000352c
000000c000009b08: 000000000000000d 0000000000000003
000000c000009b18: 0000000000000010 0000000000000009
000000c000009b28: 000055aff99da678 0000000000000010
000000c000009b38: 000000c00059f108 0000000000000000
000000c000009b48: 000055aff99da678 000055aff7b70320 fatal error: index out of range
panic during panic
goroutine 0 [idle]:
runtime: unexpected return pc for runtime.sigtramp called from 0x7ff4b65a73c0
stack: frame={sp:0xc000009a68, fp:0xc000009ac0} stack=[0xc000002000,0xc00000a000)
000000c000009968: 0000000000000000 0000000000000000
000000c000009978: 0000000000000101 0000000000000000
000000c000009988: 0000000000000000 0000000000000000
000000c000009998: 0000000000000000 0000000000000000
000000c0000099a8: 000000c0000099f0 0000000000000000
000000c0000099b8: 000000c000000000 0000000000000000
000000c0000099c8: 0000000000000000 000000c000009bf0
000000c0000099d8: 000000c000009ac0 000000c000009a58
000000c0000099e8: 000055aff7014ae5 <runtime.sigtrampgo+421> 000000c000000017
000000c0000099f8: 000000c000009bf0 000000c000009ac0
000000c000009a08: 000000c000000180 0000000000000000
000000c000009a18: 0000000000000000 0000000000000000
000000c000009a28: 0000000000000000 0000000000000000
000000c000009a38: 0000000000000000 000000c000000180
000000c000009a48: 000000c000009bf0 000000c000009ac0
000000c000009a58: 000000c000009ab0 000055aff7037663 <runtime.sigtramp+67>
000000c000009a68: <0000000000000017 000000c000009bf0
000000c000009a78: 000000c000009ac0 0000000000000009
000000c000009a88: 0000000000000010 0000000000000003
000000c000009a98: 000000000000000d 000000c000009ab0
000000c000009aa8: 0000000000000000 000000c00059f108
000000c000009ab8: !00007ff4b65a73c0 >0000000000000007
000000c000009ac8: 0000000000000000 000000c000002000
000000c000009ad8: 0000000000000000 0000000000008000
000000c000009ae8: 0000000000000000 000055aff8864ae0
000000c000009af8: 000000000000352d 000000000000352c
000000c000009b08: 000000000000000d 0000000000000003
000000c000009b18: 0000000000000010 0000000000000009
000000c000009b28: 000055aff99da678 0000000000000010
000000c000009b38: 000000c00059f108 0000000000000000
000000c000009b48: 000055aff99da678 000055aff7b70320 fatal error: index out of range
stack trace unavailable

Getting "undefined reference to `memcpy#GLIBC_2.14'" on Ubuntu 18.04.2 LTS

I am running Ubuntu 18.04.2 LTS and trying to build some code using gcc and getting the following error:
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../x86_64-linux-gnu/lib/../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/lib64/libgcc_s.so.1: undefined reference to `memcpy#GLIBC_2.14'
collect2: error: ld returned 1 exit status
I believe that the problem does not lie in the project but the object file that is trying to use a GLIBC version unavailable on my system but is part of my system.
The object file is /usr/x86_64-linux-gnu/lib/crt1.o part of libc6-dev-amd64-cross version 2.27-3ubuntu1cross1.1.
I am not too familiar with gcc and it seems that I cannot just pull a libc 2.14 and get it to build.
I am stuck.
What should I check? Do I need to use an option for gcc? Or perhaps is this a problem in my distribution?
More info:
# readlink -f /usr/lib/gcc/x86_64-linux-gnu/7/../../../../x86_64-linux-gnu/lib/../lib/crt1.o
/usr/x86_64-linux-gnu/lib/crt1.o
# file /usr/x86_64-linux-gnu/lib/crt1.o
/usr/x86_64-linux-gnu/lib/crt1.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), for GNU/Linux 3.2.0, not stripped
# apt-file search /usr/x86_64-linux-gnu/lib/crt1.o
libc6-dev-amd64-cross: /usr/x86_64-linux-gnu/lib/crt1.o
# dpkg -l | grep libc6-dev-amd64-cross
ii libc6-dev-amd64-cross 2.27-3ubuntu1cross1.1 [...]
# objdump -t /usr/x86_64-linux-gnu/lib/crt1.o
/usr/x86_64-linux-gnu/lib/crt1.o: file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 l d .note.ABI-tag 0000000000000000 .note.ABI-tag
0000000000000000 l d .rodata.cst4 0000000000000000 .rodata.cst4
0000000000000000 l d .eh_frame 0000000000000000 .eh_frame
0000000000000000 l d .data 0000000000000000 .data
0000000000000000 l d .bss 0000000000000000 .bss
0000000000000000 l d .note.GNU-stack 0000000000000000 .note.GNU-stack
0000000000000000 *UND* 0000000000000000 __libc_csu_fini
0000000000000030 g F .text 0000000000000002 .hidden _dl_relocate_static_pie
0000000000000000 g F .text 000000000000002b _start
0000000000000000 *UND* 0000000000000000 __libc_csu_init
0000000000000000 *UND* 0000000000000000 main
0000000000000000 w .data 0000000000000000 data_start
0000000000000000 *UND* 0000000000000000 _GLOBAL_OFFSET_TABLE_
0000000000000000 g O .rodata.cst4 0000000000000004 _IO_stdin_used
0000000000000000 *UND* 0000000000000000 __libc_start_main
0000000000000000 g .data 0000000000000000 __data_start
# gcc --version
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
[...]
# ldd --version
ldd (Ubuntu GLIBC 2.27-3ubuntu1) 2.27
[...]
Edit 1: I have checked out this question but as crt1.o is not an object I compiled but part of a system package installed by Ubuntu, I think I am unable to re-compile it. But I might be wrong, I am not really familiar with gcc.

Editing an ELF executable to reduce it's size

I'm trying to drop a C program down to a size < 1kb. I'm getting close, but I'm stuck on editing my ELF executable. My program, main.c looks like:
#include<unistd.h>
#include<sys/syscall.h>
void _start() {
const char msg [] = "Hello World!";
syscall(SYS_write, 0, msg, sizeof(msg)-1);
syscall(SYS_exit, 0);
}
I'm compiling it with
gcc -nostdlib -s -O3 -o main main.c /usr/lib/path/to/libc.a
Then I strip it. But If I did an objdump on it before stripping it I see
main: file format elf64-x86-64
SYMBOL TABLE:
0000000000400158 l d .note.gnu.build-id 0000000000000000 .note.gnu.build-id
0000000000400180 l d .text 0000000000000000 .text
0000000000400214 l d .eh_frame_hdr 0000000000000000 .eh_frame_hdr
0000000000400238 l d .eh_frame 0000000000000000 .eh_frame
0000000000601000 l d .tbss 0000000000000000 .tbss
0000000000000000 l d .comment 0000000000000000 .comment
0000000000000000 l df *ABS* 0000000000000000 main.c
0000000000000000 l df *ABS* 0000000000000000
00000000004001d0 g F .text 0000000000000026 syscall
0000000000000000 g .tbss 0000000000000004 errno
0000000000400203 g .text 0000000000000000 __syscall_error_1
0000000000400180 g F .text 0000000000000048 _start
0000000000000000 g .tbss 0000000000000004 __libc_errno
0000000000400200 g F .text 0000000000000013 __syscall_error
0000000000601000 g .eh_frame 0000000000000000 __bss_start
0000000000601000 g .eh_frame 0000000000000000 _edata
0000000000000000 *UND* 0000000000000000 _GLOBAL_OFFSET_TABLE_
0000000000601000 g .eh_frame 0000000000000000 _end
It seems like there's some stuff I can remove to manually reduce the size of the executable? note: I know this is not something I'd actually do, but I'm simply trying to remove any boilerplate existing.
What would I remove from the executable, main, to reduce it's size? And how could I do that?
side note: I've already read this and this article. No need to link them. I am purposely choosing to stay with C
Simple stuff
You can remove quite a few useless bits with:
-fno-asynchronous-unwind-tables -Qn;
using a custom linker script -rlinker_script.
I get a working binary of 992 bytes with this (after strip).
Linker script
Let's look at the sections (before stripping):
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .note.gnu.build-i NOTE 0000000000400120 00000120
0000000000000024 0000000000000000 A 0 0 4
[ 2] .text PROGBITS 0000000000400150 00000150
0000000000000090 0000000000000000 AX 0 0 16
[ 3] .eh_frame PROGBITS 00000000004001e0 000001e0
0000000000000048 0000000000000000 A 0 0 8
[ 4] .tbss NOBITS 0000000000601000 00000228
0000000000000004 0000000000000000 WAT 0 0 4
[ 5] .shstrtab STRTAB 0000000000000000 000003e7
0000000000000044 0000000000000000 0 0 1
[ 6] .symtab SYMTAB 0000000000000000 00000228
0000000000000168 0000000000000018 7 6 8
[ 7] .strtab STRTAB 0000000000000000 00000390
0000000000000057 0000000000000000
From program header 5, everything is stripped but we gave two comparatively useless sections which are not stripped: .note.gnu.build-id and .eh_frame. The .eh_frame was disabled in the compiler but some .eh_frame is contributed from the static libc.
We can completely get rid of the .eh_frame and .note.gnu.build-id sections with a custom linker script (gcc -T linker_script).
First, we get the default linker script:
gcc test.c -Wl,--verbose
We remove those lines:
.eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) }
.eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) }
.note.gnu.build-id : { *(.note.gnu.build-id) }
and modify this line:
/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) *(.note.gnu.build-id) *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) *(.eh_frame) *(.eh_frame.*) }
I get 664 bytes using this.
Additional options
Other solutions for a reduced size:
optimize for size (-Os);
32 bit compilation (-m32).
With all of this, I get a binary with 760 bytes without the custom linker script and 488 bytes with the modified linker script.
Get rid of errno
There are quite few "useless" things remaining (such as errno handling and the TLS) that could be removed.
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 080480a0 0000a0 00008e 00 AX 0 0 16
[ 2] .tbss NOBITS 08049130 000130 000004 00 WAT 0 0 4
[ 3] .shstrtab STRTAB 00000000 000257 000027 00 0 0 1
[ 4] .symtab SYMTAB 00000000 000130 0000d0 10 5 4 4
[ 5] .strtab STRTAB 00000000 000200 000057 00 0 0 1
(Everything beginning with section 3 is stripped.)
By writing our own syscall code we could get rid of errno handling. We would remove:
4 bytes of .symtab;
errno related instructions.
But doing this involves using (inline) assembly.

Program running on simulator but not on FPGA

I'm a bit newbie with RISCV processor and this is really anoying me
I've created a .C program for RISCV using the tutorial here http://riscv.org/download.html#tab_tools at "Testing Your Toolchain".
If I use in my computer
spike pk myhello
I get "Hello world!" as expected but if I run it on mi FPGA using this command
./fesvr-zynq pk myhello
I get
z 0000000000000000 ra 0000000000010030 s0 ffffffffffffffc0
s1 0000000000015290 s2 0000000000000000 s3 0000000000014b60
s4 0000000000014b60 s5 0000000000000000 s6 0000000000000000
s7 0000000000000000 s8 0000000000000000 s9 0000000000010258
sA 0000000000000000 sB 0000000000000000 sp 000000000ff7fb40
tp 0000000000000000 v0 0000000000000000 v1 0000000000000000
a0 0000000000000000 a1 0000000000000000 a2 0000000000000000
a3 0000000000000000 a4 0000000000000000 a5 0000000000000000
a6 0000000000000000 a7 0000000000000000 t0 0000000000000000
t1 0000000000000000 t2 0000000000000000 t3 0000000000000000
t4 0000000000000000 t5 0000000000000000 sr 00000000a00000e9
pc 0000000000010448 va 000007fffffffff0 insn ffffffff
User store segfault # 0x000007fffffffff0
Instead getting "hello world!" like the included example in the precompiled-image at SDCARD.
I've tried to boot linux but I can't send my program to the FPGA when the OS is launched.
Thanks a lot
As pointed by user2548418, updating "pk" package solved the problem (and creating again the image with it inside).
Copying files with scp when linux kernel is booted is not currently available, but they can be loaded into the system following the instructions at "Creating a Root Disk Image" in RISCV tools, copying the files into the folders created by mkdir.

Resources