relocation truncated to fit : 16 .text

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
stonedzealot

relocation truncated to fit : 16 .text

Post by stonedzealot »

I'm trying to get this code to work:

Code: Select all

[BITS 32]
[GLOBAL start]
[EXTERN _osmain]
;we are currently @ 1MB physical
start:
   lgdt [gdtr]   ;load new GDT (after bootsector did its thing)
   lidt [idtr]   ;load IDT   
%macro DEFINT 2
   push ax
   
   mov ax, _int32
   and ax, 0xFFFF
   mov [idt + (32 * 8)],ax
   
   mov ax, _int32
   shr ax, 16
   mov [idt + (32 * 8) + 6], ax
   
   pop ax
%endmacro
   DEFINT 32, _int32 
   call _osmain   ;hop into C
   hlt      ;if C ever returns to this...halt the CPU, something's fucked   

[EXTERN _testint]
[GLOBAL _int32]
_int32:
        pusha
        push ds
        push es
        push fs
        push gs
        mov eax,0x10    ; Data segment
        mov ds,eax
        mov es,eax
     cld
   call _testint
    pop gs
        pop fs
        pop es
        pop ds
        popa
     iret

gdt:
   dw 0      ; limit 15:0
   dw 0      ; base 15:0
   db 0      ; base 23:16
   db 0      ; type
   db 0      ; limit 19:16, flags
   db 0      ; base 31:24

   dd 0      ;this is the same as above, still just 8 bytes of blanks
   dd 0      

DATASEL   equ   $-gdt
   dw 0FFFFh
   dw 0
   db 0
   db 92h      ; present, ring 0, data, expand-up, writable
   db 0CFh      ; page-granular (4 gig limit), 32-bit
   db 0

CODESEL   equ   $-gdt
   dw 0FFFFh
   dw 0
   db 0
   db 9Ah      ; present,ring 0,code,non-conforming,readable
   db 0CFh      ; page-granular (4 gig limit), 32-bit
   db 0
gdtend:

gdtr:
   dw gdtend - gdt - 1
   dd gdt
idt:            
%rep 256      
   dw 0      ;0
   dw DATASEL   ;2
   dw 0x8E00    ;4   
   dw 0      ;6
%endrep   
endidt:

idtr:
   dw endidt - idt - 1
   dd idt

and it gives the error in the subject of this post. I guess I'm just not sure what the hell is happening in this error. I can tell you however, that NASM likes the file, it's the linker that blows up (of course, since it's a relocation error). Can someone shed a little light on what's going on?
Tim

Re:relocation truncated to fit : 16 .text

Post by Tim »

Put a [bits 16] after your 32-bit code but before your data.
stonedzealot

Re:relocation truncated to fit : 16 .text

Post by stonedzealot »

Sorry I'm so late in replying...no internet access for about two days. Anyway, I supposed that you meant before the "gdt:" label (as there are no opcodes after that) But unfortunately, I still have the same problem... same text at the same offsets.
Tim

Re:relocation truncated to fit : 16 .text

Post by Tim »

I missed the point of your code; I see it's all 32-bit.

You can't load the address of a label in a 32-bit section as you are here. You will need to load it into a 32-bit register and do the appropriate arithmetic to extract the right part.

I think this is what you intended to do, because you have code like this:

Code: Select all

mov ax, _int32
shr ax, 16
As it is, this code won't work; if you shift AX 16 bits you end up with nothing (zeroes). If you replace AX with EAX everywhere, your code should assemble and work.
stonedzealot

Re:relocation truncated to fit : 16 .text

Post by stonedzealot »

Right-o. Thank you very much Mr. Robinson...
Post Reply