GDB is not stopping at breakpoint when debugging a PIE kernel - c

I have a position independent kernel that gets compiled and linked like this:
gcc -Og -g -Werror -nostdlib -o kernel.o -c kernel/main.c
gcc -nostdlib -ffreestanding -e kmain -o kernel.elf kernel.o
The problem I have is that when I launch the bootloader in qemu like this:
qemu-system-x86_64 -s -S -bios /usr/share/ovmf/OVMF.fd -drive file=/os.img,format=raw
and then do this in gdb:
target remote :1234
symbol-file kernel.elf
break kmain
continue
layout next
it will not stop at the breakpoint, it just keeps continuing.
This is how I call kmain from the bootloader:
int (*kmain)(void*) = (int(*)(void*)) (buf + elf->entry);
stat = uefi_call_wrapper(BS->ExitBootServices, 2, IH, mapkey);
kmain(&bootinfo);
where buf is the beginning of the kernel.elf file and this is the kmain()
int kmain(Bootinfo *Bootinfo) {
int x = 1;
int y = x;
return 0;
}
Someone told me this:
"If your kernel is position-independent or relocatable and your bootloader can handle the runtime requirements of position-independent or relocatable code, you need to tell GDB that you've loaded your kernel at a different base address."
How can I tell gdb that my kernel is loaded at a different base address?
Is the base address in this case buf?
if I disassemble kmain in gdb this is what it shows:
gdb kmain

in gdb the command symbol-file can be given a -o offset argument:
symbol-file [ filename [ -o offset ]]
If an optional offset is specified, it is added to the start address of each section in the symbol file. This is useful if the program is relocated at runtime, such as the Linux kernel with kASLR enabled.
source: https://sourceware.org/gdb/onlinedocs/gdb/Files.html

Related

How can I use UEFI Runtime Services?

I want to learn more about firmware developement. I already know how to write assembly programs for the old BIOS and now I wanted to start with UEFI. I managed to compile and emulate a Hello World program, and now I was trying to write a program which displays on the screen the current time using the Runtime Service GetTime(). However when I use this function the program hangs, as if it wasn't installed during PI.
Here is the code:
#include <efi.h>
#include <efilib.h>
#include <efiapi.h>
//gBS: SystemTable->BootServices;
//gRS: SystemTable->RuntimeServices;
EFI_STATUS
efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE* systab)
{
EFI_TIME* time;
InitializeLib(image, systab);
RT->GetTime(time, NULL);
Print(L"Time %u\n", time->Hour);
return EFI_SUCCESS;
}
Do you have any clue about what I've done wrong?
Here is the code I use to compile and emulate in case you need:
gcc -I/usr/include/efi -I/usr/include/efi/x86_64/ -fpic -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -c main.c -o main.o
ld -shared -Bsymbolic -L/usr/lib -T/usr/lib/elf_x86_64_efi.lds /usr/lib/crt0-efi-x86_64.o main.o -o main.so -lgnuefi -lefi
objcopy -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 main.so main.efi
uefi-run -b /usr/share/edk2-ovmf/x64/OVMF.fd -q /usr/bin/qemu-system-x86_64 main.efi
If you are using gnu-efi, use uefi_call_wrapper() to call UEFI functions.
RT->GetTime(time, NULL); // Program hangs
uefi_call_wrapper(RT->GetTime, 2, time, NULL); // Okay
The reason is the different calling convention between UEFI (which uses Microsoft x64 calling convention) and Linux (which uses System V amd64 ABI). By default, gcc will generate the code in Linux format, so we need to explicitly tell it to generate it in UEFI format.
You can see the difference by peforming an objdump.
I think you missed to initialize RT.
RT = SystemTable->RuntimeServices;
Your code is very similar to one of the examples (the one at section 4.7.1) of the Unified Extensible Firmware Interface Specification 2.6. I doubth you haven't read it, but just in case.
https://www.uefi.org/sites/default/files/resources/UEFI%20Spec%202_6.pdf

Compiling statically libc C code and ASM code

