Author Topic: adjustments for elf objects linking for bin files  (Read 8255 times)

Offline Deskman243

  • Jr. Member
  • *
  • Posts: 49
adjustments for elf objects linking for bin files
« on: October 29, 2022, 06:06:27 AM »
Greetings chat glad to have y'all here.

I have been coming on here more regularly and was going through the previous post on here

https://forum.nasm.us/index.php?topic=2850.0

Today I was practicing using .ld files to make a boot section bin that was debugable for gdb under the elf construct.  Specifically I was changing the mbr tag to fit the 512 db standard.
Here I would like to show the lines of code I modified to the print boot first

1.
Code: [Select]
section .rodata:
msg: 'test string',0

times 510 - 32- ($-$$) db 0
dw 0xaa55
   

2. Here is the change in link.ld

Code: [Select]
.rodata : {.boot.o}

. = _premier +510*2

3. so the new code is now

[code]

OUTPUT_FORMAT(binary)
OUTPUT_ARCH(i386)
ENTRY(_prep_module)

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

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

. = _premier + 510;
.dummy : {BYTE(0x55); BYTE(0xaa);}

_bss_premier =.;
.bss : {*(.bss)}
_bss_stub = .;

_heap = .;

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

}

[/code]
bits 16

;%include 'printstr.asm'
extern printstr

section .text
global _prep_module
_prep_module:

push cs
pop ds
cld

lea si,[msg]
call printstr

.halt:
   hlt
   jmp .halt

;%include 'printstr.asm'

section .rodata
msg:   db 'test string',0

Code: [Select]
bits 16

global printstr

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

.done
ret


Code: [Select]
boot.bin: boot.o printstr.o
ld -T link.ld -o boot.bin boot.o printstr.o

boot.o: boot.asm
nasm -f elf -g -o $@ $<

printstr.o: printstr.asm
nasm -f elf -g -o $@ $<



This combines the files in a way where I got the string alignment I was trying to get originally. I got stuck for several hours on the fact that the code does not parse the string if the times function is above the .rodata section. Another intrigue I have  is why the times functions took 2(16) alignment.

This was the only way I got the program to run so if anyone else would like to contribute here now would be a pretty good  :)