linker scripts and link to bin?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

linker scripts and link to bin?

Post by Sam111 »

Ok , I am trying to completely understand linker scripts.

Say I have this

Code: Select all

/* Link.ld -- Linker script for the kernel - ensure everything goes in the */
/*            Correct place.  */
/*            Original file taken from Bran's Kernel Development */
/*            tutorials: http://www.osdever.net/bkerndev/index.php. */

ENTRY(start)
SECTIONS
{
  .text 0x100000 :
  {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
  }

  .data :
  {
     data = .; _data = .; __data = .;
     *(.data)
     *(.rodata)
     . = ALIGN(4096);
  }

  .bss :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }

  end = .; _end = .; __end = .;
} 
These are some of my problems

question 1) Are linker scripts only meant for the LD linker?
question 2) Is their a windows version of LD linker?
question 3) I am finding most linkers don't support .bin format. So how can I get a .bin file out of my .obj files. I used Visual Studio 2005 to compile them to .obj. I know that VS link does not allow you to link the obj into .bin format.

question 4) Is their a way to make a bin file under windows xp out of obj ? I cann't find any way.
The only way I have seen is using LD and linker scripts but that is in linux. I don't want to have to do my whole os in asm using nasm -f bin ...etc etc would really like to use my c obj files.

Question 5) Is their a linker for nasm under windows ? I can compile to all different formats but unless I am under linux using gcc (gnu compiler). I cann't make the .exe file.

question 6) Does visual studio compiler compile to only OMF/COFF format could I ever get it to compile to ELF?

question 7) Which format is better to use ELF or OBJ for creating an OS? Why?

question 8) If I want to call c functions in a asm program I cann't compile to a bin using nasm ?
Nasm doesn't support external references in bin file format. So then how is it even possible to use nasm to compile into a bin with external c code. (I believe it is not which is why you must have a linker that supports linking to a bin.)


I am also having a little trouble understanding the meaning of some of the lines in the linker script

Code: Select all

 .data :
  {
     data = .; _data = .; __data = .;
     *(.data)
     *(.rodata)
     . = ALIGN(4096);
  }
data=.; _data=.; ...etc what is this for ?
*(your section) what is this for.
And why do you have to align? . = ALIGN(4096);


.text 0x100000 : means put the code segment at 1048576 memory location. What is so special about putting the code segment their? Could you randomly choose any memory location as long as it didn't overlay the GDT, IDT ,...etc etc.

Thanks for any clarity on these question's.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: linker scripts and link to bin?

Post by ru2aqare »

Sam111 wrote: question 1) Are linker scripts only meant for the LD linker?
question 2) Is their a windows version of LD linker?
ld can be compiled for Cygwin, so yes, there is a Windows version of ld.
Sam111 wrote:question 3) I am finding most linkers don't support .bin format. So how can I get a .bin file out of my .obj files. I used Visual Studio 2005 to compile them to .obj. I know that VS link does not allow you to link the obj into .bin format.
I once wrote a linker that runs under Windows (or Mono), accepts COFF object files and produces PE or .bin files. Check this thread: linking 32/64 bit.
Sam111 wrote: question 4) Is their a way to make a bin file under windows xp out of obj ? I cann't find any way.
The only way I have seen is using LD and linker scripts but that is in linux. I don't want to have to do my whole os in asm using nasm -f bin ...etc etc would really like to use my c obj files.

