1

I've got this code:

char* vidmem = (char*)0xb8000;

int main()
{
    vidmem[0] = 'x';
}

but this acts like vidmem is not initalized. if instead i do something like this:

char* vidmem;

int main()
{
    vidmem = (char*)0xb8000;

    vidmem[0] = 'x';
}

this works perfectly. Why?

I use this lines to compile and link:

gcc -c main.c -o main.o -ffreestanding -fno-exceptions -m64
gcc -m64 -Wl,--build-id=none -static -nostdlib -nodefaultlibs -lgcc main.obj [...] -T linker.ld -o out.bin

using this linker file:

ENTRY(_start)

SECTIONS
{
    . = 0x7C00;

    .bss :
    {
        *(.bss);
    }

    .text :
    {
        *(.text);
    }
}

There is actually some assembly code calling this C file but it should not matter. Am i doing something wrong with gcc? How can i fix it?

4
  • 2
    Why are you asking a new question? stackoverflow.com/questions/56430956/… this one is the same and does not have a proper answer (even though you have accepted some workaround), yet you have got many valuable comments there. The reason is probably in your (lack of) startup code for copying the data segment. Commented Jun 3, 2019 at 17:12
  • 1
    Possible duplicate of Can't use global variables with gcc Commented Jun 3, 2019 at 17:26
  • @AjayBrahmakshatriya that is half of a question but no one was responding right to the question and so i've asked the secon half here Commented Jun 3, 2019 at 17:28
  • 2
    That is the problem of the question - it is incomplete. So you have to complete it and not post another one. Also if you are not satisfied with the answer - do not accept it Commented Jun 3, 2019 at 17:29

2 Answers 2

1

You haven't put your data or rodata sections in your linker script. Check your main.o file for what section vidmem is in and make sure you set that up correctly in your script.

Sign up to request clarification or add additional context in comments.

7 Comments

Why would replacing the initialization with an assignment change that?
Nope.. i've added both data and rodata and evene tried with just one at a time but neither of this solution works..
@Mimmo - does your runtime relocate the data section correctly? It has to work if it's included in the executable.
@Barmar - because it gets initialized? Normally the data section would get relocated before main, but if it doesn't, putting the initialization in there will at least put something useful in the memory location the compiler expected it to be in.
I am actually kinda new to this enviroment i don't know.. how can i check?
|
0

Not only sections in the linked file are needed but also the initialization code which will copy the data.

In the linked script you also need to show there to place the data and there the values in the RO memory are stored( after the closing bracket

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.