I am new to concept of linker scripts . So I was trying myself with simple c program and linker scripts and memory file.
I tried to pass a custom linker script for a c program , When I looked into the memory file that got generated , I am able to see the variable that is initialized is placed at the address mentioned in the linker scripts.
But the problem is I cannot execute the executable that got generated for the c program
This is my c file simpleHello.c
#include<stdio.h>
int g1 = 100;
int main(int argc , char *argv[])
{
printf("value of g1 is %d",g1);
return 0;
}
This is my linker script fie name linker.ld
ENTRY(main)
SECTIONS
{
. = ALIGN(4);
. = 0x0000000000400410;
.text : { *(.text) }
. = 0x0000000000601038;
.data : { *(.data) }
.bss : { *(.bss) }
}
I am running the below command
clang simpleHello.c -g -Wl,-emain,-Map=o.map,-Tlinker.ld -ffreestanding -nostartfiles -nodefaultlibs -L/usr/lib/x86_64-linux-gnu -lc -o simple
If i run ./simple
Segmentation fault (core dumped)
So i debugged the executable with gdb and I got below this.
gdb ./simple
Reading symbols from ./simple...done.
(gdb) b main
Breakpoint 1 at 0x40041f: file simpleHello.c, line 9.
(gdb) r
Starting program: Desktop/Memory-Map/programs/simple
Breakpoint 1, main (argc=-134225560, argv=0x1) at simpleHello.c:9
9 printf("value of g1 is %d",g1);
(gdb) bt
#0 main (argc=-134225560, argv=0x1) at simpleHello.c:9
(gdb)
So the probelm is with argc and argv or with linker file that I passed.?
Kindly suggest some solution for this issue.
value of g1 is %din memory file