Linker in Mac OS X & OS Dev

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.
R2_
Member
Member
Posts: 50
Joined: Sat Dec 02, 2006 3:27 pm

Linker in Mac OS X & OS Dev

Post by R2_ »

I was looking thru old topics and I saw that some people managed to fix the mac os x linker to work with flat binary files but I didn't see anyone post a step by step on how they did this. I've been googling for a couple of hours for the answer to this problem but I found nothing so any help would be greatly appreciated.

In the past I have been able to overcome this problem by emulating fedora and using its linker. (Which is a pain as it wastes my computers resources and causes me to lag a lot)

the error the mac os x linker gives me is:
ld: Pass 1
link.ld
ld: link.ld bad magic number (not a Mach-O file)
test.o
kernel.o
ld: kernel.o bad magic number (not a Mach-O file)
I tried rebuilding bin utils but that I get a couple errors
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Post by Colonel Kernel »

Build a GCC cross-compiler. The wiki has a tutorial. That's what I did on Mac OS X, and it worked great.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
R2_
Member
Member
Posts: 50
Joined: Sat Dec 02, 2006 3:27 pm

Post by R2_ »

Colonel Kernel wrote:Build a GCC cross-compiler. The wiki has a tutorial. That's what I did on Mac OS X, and it worked great.
but my compiler works fine the problem is with the linker complaining is not a mach-o file.. wouldn't this just reinstall gcc ? or does it also include the linker ?

Which cross-compiler article did you mean ? I see 3 on the wiki

thanks for responding :P.
R2_
Member
Member
Posts: 50
Joined: Sat Dec 02, 2006 3:27 pm

Post by R2_ »

Woops sorry I pressed wrong button I meant to press edit..

do you have the cross compiler binaries already compiled ?
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Post by Colonel Kernel »

If you read the tutorial, all will be explained. Everyone runs into this problem (although the error message is different on other platforms), and the solution suggested is always the same.

To get you started, I will clarify a bit: The tutorial covers building all the tools you'll need for compiling and linking your kernel -- so yes, ld is included. Also, here is the link:

http://www.osdev.org/wiki/GCC_Cross-Compiler

I could send you binaries, but I think building your own cross-compiler is a better long-term solution. You'll learn more about how the GNU toolchain works and how to use it, plus you'll be one step ahead when the day finally comes to make your OS self-hosted.

To put it more bluntly, it's a rite of passage. So get busy. ;)
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
R2_
Member
Member
Posts: 50
Joined: Sat Dec 02, 2006 3:27 pm

Post by R2_ »

Colonel Kernel wrote:If you read the tutorial, all will be explained. Everyone runs into this problem (although the error message is different on other platforms), and the solution suggested is always the same.

To get you started, I will clarify a bit: The tutorial covers building all the tools you'll need for compiling and linking your kernel -- so yes, ld is included. Also, here is the link:

http://www.osdev.org/wiki/GCC_Cross-Compiler

I could send you binaries, but I think building your own cross-compiler is a better long-term solution. You'll learn more about how the GNU toolchain works and how to use it, plus you'll be one step ahead when the day finally comes to make your OS self-hosted.

To put it more bluntly, it's a rite of passage. So get busy. ;)
haha iight :P
If I fail to compile these I'll get back to you =]
R2_
Member
Member
Posts: 50
Joined: Sat Dec 02, 2006 3:27 pm

Post by R2_ »

Ok I compiled the linker but it still says
abel-duartes-computer:~/Desktop/OS HD$ /Users/HD/Desktop/build/bin/i586-elf-ld -Tlink.ld -o kernel.bin test.o kernel.o
test.o: file not recognized: File format not recognized
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Post by Colonel Kernel »

Did you re-compile test.o using your new cross-compiler first...?
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
R2_
Member
Member
Posts: 50
Joined: Sat Dec 02, 2006 3:27 pm

Post by R2_ »

Colonel Kernel wrote:Did you re-compile test.o using your new cross-compiler first...?
Im compiling GCC right now but how can that be the problem when it isnt recognizing an assembly flat binary either ?

Does this have anything to do with the target parameter ?
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:

Post by Combuster »

You can not use binary files as input to a linker. The linker will have no clue as what to link as binary files do not contain any such information. You'll need to assemble to an object format. For the cross-compiler, that's ELF.
"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 ]
R2_
Member
Member
Posts: 50
Joined: Sat Dec 02, 2006 3:27 pm

Post by R2_ »

Combuster wrote:You can not use binary files as input to a linker. The linker will have no clue as what to link as binary files do not contain any such information. You'll need to assemble to an object format. For the cross-compiler, that's ELF.
well now it works with nasm after I specified elf output format for it but gcc still fails to compile..

Thanks Combuster.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Well then post the error R2_, We're not mind readers... :wink:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
R2_
Member
Member
Posts: 50
Joined: Sat Dec 02, 2006 3:27 pm

Post by R2_ »

Brynet-Inc wrote:Well then post the error R2_, We're not mind readers... :wink:
haha sorry

They are makefile errors
crtstuff.c:116:Unknown pseudo-op: .weak
crtstuff.c:116:Rest of line ignored. 1st junk character valued 95 (_).
make[1]: *** [crtbegin.o] Error 1
make: *** [install-gcc] Error 2
I think I might stick with 100% assembly.. it might be harder to code but it will definitely avoid all these problems :D
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:

Post by Combuster »

Sounds like
wiki wrote:This, in combination with lots of other assembly-level error messages (like, Warning: .type pseudo-op used outside of .def/.endef ignored, or Error: unknown pseudo-op: '.local') results when you did not correctly set the --prefix=$PREFIX during the binutils configure.
I don't own a mac so if Colonel Kernel (or somebody else who has one) could test-build the cross-compiler according to the wiki instructions, we know wether it should work or not. Since I do know is that those instructions work on both linux (gentoo) and cygwin, I think you missed something. Still, a check would be welcome.
"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
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

I guess the best way for us to be sure is to know a few more things..

Did you download the latest GCC/binutils source from their "official" locations?

What ./configure arguments did you use for each? "make" commands as well..

All of this information would be somewhat helpful.. 8)
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Post Reply