Question 5) Is their a linker for nasm under windows ? I can compile to all different formats but unless I am under linux using gcc (gnu compiler). I cann't make the .exe file.
Why not link the intermediate files (the .obj files) together, and have the linker produce a .bin file?
Sam111 wrote: question 6) Does visual studio compiler compile to only OMF/COFF format could I ever get it to compile to ELF?
The MS toolchain uses COFF format only. The Borland toolchain uses OMF, if I remember correctly. The Cygwin toolchain uses COFF by default, although you can compile binutils and gcc under Cygwin to use ELF.
Sam111 wrote: question 7) Which format is better to use ELF or OBJ for creating an OS? Why?
It depends.
Sam111 wrote: question 8) If I want to call c functions in a asm program I cann't compile to a bin using nasm ?
Nasm doesn't support external references in bin file format. So then how is it even possible to use nasm to compile into a bin with external c code. (I believe it is not which is why you must have a linker that supports linking to a bin.)
Binary files are just an array of bytes, nothing else. You need a format that supports externals and relocations - for example, COFF.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re: linker scripts and link to bin?

Post by Pype.Clicker »

Sam111 wrote:Ok , I am trying to completely understand linker scripts.
question 1) Are linker scripts only meant for the LD linker?
Yes. (at least, if another linker has linker script, it's unlikely to use the same syntax/semantic for them).
question 2) Is their a windows version of LD linker?
Yes, provided with cygwin or any other port of the GNU compiler suite that you manage to install on your windows.
question 3) I am finding most linkers don't support .bin format. So how can I get a .bin file out of my .obj files. I used Visual Studio 2005 to compile them to .obj. I know that VS link does not allow you to link the obj into .bin format.
"bin" format does not quite exists. It's just the absence of any format, and since most object files expect the loader to do some stuff (e.g. clearing the .Bss section, loading some section at specific addresses, etc.) they aren't quite often used. Usually, a real ".bin" would be loaded at a well-known address and contains a loader of its own (usually in ASM) that does all the rest of suitable things.
By the way, you are aware that you could use GRUB as bootloader and enjoy the full power of ELF binaries rather than playing rough with .bin files, right ?
question 4) Is their a way to make a bin file under windows xp out of obj ? I cann't find any way.
The only way I have seen is using LD and linker scripts but that is in linux. I don't want to have to do my whole os in asm using nasm -f bin ...etc etc would really like to use my c obj files.
Again, you can install yourself GNU tools (compiler, linker, etc.) on your windows system. No need to switch to another OS or do the whole stuff in asm.
question 6) Does visual studio compiler compile to only OMF/COFF format could I ever get it to compile to ELF?
No chance to see an ELF file from VS compilers ... ever. But you can too use the GNU compiler chain from the VS IDE (iirc, there are even tutorials about this in the FAQ)
question 7) Which format is better to use ELF or OBJ for creating an OS? Why?
I'd say ELF because of GRUB. If you know of another bootloader that has the same flexibility, that is open-source and that works with .OBJ, you can very well go with OBJ.

I used to have a COFF->something simpler converter in the early days of my OS development story. It should still be in Clicker's binaries or CVS (http://clicker.cvs.sourceforge.net/view ... ools/misc/). Maybe you could give it a look to de-mystify those binaries things.
question 8) If I want to call c functions in a asm program I cann't compile to a bin using nasm ?
Nasm doesn't support external references in bin file format. So then how is it even possible to use nasm to compile into a bin with external c code. (I believe it is not which is why you must have a linker that supports linking to a bin.)

That would not work. since C code comes from another "translation module" (the C compiler), you need a linker so that the ASM code know where the C code stands, etc.

Btw, you may want to check the objcopy program, too. Iirc, that would be the perfect tool to extract a single section out of a binary (be it ELF or COFF) and have it stored in a .bin file... Given that your program has been linked to a single section.

And about "what is so special about 1MB" ... This is the first address where you can store your kernel whatever size it has (below 1MB barrier, you have real-mode stuffs, BIOS, VGA memory and so on... And typically your real-mode bootloader as well).
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: linker scripts and link to bin?

Post by Sam111 »

So under a windows os the only way to compile to a bin is to have some type of gnu compiler/linker.
The IDE Dev C++ use this and this is the only way I can see of doing it. Either using the IDE or getting the gnu command line stuff for windows (i.e cygwin ...etc etc)