I have ASM code:
extern my_func
extern printf
extern exit
global _start
section .data
...
section .text
_start:
...
call printf
...
call my_func
...
call exit
and C code:
int my_func(int a, int b)
{
return a+b;
}
I'm using fedora on 64-bit machine. I want the executable be 32-bit.
For dynamic linking I do:
nasm -f elf32 asm.asm ; this gives me asm.o
gcc -m32 -Wall -c c_code.c ; this gives me c_code.o
ld c_code.o asm.o -melf_i386 -L /usr/lib/ -lc -I /lib/ld-linux.so.2 ; this gives me a.out which runs fine and weights 5601 bytes.
What I want to do is link libc statically. I do the following:
gcc -o a2.out -m32 -static -m32 asm.o c_code.o
And I get error:
asm.o: In function `_start':
asm.asm:(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../lib64/crt1.o:(.text+0x0):
first defined here
collect2: error: ld returned 1 exit status
Then I change _start to main in ASM code and the whole thing links fine! ldd shows "not a dynamic executable". But the file created weights 721067 bytes! I think that it compiles statically a lot of unnecessary code.
So, my 1st question is:
1) How can I link statically only libc for the required printf and exit functions?
When I try
gcc -m32 -o a3.out -lc asm.o c_code.o ; ASM file has main instead of _start
I get a file that weights 7406 bytes. ldd shows the same dynamic libraries as for the a.out which weights 5601 bytes.
2) Why is that difference? Looks like some additional code that "connects" _start with main in my code...
3) What is the difference between linking with gcc and ld?
Thanks a lot for your attention!
1) How can I link statically only libc for the required printf
and exit functions?
Try compiling with -nostartfiles -static -nostdlib -lc which will avoid adding crt1.o and crtend.o. But keep in mind that this will disable all Glibc initialization code so some Glibc functions will fail to work.
2) Why is that difference? Looks like some additional code
that "connects" _start with main in my code...
GCC adds start files (crt*.o) which perform initialization. See the many online articles for details (e.g. this one).
3) What is the difference between linking with gcc and ld?
Already answered above but in general you can run gcc -v and inspect ld's (or collect2's) arguments.

Linking kernel - i386 architecture of input file is incompatible with i386:x86_64 output

I am trying to move on from my assembly file kernel stage to my C file kernel stage (finally...). But, I am having some trouble in the process of linking my compiled C kernel to my compiled assembly kernel entry program.
Here is the code for my kernel_entry.asm file.
[BITS 32] ; Starting in 32 bit protected mode
[EXTERN main] ; Extern to C file main function
call main ; Invoke main in our C kernel
jmp $ ; Jump here - Infinite loop
Here is the code for my kernel.c file.
void main() {
char* video_memory = (char*) 0xB8000;
*video_memory = 'X';
}
Here are the command lines I am using to compile them.
nasm -f elf -o kernel_entry.o kernel_entry.asm
gcc -ffreestanding -c kernel.c -o kernel.o
ld -o kernel.bin -Ttext 0x0500 kernel_entry.o kernel.o --oformat binary
The last command line gives me this error.
ld: i386 architecture of input file `kernel_entry.o' is incompatible with i386:x86-64 output
ld: warning: cannot find entry symbol _start; defaulting to 0000000000000500
Note: I am loading my kernel to the address and offset 0x0000:0x0500, which is why I use -Ttext 0x0500, I am unsure why the second ld warning appears but for now it seems unimportant (although if you offer any help in that regard it would be appreciated as well).
Can anyone tell me why I cannot link these files together? I am also running on Ubuntu dekstop 64 bit. Thank you in advance for any help you may give.
It looks as though you're compiling this code on a 64-bit system. As such, kernel.o is a 64-bit binary, and cannot be linked with the 32-bit kernel_entry.o.
Since you don't have any code in place to get the system into long mode, you probably want to compile the "kernel" as 32-bit code. Use -m32 to trigger this:
gcc -m32 -ffreestanding -c kernel.c -o kernel.o
^^^^

How to call c functions that call c standard library in nasm?

