Self and cross development environment for Raspberry Pi2

I bought Rapberry Pi2 and set up self and cross development environment.

1. Self dev env on Raspberry pi
Installed gcc/g++ 4. Used pre-installed gdb ver 7.4 for ARM.

src: main.s

/* main.s */
/* data section */
.data
.balign 4
myvar1:
  .word 3

.balign 4
myvar2:
  .word 4

.balign 4
myvar3:
  .word 0

/* text section */
.text
.balign 4
.global main
.func   main

main:
  ldr r1, addr_of_myvar1
  ldr r1, [r1]
  ldr r2, addr_of_myvar2
  ldr r2, [r2]
  add r2, r1, r2
  ldr r3, addr_of_myvar3
  /* store value of r2 -> address of r3 */
  str r2, [r3]
  /* load [r3] into r0 */
  ldr r0, addr_of_myvar3
  ldr r0, [r0]
  bx lr

gdb test. confirmed r1/r2 are loaded from .data section and added. Return value 7 was successfully returned.

(gdb) start
Temporary breakpoint 1 at 0x8418
Starting program: /home/pi/workspace/asm/tutorial/003/hoge

Temporary breakpoint 1, 0x00008418 in main ()
(gdb) disass
Dump of assembler code for function main:
=> 0x00008418 <+0>:	ldr	r1, [pc, #32]	; 0x8440 <addr_of_myvar1>
   0x0000841c <+4>:	ldr	r1, [r1]
   0x00008420 <+8>:	ldr	r2, [pc, #28]	; 0x8444 <addr_of_myvar2>
   0x00008424 <+12>:	ldr	r2, [r2]
   0x00008428 <+16>:	add	r2, r1, r2
   0x0000842c <+20>:	ldr	r3, [pc, #20]	; 0x8448 <addr_of_myvar3>
   0x00008430 <+24>:	str	r2, [r3]
   0x00008434 <+28>:	ldr	r0, [pc, #12]	; 0x8448 <addr_of_myvar3>
   0x00008438 <+32>:	ldr	r0, [r0]
   0x0000843c <+36>:	bx	lr
End of assembler dump.
(gdb) si
0x0000841c in main ()
(gdb) si
0x00008420 in main ()
(gdb) info r
r0             0x1	1
r1             0x3	3
r2             0x7efff79c	2130704284
r3             0x8418	33816
r4             0x0	0
r5             0x0	0
r6             0x82e4	33508
r7             0x0	0
r8             0x0	0
r9             0x0	0
r10            0x76fff000	1996484608
r11            0x0	0
r12            0x76fb9000	1996197888
sp             0x7efff648	0x7efff648
lr             0x76ea481c	1995065372
pc             0x8420	0x8420 <main+8>
cpsr           0x60000010	1610612752
(gdb) si
0x00008424 in main ()
(gdb) si
0x00008428 in main ()
(gdb) si
0x0000842c in main ()
(gdb) disass
Dump of assembler code for function main:
   0x00008418 <+0>:	ldr	r1, [pc, #32]	; 0x8440 <addr_of_myvar1>
   0x0000841c <+4>:	ldr	r1, [r1]
   0x00008420 <+8>:	ldr	r2, [pc, #28]	; 0x8444 <addr_of_myvar2>
   0x00008424 <+12>:	ldr	r2, [r2]
   0x00008428 <+16>:	add	r2, r1, r2
=> 0x0000842c <+20>:	ldr	r3, [pc, #20]	; 0x8448 <addr_of_myvar3>
   0x00008430 <+24>:	str	r2, [r3]
   0x00008434 <+28>:	ldr	r0, [pc, #12]	; 0x8448 <addr_of_myvar3>
   0x00008438 <+32>:	ldr	r0, [r0]
   0x0000843c <+36>:	bx	lr
End of assembler dump.
(gdb) info r
r0             0x1	1
r1             0x3	3
r2             0x7	7
r3             0x8418	33816
r4             0x0	0
r5             0x0	0
r6             0x82e4	33508
r7             0x0	0
r8             0x0	0
r9             0x0	0
r10            0x76fff000	1996484608
r11            0x0	0
r12            0x76fb9000	1996197888
sp             0x7efff648	0x7efff648
lr             0x76ea481c	1995065372
pc             0x842c	0x842c <main+20>
cpsr           0x60000010	1610612752

2. Cross dev env with ARM simulator on Ubuntu x64
Installed crosstool-NG and configured gcc/g++ 4.9 and gdb 7.8 for ARM with ARM sim.
Since Raspberry Pi hardware’s boot loader loads initial code at 0x8000 (I’ll make bare-metal micro code later), hoge.lds defines it to be loaded there.

@ startup
  .global _start
  .align

_start:
  ldr r0, =0x000000d3
  msr cpsr, r0
  ldr sp, =0x06400000
  bl main
  b .

hoge.lds

OUTPUT_ARCH(arm)
ENTRY(_start)
SECTIONS
{
	. = 0x8000;

	.text : { *(.text*) }
	. = ALIGN(4);

	__rodata_start = .;
	.rodata : { *(.data*) }
	. = ALIGN(4);
	__rodata_end = .;

	__data_start = . ;
	.data : { *(.data*) }
	. = ALIGN(4);
	__data_end = . ;

	__bss_start = . ;
	.bss : { *(.bss*) }
	. = ALIGN(4);
	__bss_end = . ;
}

hoge.c

int main(int argc, char const* argv[]) {
  int a = 3;
  int b = 4;
  return a + b;
}

gdb test with sim. Confirmed return value set to 7.

arm-unknown-eabi-gdb 
GNU gdb (crosstool-NG 1.20.0) 7.8
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-build_unknown-linux-gnu --target=arm-unknown-eabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
/home/sokoide/.gdbinit:1: Error in sourced command file:
No symbol table is loaded.  Use the "file" command.
(gdb) target sim
Connected to the simulator.
(gdb) load hoge
Loading section .text, size 0x54 vma 0x8000
Loading section .rodata, size 0x8 vma 0x8054
Start address 0x8000
Transfer rate: 736 bits in <1 sec.
(gdb) file hoge
Reading symbols from hoge...(no debugging symbols found)...done.
(gdb) start
Temporary breakpoint 1 at 0x8014
Starting program: /media/psf/Dropbox/workspace/arm/helloc/hoge 

Temporary breakpoint 1, 0x00008014 in main ()
(gdb) disass
Dump of assembler code for function main:
=> 0x00008014 <+0>:	push	{r11}		; (str r11, [sp, #-4]!)
   0x00008018 <+4>:	add	r11, sp, #0
   0x0000801c <+8>:	sub	sp, sp, #20
   0x00008020 <+12>:	str	r0, [r11, #-16]
   0x00008024 <+16>:	str	r1, [r11, #-20]
   0x00008028 <+20>:	mov	r3, #3
   0x0000802c <+24>:	str	r3, [r11, #-8]
   0x00008030 <+28>:	mov	r3, #4
   0x00008034 <+32>:	str	r3, [r11, #-12]
   0x00008038 <+36>:	ldr	r2, [r11, #-8]
   0x0000803c <+40>:	ldr	r3, [r11, #-12]
   0x00008040 <+44>:	add	r3, r2, r3
   0x00008044 <+48>:	mov	r0, r3
   0x00008048 <+52>:	sub	sp, r11, #0
   0x0000804c <+56>:	pop	{r11}		; (ldr r11, [sp], #4)
   0x00008050 <+60>:	bx	lr
End of assembler dump.
(gdb) info r
r0             0xd3	211
r1             0x0	0
r2             0x0	0
r3             0x0	0
r4             0x0	0
r5             0x0	0
r6             0x0	0
r7             0x0	0
r8             0x0	0
r9             0x0	0
r10            0x0	0
r11            0x0	0
r12            0x0	0
sp             0x6400000	0x6400000
lr             0x8010	32784
pc             0x8014	0x8014 <main>
cpsr           0xd3	211
(gdb) si
0x00008018 in main ()
(gdb) si
0x0000801c in main ()
(gdb) si
0x00008020 in main ()
(gdb) si
0x00008024 in main ()
(gdb) si
0x00008028 in main ()
(gdb) si
0x0000802c in main ()
(gdb) si
0x00008030 in main ()
(gdb) si
0x00008034 in main ()
(gdb) si
0x00008038 in main ()
(gdb) si
0x0000803c in main ()
(gdb) si
0x00008040 in main ()
(gdb) si
0x00008044 in main ()
(gdb) si
0x00008048 in main ()
(gdb) info r
r0             0x7	7
r1             0x0	0
r2             0x3	3
r3             0x7	7
r4             0x0	0
r5             0x0	0
r6             0x0	0
r7             0x0	0
r8             0x0	0
r9             0x0	0
r10            0x0	0
r11            0x63ffffc	104857596
r12            0x0	0
sp             0x63fffe8	0x63fffe8
lr             0x8010	32784
pc             0x8048	0x8048 <main+52>
cpsr           0xd3	211

Leave a Reply

Your email address will not be published. Required fields are marked *