Can you ever compile/link obj files to bin using not a gnu product and not having to write your own?
And about "what is so special about 1MB" ... This is the first address where you can store your kernel whatever size it has (below 1MB barrier, you have real-mode stuffs, BIOS, VGA memory and so on... And typically your real-mode bootloader as well).
So what would happen if you loaded your os under 1MB and jumped to it making sure to turn of interrupts and stuff. Just curious what error it would give you if you didn't overlay the IVT (in 16 bit mode ) or IDT GDT in (32 bit mode) , and didn't overlay VGA memory.


Also saw this on your wiki
On Windows it is also possible to use Visual Studio to write and compile your operating system. You'll require a special configuration file, and you will certainly be in the minority, but it does work quite well.
IS their an example configuration file for compiling your kernel to a bin. Or does this just mean they are setting it up to use a different compiler/linker just thru the VS 2005 IDE.
Either way back to linker stuff.
Is their away to take an .exe file and create a bin from it? Any tools?
Is their away to convert a obj file to elf ?

And I am doing my own bootloader so I am not using grub. So the grub stuff you mentioned is not applicable.

And I am just curious how hard it would be to take the obj files and write a program that would create a bin for those obj files. I think all you would need is the entry point and to merge the data segments togther , code segments together. But what would need to be recalculated ?
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: linker scripts and link to bin?

Post by ru2aqare »

Sam111 wrote: So under a windows os the only way to compile to a bin is to have some type of gnu compiler/linker.
No, it is not the only solution. Another solution is to link your program/kernel/whatever to an executable file, and use a tool that can copy a region from file A to file B. If you don't have one, such a tool can be written in like five minutes...
Sam111 wrote: So what would happen if you loaded your os under 1MB and jumped to it making sure to turn of interrupts and stuff. Just curious what error it would give you if you didn't overlay the IVT (in 16 bit mode ) or IDT GDT in (32 bit mode) , and didn't overlay VGA memory.
Nothing special. Your kernel would receive control, and start executing.
Sam111 wrote: And I am just curious how hard it would be to take the obj files and write a program that would create a bin for those obj files. I think all you would need is the entry point and to merge the data segments togther , code segments together. But what would need to be recalculated ?
If you read my post, I already pointed you to my linker that does exactly this... (sorry for advertising my linker, but it does get the job done.)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: linker scripts and link to bin?

Post by Combuster »

Sam111 wrote:So under a windows os the only way to compile to a bin is to have some type of gnu compiler/linker.
The IDE Dev C++ use this and this is the only way I can see of doing it. Either using the IDE or getting the gnu command line stuff for windows (i.e cygwin ...etc etc)
There are other linkers that can emit binary files: Tool Comparison
Can you ever compile/link obj files to bin using not a gnu product and not having to write your own?
JLoc
And about "what is so special about 1MB" ... This is the first address where you can store your kernel whatever size it has (below 1MB barrier, you have real-mode stuffs, BIOS, VGA memory and so on... And typically your real-mode bootloader as well).
So what would happen if you loaded your os under 1MB and jumped to it making sure to turn of interrupts and stuff. Just curious what error it would give you if you didn't overlay the IVT (in 16 bit mode ) or IDT GDT in (32 bit mode) , and didn't overlay VGA memory.
There's nothing stopping you from loading a kernel below 1MB - there is normal memory there as well. The problem is that you're stuck between bios and reserved data area's which make memory management a tad more difficult.
Either way back to linker stuff.
Is their away to take an .exe file and create a bin from it? Any tools?
An .EXE file usually means it is meant for DOS or Windows and therefore not suitable to run standalone.
Is their away to convert a obj file to elf?
objcopy.
And I am just curious how hard it would be to take the obj files and write a program that would create a bin for those obj files. I think all you would need is the entry point and to merge the data segments togther , code segments together. But what would need to be recalculated ?
Essentially, you'd need to write a basic linker of your own. Is that what you want, and if so, do you have the necessary knowledge of binaries to do that?



