Address of (&) not returning correct address

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
itsankitkp
Posts: 1
Joined: Mon Jul 24, 2023 12:25 pm

Address of (&) not returning correct address

Post by itsankitkp »

I had basic setup on 32 bit kernel setup, things worked fine there. Now when I try to do same on 64 bit mode, I am facing weird issue.
Basically whenever I am trying to print string, & is not returning correct address. Printing individual chars works fine, or if it pass address as obtained from objdump.

objdump results for ro data
Contents of section .rodata:
10020d 48656c6c 6f0a00 Hello..

printing address of string (using gdb) yield 0x1022b8. This is what gets passed if I call my write_string("Hello"...) function.
if I pass write_string((char*) 10020d, ...) it works fine.

linker configuration:
OUTPUT_FORMAT(elf64-x86-64)
ENTRY(start)
SECTIONS
{
. = 1M;
.text BLOCK(128) : ALIGN(32)
{
*(.multiboot)
*(.text)

}
.rodata : { *(.rodata) }
.data : { *(.data) }
.bss : { *(.bss) }
}

flags:
CFLAGS = -m64 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \
-nostartfiles -nodefaultlibs -Wall -Wextra -Werror -g -c
LDFLAGS = -T link.ld -melf_x86_64
AS = nasm
ASFLAGS = -f elf64

Not sure how I am supposed to debug this. Any pointers will help.
Attachments
ch2.zip
(3.75 KiB) Downloaded 36 times
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: Address of (&) not returning correct address

Post by Octocontrabass »

itsankitkp wrote:Now when I try to do same on 64 bit mode,
But you're not in 64-bit mode. The CPU is in 32-bit mode when GRUB loads your kernel, and you didn't switch the CPU to 64-bit mode, so you're running 64-bit code in 32-bit mode.
Post Reply