OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:55 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: relocation R_X86_64_32S against '.text' can not be used
PostPosted: Fri Jul 16, 2021 12:02 pm 
Offline
Member
Member
User avatar

Joined: Tue Jul 13, 2021 5:15 am
Posts: 35
Location: Metaverse
Hello OSDev forum!
I got a problem about GDT table when I am trying to compile.
Code:
gdt.o: relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIE


My makefile code:
Code:
INTERNALLDFLAGS := \
   -fno-pic -fPIE \
   -Wl,-static,-pie,--no-dynamic-linker,-ztext \
   -static-pie    \
   -nostdlib      \
   -Tlinker.ld    \
   -z max-page-size=0x1000

INTERNALCFLAGS  :=       \
   -I${INCLUDE_DIR}     \
   -std=gnu11           \
   -ffreestanding       \
   -fno-stack-protector \
   -fno-pic -fPIE       \
   -mno-80387           \
   -mno-mmx             \
   -mno-3dnow           \
   -mno-red-zone

build: $(KERNEL)

$(KERNEL): $(OBJ)
   $(CC) $(INTERNALLDFLAGS) $(OBJ) -o $@

%.o: %.c ${C_HEADERS}
   $(CC) $(CFLAGS) $(INTERNALCFLAGS) -c $< -o $@

%.o: %.asm
   ${NASM} $< -f elf64 -o $@


Code that is causing relocation error

Code:
void init_gdt()
{
    __lgdt(&g_gdt);

    asm volatile(
        "movq %%rsp, %%rax\n"
        "pushq $16\n"
        "pushq %%rax\n"
        "pushfq\n"
        "pushq $8\n"
        "pushq $1f\n"
        "iretq\n"
        "1:\n"
        "movw $16, %%ax\n"
        "movw %%ax, %%ds\n"
        "movw %%ax, %%es\n"
        "movw %%ax, %%fs\n"
        "movw %%ax, %%gs\n" ::
            : "memory", "rax");
}


I am using gcc cross compiler like in GCC_Cross-Compiler tutorial
Do anyone knows what's wrong?

_________________
The code does get a bit quirky at night.


Top
 Profile  
 
 Post subject: Re: relocation R_X86_64_32S against '.text' can not be used
PostPosted: Fri Jul 16, 2021 2:57 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
What does linker.ld look like

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: relocation R_X86_64_32S against '.text' can not be used
PostPosted: Fri Jul 16, 2021 3:18 pm 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
22OsC wrote:
Code:
        "pushq $1f\n"

This instruction requires the R_X86_64_32S relocation. You need to either choose a position-independent way of getting that address or stop trying to make your kernel position-independent.

Why are you trying to make your kernel position-independent?


Top
 Profile  
 
 Post subject: Re: relocation R_X86_64_32S against '.text' can not be used
PostPosted: Fri Jul 16, 2021 6:07 pm 
Offline
Member
Member
User avatar

Joined: Tue Jul 13, 2021 5:15 am
Posts: 35
Location: Metaverse
nexos wrote:
What does linker.ld look like


Oops i forgot

Code:
ENTRY(kernel_main)

OUTPUT_FORMAT(elf64-x86-64)

SECTIONS
{
    /**************** KERNEL SECTIONS ****************/
    . = 0xffffffff80000000 + 200000;
    _kernel_start =.;

    /**************** STIVALE2 HEADER ****************/
    .stivale2hdr :
    {
        KEEP(*(.stivale2hdr))
    }

    .text :
    {
        *(.text*)
    }

    .rodata :
    {
        *(.rodata*)
    }

    .data :
    {
        *(.data*)
    }

    .bss :
    {
        *(COMMON)
        *(.bss*)
    }
    _kernel_end = .;
}

_________________
The code does get a bit quirky at night.


Top
 Profile  
 
 Post subject: Re: relocation R_X86_64_32S against '.text' can not be used
PostPosted: Fri Jul 16, 2021 6:26 pm 
Offline
Member
Member
User avatar

