1

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.

3
  • I see you're missing .rodata. Does the string "value of g1 is %d" appear in the file? Commented Aug 14, 2019 at 4:37
  • No there is no string value of g1 is %d in memory file Commented Aug 14, 2019 at 4:41
  • That string probably goes in the .rodata section which is not included in your output file. Commented Aug 15, 2019 at 2:08

1 Answer 1

1
  1. The linker scripts for executables are not that simple. You need to provide all kinds of sections for the application and the operating system to load and run the application.

    Please see the default linker scripts:

    • On Linux they are in /usr/lib/ldscripts - at least with the distribution I tried.
    • On Windows they are in <your-installation-path>\mingw64\x86_64-w64-mingw32\lib\ldscripts - at least with the installation I tried.
  2. You put hard coded addresses like 0x0000000000400410 for .text in your script. You can't do this, because our casual operating systems decide on their own where to load the sections.

So, if you like to play with linker scripts, I'd suggest to copy one of the system's linker scripts and edit that. Which one you need, I don't know.

There might be some option to let the linker show which one it uses. You can start with a standard compile command and the option -v which will give you a lot of details. But you will need to read the linker's documentation, for sure.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes , I tried with default linker scripts . I am able to execute my program correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.