C to NASM - lnkflat problem

  • Thread starter Marcin Balcerzak
  • Start date
M

Marcin Balcerzak

Hi,

after some googling I've found the post, in which there was a recipe how
to convert C to NASM. I'd copied and compiled remnum.c and wrote
Makefile like:
x.asm: x.bin
ndisasm -u x.bin | ./remnum > x.asm

x.bin: x.o
lnkflat -o x.bin x.o

x.o: x.c
gcc -Wall -c -ffreestanding -O2 -s x.c

clean:
rm -f x.o x.bin

and x.c is:

#include <stdio.h>
int main()
{
printf("Test\n");
return 0;
}

And there is a problem :)
I obtain:
gcc -Wall -c -ffreestanding -O2 -s x.c
lnkflat -o x.bin x.o
x.o(.text+0x11): In function `main':
: undefined reference to `printf'
error linking file x.bin
make: *** [x.bin] Error 2

Apparently, it doesn't recognize printf. So I tried:
gcc -Wall -c -ffreestanding -O2 -s x.c -I /usr/lib
or gcc -Wall -c -ffreestanding -O2 -s x.c -I /usr/include
and then even:

lnkflat -o x.bin x.o -l libc
and when reported that libc not found:
lnkflat -o x.bin x.o -l libc -L /usr/lib
where in /usr/lib I've got libc.a and libc.so
that gives me back:
lnkflat -o x.bin x.o -l libc -L /usr/lib
ld: cannot find -llibc
error linking file x.bin
make: *** [x.bin] Error 2

(** to everyone who don't know/trust/heard of remnum.c - listing **)
#include <stdio.h>

int main()
{
char s[4096];

while(fgets(s,4096,stdin)!=NULL)
{
if(strlen(s)>28)
printf("%s",s+28);
}

return 0;
}
(** end of remnum.c **)

My invention is currently close to death.
What should I do to convert C programs (using scanf, printf, stdio.h,
stdlib.h, string.h, and runtime parameters: char *argv[], int argc)?
A big THANK YOU to everybody who will write something helpful.
 
J

Jonathan Bartlett

And there is a problem :)
I obtain:
gcc -Wall -c -ffreestanding -O2 -s x.c
lnkflat -o x.bin x.o
x.o(.text+0x11): In function `main':
: undefined reference to `printf'
error linking file x.bin
make: *** [x.bin] Error 2

This is offtopic for c.l.c, but I think I know the answer.
Apparently, it doesn't recognize printf.

Specifically, the linker doesn't recognize printf. "Undefined
reference" errors are from the linker.
So I tried:
gcc -Wall -c -ffreestanding -O2 -s x.c -I /usr/lib
or gcc -Wall -c -ffreestanding -O2 -s x.c -I /usr/include

Yep, it's not a gcc problem.
lnkflat -o x.bin x.o -l libc
and when reported that libc not found:

You're getting close. I haven't used lnkflat, but since lnkflat uses ld
internally, that is probably supposed to be "-lc" (the "lib" part of
libc is assumed -- your command line was looking for liblibc.so or
liblibc.a).

Anyway, try that and see if it works.

Jon
 
M

Marcin Balcerzak

This is offtopic for c.l.c, but I think I know the answer.

I'm sorry...
You're getting close. I haven't used lnkflat, but since lnkflat uses
ld internally, that is probably supposed to be "-lc" (the "lib" part
of libc is assumed -- your command line was looking for liblibc.so or
liblibc.a).

Anyway, try that and see if it works.

Many experiments I've done...
Generally, if I try to link libc - for instance by
lnkflat -o x.bin x.o -L /usr/lib -l c
I've got:

gcc -Wall -c -ffreestanding -O2 -s x.c -lc -L /usr/lib
lnkflat -o x.bin x.o -L /usr/lib -l c
gcc: -lc: linker input file unused because linking not done
/usr/lib/libc.a(printf.o)(.text+0x1a): In function `_IO_printf':
: undefined reference to `_GLOBAL_OFFSET_TABLE_'
/usr/lib/libc.a(vfprintf.o)(.text+0x10): In function `_i18n_number_rewrite':
: undefined reference to `_GLOBAL_OFFSET_TABLE_'
/usr/lib/libc.a(vfprintf.o)(.text+0x295): In function `vfprintf':
: undefined reference to `_GLOBAL_OFFSET_TABLE_'
/usr/lib/libc.a(vfprintf.o)(.text+0x3b33): In function `printf_unknown':
: undefined reference to `_GLOBAL_OFFSET_TABLE_'
/usr/lib/libc.a(vfprintf.o)(.text+0x3ef2): In function `group_number':
: undefined reference to `_GLOBAL_OFFSET_TABLE_'
/usr/lib/libc.a(vfprintf.o)(.text+0x402c): more undefined references to
`_GLOBAL_OFFSET_TABLE_' follow
make: *** [x.bin] Error 5
unable to link flat file
(perhaps installed version of ld doesn't support output format "binary")

I've also tried to link some others, additional libraries like libgcc with
no result, alas.
Has anyone have any ideas? (Thanx :) )
Generally I want to obtain NASM code from C. Without disassembling the
executable, thus the ways are to create flat binary from C and disassemble
it or produce by gcc at&t or intel-gas code and convert it to NASM. A2I
doesn't work - it results with some series of warnings and the output is not
accepted by NASM. Or mayby there is some C compiler that produces NASM code?
I'm sure many people have encountered the problem of converting C to NASM.
How to do this? :)
 
K

Keith Thompson

Marcin Balcerzak said:
And there is a problem :)
I obtain:
gcc -Wall -c -ffreestanding -O2 -s x.c
lnkflat -o x.bin x.o
x.o(.text+0x11): In function `main':
: undefined reference to `printf'
error linking file x.bin
make: *** [x.bin] Error 2

Apparently, it doesn't recognize printf. So I tried:
gcc -Wall -c -ffreestanding -O2 -s x.c -I /usr/lib
or gcc -Wall -c -ffreestanding -O2 -s x.c -I /usr/include
and then even:
[snip]

If the "-ffreestanding" option means what the name implies, it tells
the compiler to assume a freestanding environment, i.e., one in which
most of the standard library may not exist. (A freestanding
environment is allowed, but not required, to provide some subset of
the standard library.)

As far as the language is concerned, you can use a freestanding
implementation *or* you can use printf. Take your pick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top