I use nasm and I have the gnu linker. What I want to do is link my main program file with a file containing procedures and then output a flat binary file. Since I can't use nasm to make binary files with EXTERN's in the source I figured I could assemble them to intermediate object files and use ld to link them. I tried to assemble both files aout type object files and use the command:

ld -oformat=binary -e start -o main.bin main.o procs.o

Problem is, the file ends up over 4kb when it should be exactly 512 bytes.

I just want to link with the procedures I use in my source and not the entire file.

tyvm in advance

Recommended Answers

All 10 Replies

what is main.bin? I never heard of linking a *.bin file with *.o files.

>> Since I can't use nasm to make binary files with EXTERN's
And why not? Its the linker that resolves the externs, not the assembler.

Because if I try to assembly a file that has extern's using nasm with flat binary output it says "error: binary output format does not support external references". And main.bin is supposed to be the output file name.

According to nasm documentation PDF the EXTERN keyword should work. Maybe you should read it more carefully -- you do have that documentation don't you?

The documentation was the first thing I checked. It says in section 5.4 "Not every object-file
format can support external variables: the bin format cannot". Plus you can only specify one input file with nasm so it couldn't do what I want anyway...

>>I just want to link with the procedures I use in my source and not the entire file
does that mean you don't want everything that is in procs.o? The linker will give you everything in a *.o file whethere used or not. Some linkers are however smarter and will toss out unused code. I suspect your linker is not one of them.

Ok sorry i'm too used too using gui's that do all the compiling and linking for me... Do I need to make my procedures into a library file to do this?? If so can you tell me how to make a library file out of the procs.o file?

Thanks again

Yes, create a library and split that procs source file into as many files as there are functions in it then compile each separately into its own *.o file then put all the *.o files into the library.

Ah that makes sense.

One more thing that's bothering me... Like I said in my first post ld is making the output file over 4kb when it should be much smaller. What kind of information is being added to the output and how can I get rid of it?

>>What kind of information is being added to the output and how can I get rid of it?
It adds all the code that's in procs.o and main.o. To my knowledge it doesn't add any other code like compilers for other languages do. You would have to load the bin file into a binary editor such as debug.exe in MS-Windows to find out for sure.

I just used a binary compare program called Bcf and it appears ld isn't making raw binary afterall because the ld output file starts with 'MZ' and then says something about a stubb program. Guess i'll have to read up on ld some more to see what i'm doing wrong.

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.