I have two source files (one is written in NASM assembly - try.asm
, another in plain C - tryasmc.c
). I have a C function func()
in the .c file which I have to link to the extern func
in the assembly file.
The try.asm
file :
section .text
extern func
global main
main:
push ebp
mov ebp, esp
call func
pop ebp
ret
The tryasmc.c
file :
void func()
{
// doesn't really do anything
}
The linker script (mylink.ld):
ENTRY(main)
OUTPUT_FORMAT(binary)
SECTIONS
{
.text :
{
tryasm.o (.text)
tryasmc.o (.text)
}
}
I want to separately compile them and link them (via a linker script) to produce a flat binary
file. I did the following:
nasm -f elf32 -o tryasm.o try.asm
(compiling the assembly code)gcc -c -g -m32 -o tryasmc.o tryasmc.c
(compiling the C code)gcc -m32 tryasm.o tryasmc.o -o tryasm -T mylink.ld
I am getting the following error:
/usr/lib32/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x18): undefined reference to `__init_array_end'
/usr/lib32/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x23): undefined reference to `__init_array_start'
/usr/lib32/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x47): undefined reference to `__init_array_start'
/usr/bin/ld: tryasm: hidden symbol `__init_array_end' isn't defined
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
As I stated earlier, I want to produce flat binary file tryasm
which will contain main
as the entry point and func
as a subroutine. How can I acieve that ? And why am I getting these errors ?
Note: I am using a 64 bit Linux (ubuntu-16)
machine but compiling codes as 32 bit elf format
using gcc
and nasm
.
Purpose: I am writing a little OS in assembly, so all I need is flat binary
to load onto RAM.