Also, what is your problem with GNU tools? They are both more portable and suitable for OS development.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: linker scripts and link to bin?

Post by Sam111 »

First the JLoc link on wiki is down so you cann't download that.

Second nothing is wrong with gnu compiler just don't want to download and install cygwin on this computer. And all I have is VS 2005 on my computer. Don't really want to download an install a whole new IDE like Dev C++ on this machine. So is their away to get the LD linker and the compilers that go along with it with out having to install cygwin or alike.

Third it seems like no matter what the only way to compile a kernel to a bin file is either write it completely in nasm. Or use a product geared for linux uses GNU gcc ...etc etc.
Normally I am ok with use linux and gnu gcc stuff. But I was just wondering if any well known compilers/linkers geared for windows are out their that allow me to do the anology of gcc , ld ...etc. If yes is their a download in zip format because every download is a .tar , .tar_gz..
And I don't want to start downloading 7-zip or another decompression software.

Same thing for binutils is their a windows version that is in .zip format?

I know I am being picky but this is an old computer and I don't want to install a ton of stuff that I am only going to use once or twice to open up stuff.

Essentially, you'd need to write a basic linker of your own. Is that what you want, and if so, do you have the necessary knowledge of binaries to do that?
Yes , but I think I have to learn the object formats better.
I do want to write something that takes in .exe and outputs .bin for it.
But I don't know how easy this would be from scratch. I would have to find the code section ,data section , stack section and merge it into on flat file. I would think their would be some addresses that would need recalcuation etc etc... But I don't know what the address are relative to in the first place. Like if you found the code segment and their was a jmp command inside here then if the code segment changes is the jmp command relative to the starting address of the code segment. So if I change the code segment starting address I wouldn't have to recalculate all addresses relative to it. Yes/No? God knows what else could need modification.
Also, what is your problem with GNU tools? They are both more portable and suitable for OS development.
Nothing just wondering what the bar minium for downloading stuff would be.
Seems like with linux it already their but for windows it's a pain.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: linker scripts and link to bin?

Post by Combuster »

You're concerned about disk space and you're using visual studio. You mind explaining where that and those other prejudices come from? :shock:

Btw: look what google turned up
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: linker scripts and link to bin?

Post by Sam111 »

I just installed djgpp cool .
Thanks

Anyway I am still a little shaky on linker scripts
For example My entry point (aka starting point is called startitup )

I found a sample linker scripts

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(startitup)
SECTIONS
{
  .text 0x100000 :
  {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
  }

  .data :
  {
     data = .; _data = .; __data = .;
     *(.data)
     *(.rodata)
     . = ALIGN(4096);
  }

  .bss :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }

  end = .; _end = .; __end = .;
} 


Do you really even need linker scripts because I would thing their is a switch for most of these things. Like

Code: Select all

ld -e start -nostdlib --oformat binary -o kernel.bin start.o main.o scrn.o gdt.o idt.o isrs.o irq.o timer.o kb.o
instead of

Code: Select all

ld -T link.ld -o kernel.bin start.o main.o scrn.o gdt.o idt.o isrs.o irq.o timer.o kb.o
I can see where align might be important but why 4096 what is so special about this? Why not 5555 or 1030 or any other number. 2^12 = 4096 is their any significance?

Why do we need 0x100000 in

Code: Select all

.text 0x100000 :
When we are producing a binary file what does it matter where we specify the text section to begin. This is the job of the bootloader to load the kernel.bin file into memory and jump to it.
The only thing that is important is that the text section goes in first before the data seg's, ...etc this is only so the bootloader can load the kernel.bin into memory and jump to the first address as opposed to having to jump over the data seg or bss seg ,...etc.

So I guess what I am saying is as long as all the text,data,...segments are being merged correctly and the text segment is first why even ever specify where to start .text segment at. Or am I over looking something?


Also if I do something like this

