-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
A segmentation fault occurs when running a simple ELF file #461
Comments
Avoid uploading screenshots that contain only text. Instead, use Markdown formatting to present command lists, source code, and any text-based content. This approach is more accessible for individuals with visual impairments. Please update the issue with steps to reproduce the problem you're encountering, including a minimal piece of source code that illustrates the issue. Why are you using GCC-4.8.5? |
This is the default gcc version in my system which is centos 7. Do you think the issue is caused by this old version of gcc? |
I attempted to reproduce with the following steps:
int mul(int a, int b)
{
a = 0x12345678;
b = 0x87654321;
return a * b;
}
int main(int argc, char *argv[])
{
return mul(argc, argc + 1) % 128;
}
$ riscv-none-elf-gcc -o mul.elf -march=rv32i -mabi=ilp32 mul.c
$ python3 -c "print(0x12345678 * 0x87654321 % 128)"
120 So far, everything works as expected. |
Although this is probably not related to causing a segmentation fault, this code will cause signed integer overflow and result in undefined behavior. |
My elf generation is different from yours, my c code doesn't have main() and the start point is customized by linker script.
int mul(int a, int b)
{
a=0x12345678;
b=0x87654321;
return a * b;
}
sp_setup: li sp, 32768 # for setting up the stack pointer
riscv-none-embed-objdump -D mul.elf:
|
You should specify the entry point that can jump into the |
I specified the entry point which is 0x0. The start of mul() is placed at 0x4. 0x0 is just an instruction which load sp with value 32768. So that shouldn't cause the problem. |
I have tried it out and I get segmentation fault as well. After investigating, the root cause is generated when emulating
This if conditional statement causes side effect if the PC is 0x0 because the initial value of The trivial solution is initializing |
If the ra(return address) is 0x0, the LOOKUP_OR_UPDATE_BRANCH_HISTORY_TABLE will bahave abnormally since calloc initialize ir->branch_table->PC[i] to 0x0. The 0x0 address might be not yet translated to a valid block, thus ir->branch_table->target[i] might be NULL, calling a NULL function pointer cause segmentation fault. It can be solved by initializing ir->branch_table->PC will other value than 0x0. Here, I choose unsigned integer of -1. Close sysprog21#461
Note that your linker script makes the program trapping into infinite loop even no segmentation fault. |
If the ra(return address) is 0x0, the LOOKUP_OR_UPDATE_BRANCH_HISTORY_TABLE will bahave abnormally since calloc initializes ir->branch_table->PC[i] to 0x0. The address 0x0 might be not yet translated to a valid block, thus ir->branch_table->target[i] might be NULL, accessing a NULL pointer causes segmentation fault. It can be solved by initializing ir->branch_table->PC with other value than 0x0. Here, I choose unsigned integer of -1. Close sysprog21#461
If the ra(return address) is 0x0, the LOOKUP_OR_UPDATE_BRANCH_HISTORY_TABLE will bahave abnormally since calloc initializes ir->branch_table->PC[i] to 0x0. The address 0x0 might be not yet translated to a valid block, thus ir->branch_table->target[i] might be NULL, accessing a NULL pointer causes segmentation fault. It can be solved by initializing ir->branch_table->PC with other value than 0x0. Here, I choose unsigned integer of -1. Close sysprog21#461
I ran
hello.elf
and it is fine.But when I tried to run my own mul.elf, segmentation fault happened.
I tried to do some simple gdb debugging to figure out why but just gave up because my unfamiliarity with the emulator.
Can someone explain the cause?
Emulator build configuration:
Every ENABLE_* in Makefile are disabled.
hello.elf
result:mul.elf
result:./rv32emu mul.elf Segmentation fault (core dumped)`
The objdump of mul.elf:
mul_objdump.txt
c source code:
It is just an integer multiply function.
The text was updated successfully, but these errors were encountered: