0

I'm tried this below tools, to call 2 files

My idea was to execute the function from c using assembler and create a bin file

nasm -f elf32 boot3.s -o boot3.o // convert asm file to object
gcc -c bootloader3.c -o bootloader3.o // convert c file to object
gcc boot3.o bootloader3.o -o boot3.bin // make bin file

boot3.s

section .text
bits 16

global _start              ; Declare the entry point symbol
extern main                ; Declare the main function as external

_start:
    cli                     ; Disable the interrupts
    call main               ; Call the main function
    hlt                     ; Halt the CPU

bootloader3.c

#include <stdio.h>

void main() {
    printf("Hello, World!\n");
    return;
}

My error: Error Image

Update 05.01.23

I tried to use a similar method, but with other commands, unfortunately another attempt also failed.

boot.s

bits 16

extern main

start:
    cli                     ; Disable the interrupts
    mov si, msg             ; SI now points to our message
    mov ah, 0x0E            ; Indicate BIOS we're going to print chars
.loop  lodsb                   ; Loads SI into AL and increments SI [next char]
    or al, al               ; Checks if the end of the string
    jz halt                 ; Jump to halt if the end
    int 0x10                ; Otherwise, call interrupt for printing the char
    jmp .loop               ; Next iteration of the loop

halt:  hlt                     ; CPU command to halt the execution
msg:   db "Hello, World!", 0   ; Our actual message to print

section .text
global puts

puts:
    ; Insert code for the puts function here
times 510 - ($ - $$) db 0

; Add the boot signature
dw 0xAA55

bootloader.c


//extern void puts(const char* str);

int main(void)
{
    puts("Hello user");
    return 0;
}

Compile using tools

First Step

nasm -f elf64 boot.s -o boot.o

gcc -c -m32 -o bootloader.o bootloader.c

ld -Ttext=0x7C00 -o boot.elf boot.o bootloader.o

Second Step

nasm -f bin boot.s -o boot.o

gcc -c -m32 -o bootloader.o bootloader.c

gcc -m32 -nostdlib -nostartfiles -Wl,-Ttext,0x7C00 boot.o bootloader.c -o boot.elf

Third Step

nasm -f elf32 boot.s -o boot.o

gcc -c -m32 -o bootloader.o bootloader.c

ld -Ttext=0x7C00 -o boot.elf boot.o bootloader.o

My Error:

On First & Third step

ld: bootloader.o:bootloader.c:(.text+0xa): undefined reference to `__main' ld: bootloader.o:bootloader.c:(.text+0x16): undefined reference to `puts'

On Seconds step

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Klubuntu\AppData\Local\Temp\ccojaMyU.o:bootloader.c:(.text+0xa): undefined reference to `__main' c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Klubuntu\AppData\Local\Temp\ccojaMyU.o:bootloader.c:(.text+0x16): undefined reference to `puts' collect2.exe: error: ld returned 1 exit status

Sorry for all grammar mistakes Thanks from above help

Klubuntu
  • 11
  • 6
  • See on title ;) – Klubuntu Dec 30 '22 at 19:35
  • 2
    So did anything go wrong with the linking? If so, what did? AFAIK, it should work, at least if compiled on a platform where the C compilers doesn't like underscore-prefix the generated symbols. On some platforms it does, and then you'd need to call `_main` instead. – Petr Skocik Dec 30 '22 at 19:37
  • I'm using nasm & mingw (8.10) – Klubuntu Dec 30 '22 at 19:40
  • 1
    Why is your `boot3.s` written in 16 bit assembly? Did you receive an error when you wrote this code? – fuz Dec 30 '22 at 19:41
  • 2
    You're trying to boot an OS kernel (under `qemu`), yes? See: [How can I fix my VBE implementation for my OS?](https://stackoverflow.com/q/73101699/5382650) It has bootloader code that sets up protected mode and transfers control to a C module. It even links to a github repo that has the full code. There are many more examples on SO. – Craig Estey Dec 30 '22 at 19:41
  • 1
    you cant call external file. You can call external function – 0___________ Dec 30 '22 at 20:20
  • MinGW doesn't normally use ELF object files since it's on Windows, but maybe your linker can read ELF inputs and make a Windows PE executable. I guess you have a 32-bit GCC so `-m32` is the default? You tagged this [gcc-warning] but you haven't quoted any warnings or error messages, or said what happened when you tried it. A [mcve] needs to describe the results. – Peter Cordes Dec 31 '22 at 03:21
  • @CraigEstey, yes, I'm planned to use compiled code on QEMU from ISO File – Klubuntu Jan 01 '23 at 19:53
  • @fuz You have another idea ? – Klubuntu Jan 03 '23 at 14:20
  • @Klubuntu Can you answer my question first? Why are you writing 16 bit assembly? Are you intending to write a boot loader? If so, you should know that you can't just build a Linux binary and expect it work as a boot loader. So many things are wrong here. – fuz Jan 03 '23 at 14:26
  • Thanks @fuz for reply. I use a 16 bit system because I model how Windows boots, it too has a 16 bit program when loading, which loads 32 bit and then 64 bits want to do not bootloader Linux but my system – Klubuntu Jan 04 '23 at 01:01
  • @Klubuntu If you want to write a boot loader, you need to use a freestanding toolchain and write your own linker scripts. You also cannot just call `printf`; you have to write your own `printf`. Unfortunately explaining how to set this up is too much for a Stack Overflow answer, but you can find lots of tutorials on the internet. – fuz Jan 04 '23 at 12:13
  • @PeterCordes my posted edited & I'm added error commands – Klubuntu Jan 04 '23 at 21:13
  • main exported by .c is probably called `_main` not `main` – pm100 Jan 04 '23 at 22:03
  • @pm100, change word ? – Klubuntu Jan 04 '23 at 23:11

0 Answers0