First I want to clarify that I know this question might have been answered hundreds of times. However after hours of Google search I simply couldn't find anything that's exactly what I want. Also even though I've been writing c programs for quite a while, I'm kind of new to nasm and ld. So I would really appreciate it if I can get a simple answer without having to read a whole nasm/ld tutorial or the complete manual.
What I want to do is:
say I have a function written in c that calls some function in the c standard library:
/* foo.c */
#include <stdio.h>
void foo(int i)
{
printf("%d\n", i);
}
I want to call this function in nasm so I tried this:
; main.asm
global _start
extern foo
section .text
_start:
push 1234567
call foo
add esp, 4
mov eax, 1
xor ebx, ebx
int 80h
Then I tried to compile them and run:
[user ~/Documents/asm/callc]#make all
nasm main.asm -felf
gcc -c foo.c -o foo.o -m32
ld -o main main.o foo.o -melf_i386 -lc
[user ~/Documents/asm/callc]#ls
foo.c foo.o main main.asm main.o Makefile
[user ~/Documents/asm/callc]#./main
bash: ./main: No such file or directory
[user ~/Documents/asm/callc]#bash main
main: main: cannot execute binary file
I didn't get any errors but apparently I couldn't run the executable output file.
If the c function doesn't call any library functions then the code above can be compiled and it will run without any problems. I also figured out a way to call library functions directly in nasm and use gcc to produce the final executable file. But none of them is exactly what I want.
EDIT:
1. I'm running 64-bit Ubuntu but I'm trying to write 32-bit programs so I used flags like -m32 and -melf_i386.
2. Output of file *:
[user ~/Documents/asm/sof]#file *
foo.c: C source, ASCII text
foo.c~: empty
foo.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
main.asm: C source, ASCII text
main.asm~: empty
main.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
Makefile: makefile script, ASCII text
Makefile~: makefile script, ASCII text
3. I really have no idea of how to tell ld to include the c standard library. I found something like -lglibc or -lc in some other posts. -lgibc doesn't work and -lc seems to be able to get rid of all errors and I probably thought it worked at first but maybe that's the problem since it probably doesn't link the correct library.
UPDATE
Adding -I/lib32/ld-linux.so.2 to the ld command solved my problem.
Below are commands to compile/assemble/link and run the program:
nasm main.asm -felf
gcc -c foo.c -o foo.o -m32
ld -o main main.o foo.o -melf_i386 -lc -I/lib32/ld-linux.so.2
./main
The C library provides code using the _start interface that starts the C runtime, calls main(), and shuts the runtime down. Hence if you intend to use the C library in your program you must not use the _start interface but provide a main() function.
This is the correct way to do it:
; main.asm
global main
extern foo
section .text
main:
push 1234567
call foo
add esp, 4
xor eax, eax
ret
Build with:
nasm -f elf32 -o main.o main.asm
gcc -m32 -o foo.o -c foo.c
gcc -m32 -o main main.o foo.o
Two remarks:
main() returns, instead of doing an exit system call, to allow the C runtime shutdown code to run.
gcc is used for linking. Internally gcc invokes ld with the appropriate parameters to link with the C library. These are platform specific and subject to change. Hence, don't use ld for this.

How to properly link *.asm file?

I have started to learn assembly language and currently making a simple asm program to call printf function in C in stdio.h.
I am unable to link the object file properly after assembling. The problem I am facing is
undefined reference to printf
After browsing other questions with similar problems on SO, I tried
nasm -f elf -l call.lst call.asm
gcc -o call call.o
After doing this, I get the error:
i386 architecture of input file call.o is compatible with x86_64 output.
How should I properly link the file? Currently, I am doing the following :-
nasm -f elf call.asm
ld -m elf_i386 -s -o call call.o
What changes should I make to the above line ?
If you're writing 32-bit assembly code, you need to tell GCC this:
$ gcc -m32 -o call call.o
Alternatively, if you're writing 64-bit assembly (with [BITS 64]), then you would:
$ nasm -f elf64 -l call.lst call.asm
$ gcc -m64 -o call call.o
This just worked for me:
test.s
[BITS 32]
extern printf
global main
main:
push message
call printf
add esp, 4
xor eax, eax
ret
message:
db "hello",0xA,0
Build
nasm -f elf test.s
gcc -m32 -o calltest test.o
./calltest

Resources