Linking and addressing issues

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.
Post Reply
User avatar
hailstorm
Member
Member
Posts: 110
Joined: Wed Nov 02, 2005 12:00 am
Location: The Netherlands

Linking and addressing issues

Post by hailstorm »

Hello everybody.

Thank you for your time reading this topic.

A few days ago, I decided to pick up my old hobby, writing an Operating System. And although I was very far with the previous OS I wrote, I decided it
would be fun to start all over. Besides, having more knowledge and experience now should make it a breeze, shouldn't it?
Well, I was (very very) wrong). But that is a topic for another day...
At this moment, I am writing my secondary bootloader. This loader is loaded by the primary bootloader. My toolchain for writing this piece of software
is nasm, combined with the Borland turbo c++ 3.0 compiler and the turbo linker.
Everything compiles and links well(ish). I try to link all source files into one com/binary executable. Luckely, the linker understands that the entrypoint
has to be at 0x0000.
The loader executes fine, until a string is passed to the print_string function. Looking in my bochs debugger, it seems that a totally wrong offset is passed to
the print_string function. Looking at my map-file I understand what's going wrong; the linker is handling the data segment as a separate segment, while I always
thought that a com-file has only one segment.
The question now is, how do I circumvent this kind of segment handling? Below the output of the linker in map-format:

Start Stop Length Name Class

00000H 00089H 0008AH _TEXT CODE
0008AH 000B0H 00027H _DATA DATA
000B2H 000B2H 00000H _BSS BSS


Detailed map of segments

0000:0000 0050 C=CODE S=_TEXT G=(none) M=LOADHEAD.ASM ACBP=28
0000:0050 003A C=CODE S=_TEXT G=(none) M=LOADER.C ACBP=28
0008:000A 0027 C=DATA S=_DATA G=DGROUP M=LOADER.C ACBP=48
0008:0032 0000 C=BSS S=_BSS G=DGROUP M=LOADER.C ACBP=48

Address Publics by Name

0000:0003 _CLEAR_SCREEN
0000:0069 _MAIN
0000:0021 _PRINT_CHAR
0000:0050 idle _PRINT_STRING
0000:0037 _SET_CURSOR
0000:0000 idle _START

Address Publics by Value

0000:0000 idle _START
0000:0003 _CLEAR_SCREEN
0000:0021 _PRINT_CHAR
0000:0037 _SET_CURSOR
0000:0050 idle _PRINT_STRING
0000:0069 _MAIN
User avatar
hailstorm
Member
Member
Posts: 110
Joined: Wed Nov 02, 2005 12:00 am
Location: The Netherlands

Re: Linking and addressing issues

Post by hailstorm »

Because this problem was driving me crazy, I did some experimenting - a sort of desperado move.

The following commandline parameters (turbo c++) did the trick:
-zR_TEXT -zTCODE
With the parameters I tried to give the datasegment the same name and class as the code segment.
And this worked! I finally see the string on screen that my loader is supposed to show.

I hope this will help anybody else in the future. Thanks for reading!
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Linking and addressing issues

Post by Octocontrabass »

Sounds like you're using the wrong memory model. Try using "-mt" to specify the tiny memory model.
User avatar
hailstorm
Member
Member
Posts: 110
Joined: Wed Nov 02, 2005 12:00 am
Location: The Netherlands

Re: Linking and addressing issues

Post by hailstorm »

Thanks for thinking along. :) I knew I had to try at least mt or ms, but none of these worked... Strange enough...
I know masm doesn't care that much, so that wasn't the problem. For now, my commandline parameters work
like a charm.
Post Reply