The next thing we need to do is to tell the linker the memory location of our functions so it can stuff the locations into their branch instructions when it is building our program. We’ll do that by using a linker script, in which we’ll also tell the linker where in memory to locate our program. Here’s an example linker script to do both these things.:
ENTRY(main)
SECTIONS
{
rb_diag_printf = 0×45968;
rb_gets = 0x47f58;
. = 0×200000;
.text : { *(.text) }
.data : { *(.data) }
.bss : { *(.bss) }
}
This script first defines the location of the two functions we’re borrowing from RedBoot, then tells the linker that we’d like our program to reside at 0×200000. The function locations will be compiled (well, linked actually) into our object code, and the resulting ELF file will use 0×200000 as the relocation address. So when we load our ELF file into the PMP, it will automagically be placed at 0×200000. Now the entry point may not necessarily be at 0×200000 if you happen to write a bunch of functions that reside before main, but for this simple program it will be.
Now go write! Oh, you’re not going to make us post the damn source code for a hello world, are you? We’ll do it THIS time, but next time you’ve got to write it yourself as a learning experience.
#include
#include#include “rb_func.h”
#define printf rb_diag_printf
int main(void){
//printf test
char line[0x100];
uint32_t result;printf(“\n\n”);
printf(“Hello World, SPMP8k is alive! \n”);
printf(“Enter some text to see if I can read.\n”);
printf(“I’ll set the timeout to about 30 seconds for you slowpokes\n”);
printf(“Input> “);//int rb_gets(char *line, uint32_t len, uint32_t timeout);
result = rb_gets(line, 0×100, 0×800);printf(“\n”);
if (result == 1) {
printf(“\nResult was 0x%0X, your input was \n%s\n”, result, line);
}else{
printf(“You didn’t type anything! Don’t you love me?\n”);
printf(“Returning you to the RedBoot console\n”);
printf(“Use Ctrl-P to reload last line and try again.\n”);
}printf(“Done… Bye!\n”);
return 0;
}
Meh. Nothing too exciting there. We defined rb_diag_printf as printf to save ourselves some typing. Other than that, pretty typical stuff. To compile it, fire arm-elf-gdb:
arm-elf-gcc -Wall -g -c -mcpu=arm926ej-s -O0 -std=c99 helloworld.c
You may note that we’ve turned off the optimizer. It doesn’t matter here, but for later programs where you may be doing advanced register manipulation, having the optimizer meddle with the order of instructions was reaaaaally bad and rather annoying to have to debug. Frickin optimizer!
Assuming you put the linker stuff from above in a file, here’s how to link your object code to a final elf file. Here, the linker script is called ldscript.
arm-elf-ld -T ldscript helloworld.o -o helloworld.elf
You can further dump the binary section to a bin file if you plan to upload using RedBoot’s “load -r” method:
arm-elf-objcopy -O binary helloworld.elf helloworld.bin
And if you want to see the disassembly of your little program in order to check the entry point, or just marvel at opcodes:
arm-elf-objdump -d helloworld.elf
And finally, if you’re tired of the long painful wait to upload a file via RedBoot’s xmodem, you can use gdb. It is possible to disconnect minicom or your other terminal program without closing it, upload via gdb in a split second, and then resume your minicom session. That is our preferred method of getting files into the box.
To do this, just put the gdb commands into a text file and fire gdb with the command option. An example of a gdb command file (loadfile.gdb) to use for helloworld would be:
target remote
load helloworld.elf
quit
Where is /dev/USBttyS0, COM1, etc. To fire gdb with this command file, you would use the command-line arguments:
arm-elf-gdb –command=loadfile.gdb
You can use RedBoot and XMODEM to upload your elf or bin files, but once you get tired of that stupid 52 second timeout, give gdb a try. Of course, you will have to have built arm-elf-gdb, but we have a shell script for that build on the Linux side and it came with the pre-built binaries on the Windows side.
As always, here is the full source code and project files for the hello world test program.
Let us know if you have issues with any of this and we will correct any issues or mistakes that we might have made. We’ve got live nude NAND dumps open in another window so it’s rather hard to focus, as you can imagine. *wink*





Recent Comments