LD linker problems(help pls)
Posted: Tue Oct 04, 2011 4:43 pm
I've been working on a little learning OS, teaching myself assembly and just recently began linking C++ and assembly files together. In my kernel, All C++ and asm files are built to COFF object files and then linked by LD, which seems to be working improperly. The problem is that in the final binary, a function call doesn't line up. It jumps to halfway through the function and my little learning project OS crashes. This is probably just my mistake but can anybody tell me what it is? Files are below. I can post IDA views or hex views of the object files if that would help, though everything appears as it should. Thanks.
build.bat
boilerplate.asm
kernel.cpp
video.h
video.cpp
link.ld
kernel.bin
build.bat
Code: Select all
nasm -f coff -o boilerplate.o boilerplate.asm
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -ffreestanding -o kernel.o -c kernel.cpp
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -ffreestanding -o video.o -c video.cpp
ld -T link.ld
pause
Code: Select all
[BITS 32]
[extern __Z3garv]
start:
call __Z3garv
ret
Code: Select all
#include "video.h"
__attribute__ ((noreturn)) void gar()
{
unsigned char hey[13] = "Hello World!";
printCString(hey);
}
Code: Select all
#ifndef VIDEO_H_INCLUDED
#define VIDEO_H_INCLUDED
void printChar(unsigned char cer);
void printCString(unsigned char * cstring);
#endif
Code: Select all
#include "video.h"
int place = 0;
void printChar(unsigned char cer)
{
unsigned char * vidMem = (unsigned char *) 0xb8000;
vidMem[place] = cer;
vidMem[place + 1] = 0x1B;
place++;
place++;
}
void printCString(unsigned char * cstring)
{
int i = 0;
while (cstring[i] != 0)
{
printChar(cstring[i]);
i++;
}
}
Code: Select all
INPUT(boilerplate.o kernel.o video.o)
OUTPUT(kernel.bin)
OUTPUT_FORMAT("binary")
SECTIONS {
.text 0x1000 :
{
code = .;
boilerplate.o(.text)
kernel.o(.text)
video.o(.text)
}
.data :
{
data = .;
*(.data)
}
.bss :
{
bss = .;
*(.data)
}
end = .;
}
Code: Select all
seg000:00001000 call sub_1090
seg000:00001005 retn
seg000:00001005 ; ---------------------------------------------------------------------------
seg000:00001006 dd 0
seg000:0000100A dd 0
seg000:0000100E dw 0
seg000:00001010
seg000:00001010 ; =============== S U B R O U T I N E =======================================
seg000:00001010
seg000:00001010
seg000:00001010 sub_1090 proc near ; CODE XREF: seg000:00001000p
seg000:00001010 ; DATA XREF: sub_10C0r ...
seg000:00001010
seg000:00001010 var_2C = dword ptr -2Ch
seg000:00001010 var_19 = byte ptr -19h
seg000:00001010
seg000:00001010 push edi
seg000:00001011 push esi
seg000:00001012 sub esp, 24h
seg000:00001015 lea edi, [esp+2Ch+var_19]
seg000:00001019 mov esi, 1094h
seg000:0000101E mov ecx, 0Dh
seg000:00001023 rep movsb
seg000:00001025 lea eax, [esp+2Ch+var_19]
seg000:00001029 mov [esp+2Ch+var_2C], eax
seg000:0000102C call sub_1104 ; <---------------- Problem jump
seg000:00001031 add esp, 24h
seg000:00001034 pop esi
seg000:00001035 pop edi
seg000:00001036 retn
seg000:00001036 sub_1090 endp
seg000:00001036
seg000:00001037 ; ---------------------------------------------------------------------------
seg000:00001037 nop
seg000:00001037 ; ---------------------------------------------------------------------------
seg000:00001038 dd 0
seg000:0000103C dd 0
seg000:00001040
seg000:00001040 ; =============== S U B R O U T I N E =======================================
seg000:00001040
seg000:00001040
seg000:00001040 sub_10C0 proc near ; CODE XREF: seg000:00001077p
seg000:00001040
seg000:00001040 arg_0 = dword ptr 4
seg000:00001040
seg000:00001040 mov eax, large ds:1090h
seg000:00001045 mov edx, [esp+arg_0]
seg000:00001049 mov [eax+0B8000h], dl
seg000:0000104F mov eax, large ds:1090h
seg000:00001054 mov byte ptr [eax+0B8001h], 1Bh
seg000:0000105B add large dword ptr ds:1090h, 2
seg000:00001062 retn
seg000:00001062 sub_10C0 endp
seg000:00001062
seg000:00001063 ; ---------------------------------------------------------------------------
seg000:00001063 push ebx ;<---------------------------Jump should land here
seg000:00001064 sub esp, 4
seg000:00001067 mov ebx, [esp+0Ch]
seg000:0000106B mov al, [ebx]
seg000:0000106D test al, al
seg000:0000106F jz short sub_1104
seg000:00001071
seg000:00001071 loc_10F1: ; CODE XREF: seg000:00001082j
seg000:00001071 movzx eax, al
seg000:00001074 mov [esp], eax
seg000:00001077 call sub_10C0
seg000:0000107C mov al, [ebx+1]
seg000:0000107F inc ebx
seg000:00001080 test al, al
seg000:00001082 jnz short loc_10F1
seg000:00001084
seg000:00001084 ; =============== S U B R O U T I N E =======================================
seg000:00001084
seg000:00001084
seg000:00001084 sub_1104 proc near ; CODE XREF: sub_1090+1Cp <-not here
seg000:00001084 ; seg000:0000106Fj
seg000:00001084 add esp, 4
seg000:00001087 pop ebx
seg000:00001088 retn
seg000:00001088 sub_1104 endp ; sp-analysis failed
seg000:00001088
seg000:00001089 ; ---------------------------------------------------------------------------
seg000:00001089 nop
seg000:0000108A nop
seg000:0000108B nop
seg000:0000108B ; ---------------------------------------------------------------------------
seg000:0000108C dd 0
seg000:00001090 dd 0
seg000:00001094 aHelloWorld db 'Hello World!'
seg000:000010A0 dd 0
seg000:000010A0 seg000 ends
seg000:000010A0
seg000:000010A0
seg000:000010A0 end