Code: Select all

SECTIONS
     {
       . = 0x10000;
       .text : { *(.text) }
       . = 0x8000000;
       .data : { *(.data) }
       .bss : { *(.bss) }
     }
If their is extra space between the text content and the data content. Then what does this gap default to . i.e what does the linker write into the space is it like all zero's.
I know you can tell it a default some way but what is the default?

I can see where address placeing is key if you are not using bin format and are under an os writing a user mode application but I would think this is little significants if it's just going to be the raw bit's.
alexisoldroyd
Posts: 4
Joined: Wed Dec 10, 2008 8:34 pm

Re: linker scripts and link to bin?

Post by alexisoldroyd »

Pype.Clicker wrote: No chance to see an ELF file from VS compilers ... ever. But you can too use the GNU compiler chain from the VS IDE (iirc, there are even tutorials about this in the FAQ)
Where might I find the info to setup visual studio to compile ELF using the gnu toolchain? I've been looking around quite a bit here and haven't seen a tutorial mentioned in any FAQ
tantrikwizard
Member
Member
Posts: 153
Joined: Sun Jan 07, 2007 9:40 am
Contact:

Re: linker scripts and link to bin?

Post by tantrikwizard »

alexisoldroyd wrote:Where might I find the info to setup visual studio to compile ELF using the gnu toolchain? I've been looking around quite a bit here and haven't seen a tutorial mentioned in any FAQ
Most people dont use visual studio. Search 'Custom Build Tool' on MSDN for info how to set it up. Its also documented in the VS documentation. Its rather easy to setup, you can create a project template to build each file extention a certain way or build each file individually.
alexisoldroyd
Posts: 4
Joined: Wed Dec 10, 2008 8:34 pm

Re: linker scripts and link to bin?

Post by alexisoldroyd »

What do most people use? I really like the code completion in visual studio, and I have a few refactoring tools. Since I can't use the debugger or compiler anyways in Visual Studio, besides the code completion I don't have much reason to stay tied to it.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: linker scripts and link to bin?

Post by neon »

Wiki wrote:On Windows it is also possible to use Visual Studio to write and compile your operating system. You'll require a special configuration file, and you will certainly be in the minority, but it does work quite well.
This is wrong. Just set up project settings (does not require any configuration file set up), build the PE file and have your calling program call the PE program. Not to hard...

I personally use Visual Studio, however most people on these boards tend to use GCC on Linux or running in Cygwin or a GCC cross compiler.

In any case, I personally recommend sticking with whatever you have the most experience with as you would better understand the toolset better.

If you want to compile Elf in Visual Studio, you can configure the project settings to use a different toolchain instead or to run a build script that can compile and link your program. Or, just use its IDE for editing and run the compiler through command line.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
alexisoldroyd
Posts: 4
Joined: Wed Dec 10, 2008 8:34 pm

Re: linker scripts and link to bin?

Post by alexisoldroyd »

I was considering using PE or ELF, but I didn't know the technical merits of either. I have experience with PE files and modifying their headers etc. I have none with ELF. I figured everyone was using ELF for good reasons, but either way its just a format to me I have no issues loading either.
tantrikwizard
Member
Member
Posts: 153
Joined: Sun Jan 07, 2007 9:40 am
Contact:

Re: linker scripts and link to bin?

Post by tantrikwizard »

alexisoldroyd wrote:I was considering using PE or ELF, but I didn't know the technical merits of either. I have experience with PE files and modifying their headers etc. I have none with ELF. I figured everyone was using ELF for good reasons, but either way its just a format to me I have no issues loading either.
Most guys use ELF. A few guys use PE or create their own format. I created my own format using Open Watcom. I figured if I'm going to write my own OS I may as well write my own executable format as well. I agree, the VS IDE is great, code complete and intellisense save a lot of time. Check out WholeTomatoe's Visual AssitX if you get a chance, its Intellisense on steroids.
Post Reply