Joined: Tue Jul 13, 2021 5:15 am
Posts: 35
Location: Metaverse
Octocontrabass wrote:
22OsC wrote:
Code:
        "pushq $1f\n"

This instruction requires the R_X86_64_32S relocation. You need to either choose a position-independent way of getting that address or stop trying to make your kernel position-independent.

Why are you trying to make your kernel position-independent?


I tried to compile with -fno-pic but I am getting
Code:
stivale.c:16:(.text+0x1): relocation truncated to fit: R_X86_64_32 against `.rodata'
string.c:16:(.text+0x269): relocation truncated to fit: R_X86_64_32 against `.rodata.str1.1'
stivale2.c:72:(.text+0x179): relocation truncated to fit: R_X86_64_32 against `.rodata.str1.1'
and many, many more errors

I don't know why is doing this, I mean, nothing here it's wrong
Code:
        void (*term_write)(const char *string, size_t length) = (void *)term_str_tag->term_write;
        term_write("Unable to get memory map!", 25); <----- line 72 in stivale2.c

_________________
The code does get a bit quirky at night.


Top
 Profile  
 
 Post subject: Re: relocation R_X86_64_32S against '.text' can not be used
PostPosted: Fri Jul 16, 2021 6:40 pm 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
22OsC wrote:
Code:
relocation truncated to fit: R_X86_64_32

The default "small" memory model requires all symbols to have addresses in the 0-0x7FFFFFFF range.

22OsC wrote:
Code:
    . = 0xffffffff80000000 + 200000;

All of your symbols have addresses above 0xFFFFFFFF80000000. You probably want to use the "kernel" memory model instead.

Pass "-mcmodel=kernel" to the compiler. There's more information on the wiki. There's even more information in the GCC manual.


Top
 Profile  
 
 Post subject: Re: relocation R_X86_64_32S against '.text' can not be used
PostPosted: Fri Jul 16, 2021 7:55 pm 
Offline
Member
Member
User avatar

Joined: Tue Jul 13, 2021 5:15 am
Posts: 35
Location: Metaverse
Octocontrabass wrote:
22OsC wrote:
Code:
relocation truncated to fit: R_X86_64_32

The default "small" memory model requires all symbols to have addresses in the 0-0x7FFFFFFF range.

22OsC wrote:
Code:
    . = 0xffffffff80000000 + 200000;

All of your symbols have addresses above 0xFFFFFFFF80000000. You probably want to use the "kernel" memory model instead.

Pass "-mcmodel=kernel" to the compiler. There's more information on the wiki. There's even more information in the GCC manual.


Oh thanks, I didn't knew that #-o

_________________
The code does get a bit quirky at night.


Top
 Profile  
 
 Post subject: Re: relocation R_X86_64_32S against '.text' can not be used
PostPosted: Fri Jul 16, 2021 9:58 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
22OsC wrote:
Code:
void init_gdt()
{
    __lgdt(&g_gdt);

    asm volatile(
        "movq %%rsp, %%rax\n"
        "pushq $16\n"
        "pushq %%rax\n"
        "pushfq\n"
        "pushq $8\n"
        "pushq $1f\n"
        "iretq\n"
        "1:\n"
        "movw $16, %%ax\n"
        "movw %%ax, %%ds\n"
        "movw %%ax, %%es\n"
        "movw %%ax, %%fs\n"
        "movw %%ax, %%gs\n" ::
            : "memory", "rax");
}

You know, this can be done easier and position independent in a separate function:
Code:
/* void load_desc(uint16_t cs, uint16_t ds); */
.global load_desc
.type load_desc,@function
load_desc:
  movw %si, %ds
  movw %si, %es
  movw %si, %fs
  movw %si, %gs
  movw %si, %ss
  movq (%rsp), %rax
  andl $0xffff, %edi
  pushq %rdi
  pushq %rax
  lretq $8
.size load_desc, .-load_desc


You must put that into a separate function rather than inline assembler, because you need to get at the return address. There is no need to load CS before the other segments, so you can do it last, and as code address you can just use the return address.

_________________
Carpe diem!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Amazonbot [bot], Bing [Bot], Octocontrabass and 90 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