0

I have a very simple program (simple.c):

#include <stdio.h>
int main(){
  int a = 4;
  return 0;
 }

I am trying to use a the following linker script (MEMORY):

MEMORY
{
m_text : ORIGIN = 0x0000000000400000, LENGTH = 0x0001FBF0
m_data : ORIGIN = 0x0000000000600000, LENGTH = 0x00004000
}

SECTIONS
 {
  .text :
  {
    *(.text)   /* Program Code */
 } > m_text

  .data : { *(.data) } > m_data
  .bss : { *(.bss) } > m_data
  }

I am using following commands to compile and link:

gcc -c simple.c

ld -T MEMORY -o simple -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o /usr/lib/x86_64-linux-gnu/crtn.o simple.o

I am getting the following ERROR:

ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
.
.
. 
ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x12): undefined reference to `__libc_csu_fini'
/usr/lib/x86_64-linux-gnu/crt1.o: In function `_start':  
(.text+0x19): undefined reference to `__libc_csu_init'
/usr/lib/x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x25): undefined reference to `__libc_start_main'
simple.o: In function `main':
simple.c:(.text+0xa): undefined reference to `puts'

If I try to use :

ld -T MEMORY4 -o simple -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o /usr/lib/x86_64-linux-gnu/crtn.o simple.o -lc

It gives the ERROR

ld: cannot find -lc

Any suggestions? All I want to do is place my simple.c into some different memory region then default.

Community
  • 1
  • 1
G Gill
  • 1,087
  • 1
  • 12
  • 24

2 Answers2

0

There are a couple of problems, but the main one is with your linker script doesn't have a .debug_info or .debug_line section.

Chris Desjardins
  • 2,631
  • 1
  • 23
  • 25
0

You can check severals things :

  • To resolve this, add the -lc option to the linker command to link against the C library. However, you mentioned that you encountered an error when using this option (ld: cannot find -lc). This error usually occurs when the C library (libc.so) is missing from your system.

  • Ensure that the libc.so library is present in the standard library directories. You can try installing the required libc development package using your package manager.

  • If the C library (libc.so) is installed but not in the default library search path, you can specify the path using the -L flag followed by the directory path.

  • For example, if the libc.so library is located in /path/to/library, you can modify your linker command as follows:

    ld -T MEMORY -o simple -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o /usr/lib/x86_64-linux-gnu/crtn.o simple.o -L/path/to/library -lc
    

Make sure to double-check the paths and versions of the required files to ensure they match your system. If the issues persist, it might be helpful to us to provide more information about your system :the libraries versions you're using, or even the version of GCC.

neo-jgrec
  • 167
  • 8