Author Topic: How to fix simple elf bin file "truncated to fit R_386_PC8 against ABS"  (Read 12888 times)

Offline Deskman243

  • Jr. Member
  • *
  • Posts: 49
Greetings everyone

I was trying to convert the code of a simple VESA VBE program into the elf standard. Bin files  change my settings so here I was making a simple version to try and review. I use ld when I have to use a elf .o object and I can make the elf file run for boot.asm elf object however in my makefile I realize I can only run the bin form stage1a.bin (including the org setting). When I try to make the stage1.o elf object I keep getting the error from above. Here is my code so far


boot.asm
Code: [Select]
[BITS 16]
;[ORG 0x7c00]

_prep_module:
    xor ax,ax   
    mov ds,ax
    mov es,ax 
    mov ss,ax
    mov sp,0x7c00

DriveIdProp:
    mov [DriveId],dl
    mov ah,0x41
    mov bx,0x55aa
    int 0x13
    jc CheckProp
    cmp bx,0xaa55
    jne CheckProp

LoadProp:

mov bx,0x0000_7E00
mov cl,2
mov ah,0x02
mov al,1
mov ch,0
mov dh,0
    int 0x13
    jc  ReadCheck

    mov dl,[DriveId]
    jmp 0x7e00

ReadCheck:
CheckProp:

lea si,[Message]

Print:
lodsb
test al,al
jz .loop
mov ah,0x0E
mov bx,7
int 0x10
jmp Print


.loop:
    hlt   
    jmp .loop
   
DriveId:    db 0
Message:    db "We have an error in boot process"
MessageLen: equ $-Message
ReadPacket: times 16 db 0

;times (0x1be-($-$$)) db 0

;    db 80h
;    db 0,2,0
;    db 0f0h
;    db 0ffh,0ffh,0ffh
;    dd 1
;    dd (20*16*63-1)

;    times (16*3) db 0

;    db 0x55
;    db 0xaa

printstr.asm
[code]
bits 16


global printstr

printstr:

lodsb
test al,al
jz .done
mov ah,0x0E
mov bx,7
int 0x10
jmp printstr

.done:
ret



[/code]

stage1.asm
Code: [Select]
[BITS 16]
;[ORG 0x7E00]

modeblock: times 256 db 0

jmp LoadProp

;section .text

;modeblock: times 256 db 0

DriveIdProp:
    mov [DriveId],dl
    mov ah,0x41
    mov bx,0x55aa
    int 0x13
    jc CheckProp
    cmp bx,0xaa55
    jne CheckProp


LoadProp:

mov bx,0x0000_7E00

;check address arithmetic base
add bx,0x0200
mov cl,3

LoadLoop:
mov ah,0x02
mov al,3
mov ch,0
mov dh,0
int 0x13

;    jc  ReadCheck
cmp cl,5
inc cl

jmp VideoModule

;address zone check


Gdt32:
dq 0

Code32:
dw 0xffff
dw 0
db 0
db 0x9a
db 0xcf
db 0

Data32:
dw 0xffff
dw 0
db 0
db 0x92
db 0xcf
db 0

Gdt32Len: equ $-Gdt32

Gdt32Ptr: dw Gdt32Len-1
dd Gdt32

Idt32Ptr:
dw 0
dd 0

;jmp 0x8400

stack: times 256 dd 0
stackplacer:



VideoModule:
mov ax,0x4f01
mov cx,0x4117

VideoBuff:
mov di,modeblock
int 0x10
mov esi,[modeblock+0x28]

mov ax,0x4f02
mov bx,0x4117
int 0x10

mov ax,0x2401
int 0x15

;check address base
xor edx,edx
mov dx,cs
shl edx,4
add edx,stackplacer


SetVideoMode:

; in al,0x92
; or al,2
; out 0x92,al

cli
lgdt[Gdt32Ptr]
lidt[Idt32Ptr]
mov eax,cr0
or eax,1
mov cr0,eax
jmp 0x8:protectedMode

[bits 32]

protectedMode:
mov ax,0x10
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax
mov esp,edx

; mov byte[0xb8000],'D'
; mov byte[0xb8001],0xa

l1:
xor eax,eax
mov edi,esi
mov ecx,1024*768*2/4
cld
rep stosd

mov eax,0x07FF07FF
mov ecx,32
mov edi,esi

l2:
push ecx
mov ecx,768*16

lines:
cld
rep stosd

mov ecx,0xFFFFFFFF
loop $

pop ecx
sub eax,0x00410041
loop 12

mov ecx,0xFFFFFFFF
loop $
jmp l1




.loop1:
    hlt   
    jmp .loop1

ReadCheck:
CheckProp:

lea si,[Message]

Print:
lodsb
test al,al
jz .loop
mov ah,0x0E
mov bx,7
int 0x10
jmp Print


.loop:
    hlt   
    jmp .loop
   
;section .data

DriveId:    db 0
LoopProp: db 0
AddressProp: dw 0x0000_8000
Message:    db "We have an error in boot process"
MessageLen: equ $-Message
ReadPacket: times 16 db 0


link.ld
Code: [Select]
* boot.ld */

/* linker script for 16 bits - mbr */
OUTPUT_FORMAT(binary)
OUTPUT_ARCH(i386)
ENTRY(_prep_module)

SECTIONS
{
  . = 0x7c00;
  _premier = .;
  _prep_module= .;

  .text : { *(.text16) *(.text) }
  .rodata : { *(.rodata) }
  .data : { *(.data) }

  . = _premier + 512-2;
  .dummy : { BYTE(0x55); BYTE(0xAA); }

  _bss_begin = .;
    .bss : { *(.bss) }
  _bss_end = .;

  _heap = .;

  /DISCARD/ : { *(.comment) *(.eh_frame) *(.note.GNU-stack) }
}

Makefile
Code: [Select]
FILES=boot.o printstr.o

all:
dd if =boot.bin of=boot.img bs=512 count=1 conv=notrunc
dd if =stage1.bin of=stage1a.img bs=512 count=3 seek=1 conv=notrunc

boot.o: boot.asm
nasm -f elf32 -o boot.o boot.asm

printstr.o:printstr.asm
nasm -f elf32 -o printstr.o printstr.asm

stage1.o: stage1.asm
nasm -f elf32 -o stage1.o stage1.asm

stage1a.bin:stage1.asm
nasm -f bin -o stage1a.bin stage1.asm

boot.bin: $(FILES)
ld -T link.ld $(FILES) -o boot.bin

stage1.bin: stage1.o
ld -g -m i386 -tText 0x7E00 -o stage1.bin  stage1.o


clean:
rm -rf boot.img
rm -rf boot.bin
rm -rf stage1.bin
rm -rf stage1.o
rm -rf stage1a.bin
rm -rf printstr.o


I'm really hoping this post can show where I do not understand and would be happy to continue the subject further. Also thank you to fredericopisarra's post from here https://forum.nasm.us/index.php?topic=2850.0 that taught me about using ld.

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 374
  • Country: br
To build an MBR switching to i386 protected mode you don't need sections or a linker script. Here's an example (attached)...

Just make to build and make run to test (requires qemu-system-i386).