Why is gcc resolving the address of a string during compilation?

0

I'm trying to do some bare metal programming on a Raspberry Pi 3B. I still have not figured out the correct memory addresses for the UART controls so please just ignore those. I am experiencing a strange compilation issue though.

Here is the code I am trying to compile (not link):

void pstr(char* str) {
    unsigned int* AUX_MU_IO_REG = (unsigned int*)0x7E215040;
    unsigned int* AUX_MU_LSR_REG = (unsigned int*)0x7E215054;
    while (*str != 0) {
      while (!(*AUX_MU_LSR_REG & 0x00000020)) {
      }
      *AUX_MU_IO_REG = (unsigned int)((unsigned char)*str);
      str++;
    }
    return;
}
signed int kmain(unsigned int argc, char* argv[], char* envp[]) {
    char* text = "Test Output String\n";
    unsigned int* AUXENB = (unsigned int*)0x7E215004;
    *AUXENB = 0x00000001;
    pstr(text);
    return 0;
}

My addresses are not correct and invalid, but that is not the point. For some reason, the string "Test Output String\n" is being resolved to an address in the object file.

It is being compiled with the command:

aarch64-unknown-linux-gnu-gcc -Wall -Wextra -std=c99 -O2 -march=armv8-a -mtune=cortex-a53 -mlittle-endian -ffreestanding -nostdlib -nostartfiles -Wno-unused-parameter -fno-stack-check -fno-stack-protector src/kernel/base.c -c -o src/kernel/base.o

Interestingly, it doesn't happen if I compile with "-O0".

Here is what it looks like with "-O2" using "aarch64-unknown-linux-gnu-objdump -d ./src/kernel/base.o":

0000000000000040 <kmain>:
  40:   d28a0a80    mov     x0, #0x5054                 // #20564
  44:   f2afc420    movk    x0, #0x7e21, lsl #16
  48:   d28a0084    mov     x4, #0x5004                 // #20484
  4c:   f2afc424    movk    x4, #0x7e21, lsl #16
  50:   b9400000    ldr w0, [x0]

It crashes at "ldr w0, [x0]" because the address 0x7e215054 is not valid. I just don't know why the compiler would even be putting that there. It should be symbol to the data in .rodata so that it can be placed in the correct location by my linker script.

c
gcc
compilation
arm
raspberry-pi3
asked on Stack Overflow Dec 21, 2020 by Echelon X-Ray • edited Dec 21, 2020 by Bill Lynch

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0