The Place to Start for Operating System Developers
It is currently Fri May 24, 2019 9:07 am

All times are UTC - 6 hours

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: UEFI debugging with GDB in QEMU
PostPosted: Tue Jun 12, 2018 11:39 am 

Joined: Tue Jun 12, 2018 11:10 am
Posts: 1
I'm trying to debug UEFI with GDB in QEMU, but can't get the debug symbol loaded correctly. I've followed the tutorial from http://wiki.osdev.org/Debugging_UEFI_ap ... s_with_GDB
and have already seen another related forum thread (viewtopic.php?f=1&t=31192) but sadly couldn't apply my problem to it.

Here's the main.c file:

#include <efi.h>
#include <efilib.h>

efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
    EFI_LOADED_IMAGE *loaded_image = NULL;
    EFI_STATUS status;

    InitializeLib(image, systab);
    status = uefi_call_wrapper(systab->BootServices->HandleProtocol,
                              (void **)&loaded_image);
    if (EFI_ERROR(status)) {
        Print(L"handleprotocol: %r\n", status);

    Print(L"Image base: 0x%lx\n", loaded_image->ImageBase);

    int wait = 1;
    while (wait) {
        __asm__ __volatile__("pause");

    return EFI_SUCCESS;

Ouptut of the UEFI App:
Image base: 0x2EA1D000

Here's how I try to load GDB and the error that occurs:
[piri@XPIRI efi_gdb]$ gdb BOOTx64.EFI
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
Find the GDB manual and other documentation resources online at:
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from BOOTx64.EFI...(no debugging symbols found)...done.
(gdb) info files
Symbols from "/home/piri/osdev/efi_gdb/BOOTx64.EFI".
Local exec file:
        `/home/piri/osdev/efi_gdb/BOOTx64.EFI', file type pei-x86-64.
        Entry point: 0x3000
        0x0000000000003000 - 0x0000000000008950 is .text
        0x0000000000009000 - 0x000000000000900a is .reloc
        0x000000000000a000 - 0x000000000000bce0 is .data
        0x000000000000c000 - 0x000000000000c100 is .dynamic
        0x000000000000d000 - 0x000000000000de58 is .rela
        0x000000000000e000 - 0x000000000000e210 is .dynsym
(gdb) file
No executable file now.
No symbol file now.
(gdb) add-symbol-file debug.BOOTx64.EFI 0x2EA20000 -s .data 0x2EA27000
add symbol table from file "debug.BOOTx64.EFI" at
        .text_addr = 0x2ea20000
        .data_addr = 0x2ea27000
(y or n) y
Reading symbols from debug.BOOTx64.EFI...done.
(gdb) set architecture i386:x86-64:intel
The target architecture is assumed to be i386:x86-64:intel
(gdb) target remote :1234
Remote debugging using :1234
warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.
0x000000002ea200cf in efi_main ()
(gdb) set variable wait = 0
No symbol "wait" in current context.

And lastly my Makefile:

ARCH            = x86_64

OBJS            = main.o
TARGET          = BOOTx64.EFI

EFIINC          = /usr/include/efi
EFIINCS         = -I$(EFIINC) -I$(EFIINC)/$(ARCH) -I$(EFIINC)/protocol
EFILIB          = /usr/lib
EFI_CRT_OBJS    = $(EFILIB)/crt0-efi-$(ARCH).o
EFI_LDS         = $(EFILIB)/elf_$(ARCH)_efi.lds

CFLAGS          = $(EFIINCS) -fno-stack-protector -fpic \
          -fshort-wchar -mno-red-zone -Wall
ifeq ($(ARCH),x86_64)

LDFLAGS         = -nostdlib -znocombreloc -T $(EFI_LDS) -shared \
          -Bsymbolic -L $(EFILIB) $(EFI_CRT_OBJS)

SECTIONS = .text .sdata .data .dynamic .dynsym .rel .rela .reloc

DEBUG_SECTIONS = .debug_info .debug_abbrev .debug_loc .debug_aranges \
                 .debug_line .debug_macinfo .debug_str

.PHONY : all

all: $(TARGET) debug.$(TARGET)

BOOTx64.so: $(OBJS)
   ld $(LDFLAGS) $(OBJS) -o $@ -lefi -lgnuefi

%.EFI: %.so
   objcopy $(foreach sec,$(SECTIONS), -j $(sec))\
        --target=efi-app-$(ARCH) $^ $@

debug.%.EFI: %.so
   objcopy $(foreach sec,$(SECTIONS) $(DEBUG_SECTIONS), -j $(sec))\
        --target=efi-app-$(ARCH) $^ $@

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: Bing [Bot] and 7 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group