Page 1 of 1

uefi: undefined reference to [EFI functions] when linking

Posted: Fri Nov 06, 2020 2:49 pm
by austanss
I copied someone's UEFI bootloader from GitHub. Now before you go telling me how I should just use GRUB or something else instead, let me explain that I copied it so I could have easy access to the source, while having a minimal bootloader. As well, this bootloader came with an example kernel that explained how to print text, so yay.

However when compiling the bootloader I get numerous link errors:

Code: Select all

/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x67): undefined reference to `VSPrint'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0xae): undefined reference to `Print'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0xcd): undefined reference to `VPrint'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x130): undefined reference to `efi_call5'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x1a0): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x20e): undefined reference to `efi_call5'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x2ce): undefined reference to `InitializeLib'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x301): undefined reference to `efi_call4'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x35c): undefined reference to `efi_call2'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x434): undefined reference to `efi_call6'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x516): undefined reference to `efi_call2'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x63f): undefined reference to `efi_call2'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/main.o:main.c:(.text+0x712): undefined reference to `efi_call2'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x8e): undefined reference to `efi_call2'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x10a): undefined reference to `efi_call4'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x18a): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x1f3): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x262): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x2bc): undefined reference to `efi_call1'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x353): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x6b2): undefined reference to `efi_call5'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x771): undefined reference to `efi_call1'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x880): undefined reference to `efi_call1'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x8da): undefined reference to `efi_call1'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/loader.o:loader.c:(.text+0x931): undefined reference to `efi_call1'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/elf.o:elf.c:(.text+0x9da): undefined reference to `efi_call2'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/elf.o:elf.c:(.text+0xa95): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/elf.o:elf.c:(.text+0xafa): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/elf.o:elf.c:(.text+0xbbc): undefined reference to `efi_call2'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/elf.o:elf.c:(.text+0xc29): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/elf.o:elf.c:(.text+0xc8b): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/elf.o:elf.c:(.text+0xd02): undefined reference to `efi_call2'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/elf.o:elf.c:(.text+0xd67): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/elf.o:elf.c:(.text+0xdc9): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/error.o:error.c:(.text+0x1b): undefined reference to `StatusToString'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/fs.o:fs.c:(.text+0x43): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/graphics.o:graphics.c:(.text+0x27): undefined reference to `efi_call1'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/graphics.o:graphics.c:(.text+0x269): undefined reference to `efi_call4'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/graphics.o:graphics.c:(.text+0x383): undefined reference to `efi_call5'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/graphics.o:graphics.c:(.text+0x447): undefined reference to `efi_call2'
/usr/bin/x86_64-w64-mingw32-ld: /usr/lib/crt0-efi-x86_64.o: in function `_start':
(.text+0x9): undefined reference to `ImageBase'
/usr/bin/x86_64-w64-mingw32-ld: (.text+0x10): undefined reference to `_DYNAMIC'
/usr/bin/x86_64-w64-mingw32-ld: (.text+0x19): undefined reference to `_relocate'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/serial.o:serial.c:(.text+0x58): undefined reference to `efi_call7'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/serial.o:serial.c:(.text+0xd8): undefined reference to `efi_call3'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/serial.o:serial.c:(.text+0x18b): undefined reference to `StrLen'
/usr/bin/x86_64-w64-mingw32-ld: bin/obj/serial.o:serial.c:(.text+0x1eb): undefined reference to `efi_call3'
My compile options:

Code: Select all

bootloader:
	$(MINGW) -c $(BOOTLOADER_SRC_DIR)/loader.c -o $(OBJ_DIR)/loader.o
	$(MINGW) -c $(BOOTLOADER_SRC_DIR)/data.c -o $(OBJ_DIR)/data.o
	$(MINGW) -c $(BOOTLOADER_SRC_DIR)/elf.c -o $(OBJ_DIR)/elf.o
	$(MINGW) -c $(BOOTLOADER_SRC_DIR)/error.c -o $(OBJ_DIR)/error.o
	$(MINGW) -c $(BOOTLOADER_SRC_DIR)/fs.c -o $(OBJ_DIR)/fs.o
	$(MINGW) -c $(BOOTLOADER_SRC_DIR)/graphics.c -o $(OBJ_DIR)/graphics.o
	$(MINGW) -c $(BOOTLOADER_SRC_DIR)/main.c -o $(OBJ_DIR)/main.o
	$(MINGW) -c $(BOOTLOADER_SRC_DIR)/serial.c -o $(OBJ_DIR)/serial.o
	# ld  -nostdlib -znocombreloc -T /usr/lib/elf_x86_64_efi.lds -shared -Bsymbolic -L /usr/lib -l:libgnuefi.a -l:libefi.a -o main.so
	# objcopy -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .reloc --target=efi-app-x86_64 main.so main.efi
	x86_64-w64-mingw32-gcc -nostdlib -shared -Wl,--subsystem,10 -e efi_main -o bootloader.efi $(OBJ_DIR)/main.o $(OBJ_DIR)/loader.o $(OBJ_DIR)/data.o $(OBJ_DIR)/elf.o $(OBJ_DIR)/error.o $(OBJ_DIR)/fs.o $(OBJ_DIR)/graphics.o /usr/lib/crt0-efi-x86_64.o $(OBJ_DIR)/serial.o -lgcc
Quite stumped on this issue, didn't have this issue in my original UEFI app.

GitHub repo: https://github.com/microNET-OS/microCORE

Re: uefi: undefined reference to [EFI functions] when linkin

Posted: Fri Nov 06, 2020 3:29 pm
by Octocontrabass
You're using a PE toolchain with the Microsoft ABI, but gnu-efi is designed for an ELF toolchain with the System V ABI.

You'll need to adapt the code to work without gnu-efi. Mostly that means replacing uefi_call_wrapper() with direct function calls, but you might also need to find (or write) new UEFI headers.

Re: uefi: undefined reference to [EFI functions] when linkin

Posted: Fri Nov 06, 2020 4:32 pm
by austanss
I fixed the compiling options but now I get this error:

Re: uefi: undefined reference to [EFI functions] when linkin

Posted: Fri Nov 06, 2020 4:38 pm
by nexos
Run QEMU with -net none. Then it will drop you to the EFI shell.

Re: uefi: undefined reference to [EFI functions] when linkin

Posted: Fri Nov 06, 2020 5:21 pm
by austanss
I try to manually run my EFI application, I get this error:
unknown.png
I believe it is a compiling error, I simplified the code to a simple "Hello" and it still doesn't work.

Re: uefi: undefined reference to [EFI functions] when linkin

Posted: Fri Nov 06, 2020 6:47 pm
by zaval
I fixed the compiling options
seems, you didn't. show your boobs compiling options. your qemu is qemu-system-x86_64?

PS. 232 bytes, man, I bet it's not even a proper PE file. are you on Windows? try dumpbin /headers on your "efi" file.

Re: uefi: undefined reference to [EFI functions] when linkin

Posted: Fri Nov 06, 2020 8:11 pm
by austanss
zaval wrote:
I fixed the compiling options
seems, you didn't. show your boobs compiling options. your qemu is qemu-system-x86_64?

PS. 232 bytes, man, I bet it's not even a proper PE file. are you on Windows? try dumpbin /headers on your "efi" file.
No, I'm using Linux (ubuntu).

The new compiling options are on GitHub, yes my qemu is x86_64

Re: uefi: undefined reference to [EFI functions] when linkin

Posted: Sat Nov 07, 2020 8:58 am
by austanss
OK, I'm just stupppuid. I forgot to link the object files together omfg. it loads and starts now i just have some runtime errors i need to fix