Page 1 of 1
ARM String Issues
Posted: Tue Jun 24, 2008 7:23 pm
by Alboin
I've been working on getting a small ARM piece to work on the QEMU versatilepb board, but have run into a roadblock.
Strings don't seem to be passed correctly to functions. Yes, I have .rodata in my linker script (attached), and yes the pointer is pointing to the correct space.
This is my main body code (The binary loaded by QEMU.)
Code: Select all
void start() __attribute__((naked));
void print_char(const char c);
void print_string(const char *str);
void start() {
asm volatile("mov sp, #0x10000");
char *meow = "meow\n";
print_string(meow);
/* Crash */
asm volatile("mov r0, #0x34000000");
asm volatile("mov pc, r0");
}
void print_char(const char c) {
*((unsigned char*)0x101F1000) = c;
}
void print_string(const char *str) {
while(*str != '\n') {
print_char(*str);
str++;
}
}
I compile with:
Code: Select all
arm-elf-gcc -c kernel.c -o kernel.o
arm-elf-ld -Tlink.ld -oformat=binary kernel.o -o kernel.bin --entry start
arm-elf-strip -I elf32-bigarm -O binary kernel.bin
I've checked the address that meow is at (0xa0) and its location in the file (0xa0) and they are the same. The file, if my ARM research is correct, is located at 0x0 by the firmware. print_char works perfectly fine on its own.
How can 0xa0 != 0xa0? The MMU is disabled by default, so...
Thanks again,
Alboin
Re: ARM String Issues
Posted: Tue Jun 24, 2008 10:51 pm
by pcmattman
Hi,
I've done a lot of work with the ARM Versatile board emulated by QEMU for the Pedigree project, but at the moment have none of that code here. I'll reply properly when I get home.
Re: ARM String Issues
Posted: Wed Jun 25, 2008 1:29 am
by JamesM
I'll let Matt reply later but our ARM bootloader appears to be linked at 0x10000, not 0x0.
And, aren't the interrupt vectors located at 0x0?
Re: ARM String Issues
Posted: Wed Jun 25, 2008 1:31 am
by pcmattman
Interrupt vectors are at 0x0, you're correct JamesM.
As for your binary, my link.ld is just your typical linker script with the start at 0x10000, as mentioned by JamesM. That *should* fix up your problem, if it doesn't I'll be more than happy to investigate further

Re: ARM String Issues
Posted: Wed Jun 25, 2008 8:35 am
by Alboin
That worked! Thanks!
I'm not sure why though. Does QEMU's ARM board load to 0x1000 and not 0x0? Or is it that the -kernel command line option of QEMU loads it there?
By the by, what ABI is the norm on ARM? (Or what does GCC use by default?)
Thanks for the help,
Alboin
Re: ARM String Issues
Posted: Wed Jun 25, 2008 9:01 am
by JamesM
Or is it that the -kernel command line option of QEMU loads it there?
QEMU emulates a ROM monitor by parsing your elf file and loading it at the load address contained within it. It probably works now because now you're not overwriting the interrupt vector table

Re: ARM String Issues
Posted: Wed Jun 25, 2008 9:12 am
by Alboin
It loads ELF files? <Recompiles kernel w\o strip> You're right, that appears to work as well. (I had been loading a binary file.)
Re: ARM String Issues
Posted: Wed Jun 25, 2008 3:30 pm
by pcmattman
The real hardware won't load an ELF file (technically, it won't
load anything at all, just start running

), but QEMU parses and loads them for you.
Re: ARM String Issues
Posted: Wed Jun 25, 2008 3:45 pm
by Alboin
Thanks again guys.
Have you run into this error? (GCC - 4.2.4, GAS - 2.18)
Code: Select all
Error: internal_relocation (type: OFFSET_IMM) not fixed up
I've searched, but have come up with nothing besides compiling with '-01', which doesn't appear to work.
This is the 'offending' code:
Code: Select all
mov r0, #0x2
ldr r1, =0x10140010
str r1, r0
Re: ARM String Issues
Posted: Thu Jun 26, 2008 12:53 am
by pcmattman
I've never experienced that, you might want to try a similar thing in C and disassemble the output to find out if there's something specific you're missing?
Re: ARM String Issues
Posted: Thu Jun 26, 2008 1:32 am
by JamesM
I'm not experienced with ARM, but I am with two other RISC architectures.
The ARM, like others, has a fixed 32-bit instruction size. Therefore, it is absolutely impossible to represent a full 32-bit address in one instruction!
This I assume loads a doubleword from an immediate to a register (r1) - the immediate will undoubtably be encoded as a PC-relative value and will be less than 32-bits long. In other architectures the immediate in load instructions is a 16-bit signed offset (which 0x10140010 most certainly will not fit into!)
You'd be better off reading the manual, but other architectures have special load-immediate instructions to get around this; MIPS has "lui" (load upper immediate) and PowerPC has "lis" (load immediate shifted). Both of these do the same thing (and I assume there will be an equivalent in ARM) - they load a 16-bit unsigned immediate value into the
upper 16 bits of a 32 bit register, setting the lower 16 bits as zero.
That is usually followed by either an "add immedate" or an "or immediate" depending on how you're feeling at the time (also the difference here is that usually add-immediates use signed values, so the lower 16 bits of the address you want to load will have to be altered to account for that).
Then you'd load from register. So in MIPS this would be;
Code: Select all
ori $r2, $r0, 0x2 # r2 = ZERO | 0x2
lui $r1, 0x1014 # r1 = 0x10140000
ori $r1, $r1, 0x10 # r1 = 0x10140010
sw $r2, 0($r1) # Store the value of r2 at the address in r1.
In PPC:
Code: Select all
ori 2, 0, 0x2 # r2 = ZERO | 0x2
lis 1, 0x1014 # r1 = 0x10140000
ori 1, 1, 0x10 # r1 = 0x10140010
stw 2, 0(1) # Store the value of r2 at the address in r1.
ARM... well, that's for you to work out
Hope this helps a little,
James
Re: ARM String Issues
Posted: Thu Jun 26, 2008 1:35 am
by pcmattman
JamesM: ARM
opcodes alone are 32 bits, not counting operand. At least, if I've interpreted the manual correctly they are =/.
EDIT: As per below, it turns out I didn't interpret the manual correctly

.
Re: ARM String Issues
Posted: Thu Jun 26, 2008 2:32 am
by JamesM
Alas no. See the second page of the PDF.
Re: ARM String Issues
Posted: Thu Jun 26, 2008 7:15 am
by Alboin
JamesM: In ARM, the immediate operand is, IIRC, 12 bits. The '=' syntax denotes that the assembler will determine how to load the value. (Either a mov, or a pc relative load.)
It turns out that this returns the same error:
While this does not:
I feel really stupid now. I have to say though that this is, quite possibly, *the* worst error message I have ever seen. I'm sorry.
Thanks.
Re: ARM String Issues
Posted: Thu Jun 26, 2008 11:32 am
by tsp
If I remember correctly the immediate constant in ARM is 8-bit value, regarding your code:
It turns out that this returns the same error:
That is the only error, because STR instruction is used to store word from register
to memory address so you cannot use STR to move value from register to another.
That has the correct addressing mode i.e the usage of [] + register.
That compiles correctly in my environment (WinARM).