1
Learning Embedded Software
Development from Hello World
Inside C Program
liuken25@gmail.com
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
2
Outline
• Some Questions before Class
• Compilation of C Program
• Program Sections
• Data Storing
• Linking and Loading
• Simple C Program from Scratch
• Conclusions
22
Question ?
• Why the size of binary file is 0x8040, not
0x8048 ?
linux:$ ls -l section.bin
-rwxr-xr-x 1 liuken liuken 32832 2008-08-06 19:54 section.bin
反組譯 .bss 區段:
00008040 <c.0>:
8040: 00000000
00008044 <a>:
8044: 00000000
0x8040
23
BSS
• BSS is used to store uninitialized global or static data.
• We must guarantee that the value of all uninitialized data is equal to
zero.
• If we store these zeros in an image or executable file, it will waste
storage space.
• Therefore, we only make a mark in file to reserve space for BSS.
• After executing, C run time must make a room for BSS and initialize
its value to zero.
24
Linker Scripts
• The main purpose of the linker script is to describe how
the sections in the input files should be mapped into the
output file, and to control the memory layout of the
output file.
• You can use the “--verbose” command line option to
display the default linker script.
• Using “-T” to specify your own linker script.
– Doing this will replace the default linker script.
25
Example
00000000 <foo>:
...
18: e59f3014 ldr r3, [pc, #20] ; 34 <.text+0x34>
...
34: 0000803c andeq r8, r0, ip, lsr r0
反組譯 .data 區段:
00008038 <__data_start>:
8038: 00000003 andeq r0, r0, r3
0000803c <f.1>:
803c: 00000006 andeq r0, r0, r6
• What if we load below code to 0x1000 ?
linux:$ arm_v5t_le-gcc -c section.c -o section.o
linux:$ arm_v5t_le-ld -Ttext 0x0 section.o -e foo –o section
26
Example
00001000 <foo>:
...
1018: e59f3014 ldr r3, [pc, #20] ; 34 <.text+0x34>
...
1034: 0000803c andeq r8, r0, ip, lsr r0
反組譯 .data 區段:
00009038 <__data_start>:
9038: 00000003 andeq r0, r0, r3
0000903c <f.1>:
903c: 00000006 andeq r0, r0, r6
• What if we load below code to 0x1000 ?
• The variable f is at 0x903c, but we still load f from
0x803c.
linux:$ arm_v5t_le-gcc -c section.c -o section.o
linux:$ arm_v5t_le-ld -Ttext 0x0 section.o -e foo –o section
27
Output and Input Sections
SECTIONS
{
. = 0x10000;
.text : {
init.o(.intvec)
*(.text)
}
. = 0x8000000;
.data : {
*(.data)
}
}
SECTIONS
{
output_section [address] [(type)] : [AT(lma)]
{
input_section
}
}
28
Linker Script Expression
• “.” means location counter
– Current address
• Assign value to symbol
– etext = 0x500;
– etext = . + 0x5;
• Symbol can be access as a variable in C or assembly
code.
29
Example
• Instead of linker script, using
linker command line option to
specify address.
– -Ttext address
– -Tdata address
– -Tbss address
linux:$ arm_v5t_le-ld -Ttext 0x0 –Tdata 0x100 section.o -e foo –o section
or
linux:$ arm_v5t_le-ld –T section.ld section.o –o section
00000000 <foo>:
...
18: e59f3014 ldr r3, [pc, #20] ; 34 <.text+0x34>
...
34: 00000104
...
00000104 <f.1>:
0104: 00000006 andeq r0, r0, r6
ENTRY(foo)
SECTIONS
{
. = 0x00;
.text : {*(.text)}
/* . = 0x100 */
.data 0x100 : {*(.data)}
}
section.ld
30
Load Address
• Load address is where data will be loaded.
• Load address does not equal to execute address.
– Ex:
• A file including code and data sections is loaded on ROM, but the
data must be reloaded to RAM.
.text section
.data section
Binary Image .text section
.data section
ROMRAM
Burn
.text section
.data section
ROMRAM
.data section
Execute
31
Example
SECTIONS
{
.text 0x1000 :
{ *(.text) _etext = .; }
.mdata 0x2000 : AT(ADDR(.text) + SIZEOF(.text))
{_data = .; *(.data); _edata = .; }
.bss 0x3000 :
{_bstart = .; *(.bss) *(COMMON); _bend = .;}
}
extern char _etext, _data, _edata, _bstart, _bend;
char *src = _etext;
char *dst = _data;
while(dst < _edata) *dst++ = *src++;
for(dst=_bstart; dst<_bend; dst++) *dst=0;
32
PXA270 Lab Example
OUTPUT_ARCH(arm)
ENTRY(_reset_entry)
SECTIONS
{
_CODE_VMA = 0x00000000 ;
_CODE_LMA = 0x00000000 ;
_DATA_VMA = 0xA1000000 ;
.text _CODE_VMA : AT (_CODE_LMA) {
_text = .;
*(.vector)
*(.stext)
*(.mmulib)
*(.text)
*(.rodata)
. = ALIGN(4);
_etext = .;
}
.data _DATA_VMA : AT ( _CODE_LMA + SIZEOF(.text)){
_sdata = .;
...
_edata = .;
}
.bss : {
_sbss = .;
...
_ebss = .;
}
_end = .;
.stack 0xA4000000 : {
_USR_STACK = .;
_IRQ_STACK = . - 0x1000;
_FIQ_STACK = . - 0x2000;
__stack_limit = . - 0x1000;
}
}
buildgnu2953demo.ld
33
buildgnu2953starup.s.section ".vector"
_reset_entry:
b ResetHandler
b UdefHnd
b SwiHnd
b PabtHnd
b DabtHnd
b Unexpected
b IrqHnd
b FiqHnd
...
.section ".stext“
ResetHandler:
bl PreInit
bl low_level_init
relocate: /* relocate Imag to RAM*/
ands r9, pc, #0xFF000000
bne setup_mmu_table
mov r0,#0
ldr r1,=_reset_entry
mov r2, r1
ldr r3,=_edata
sub r2, r3, r2
add r2, r0, r2
copy_loop:
ldmia r0!, {r3-r10}
stmia r1!, {r3-r10}
cmp r0, r2
ble copy_loop
cstartup:
...
ldr r0, =_etext
ldr r1, =_sdata
ldr r3, =_sbss
...
1: ldr r1, =_ebss
mov r2, #0
2: cmp r3, r1
strcc r2, [r3], #4
bcc 2b
/* Set up the stack */
stack_setup:
mrs r0,cpsr
bic r0,r0,#MODE_MASK
orr r1,r0,#IRQ_MODE|NOINT
msr cpsr,r1 @IRQMode
ldr sp,=_IRQ_STACK
orr r1,r0,#FIQ_MODE|NOINT
msr cpsr,r1 @FIQMode
ldr sp,=_FIQ_STACK
mrs r0, cpsr
bic r0, r0, #MODE_MASK
orr r0, r0, #SVC_MODE
bic r0,r0,#0xc0 @enable IRQ and FIQ
msr cpsr, r0
ldr sp, =_USR_STACK
3:
bl main
b 3b
34
PXA270 Lab Example
.vector
.stext
.mmulib
.text
.rodata
.text
.data
.dtors
.ctors
.data
.data
.dtors
.ctors
.data
.bss
COMMON
.bss
FLASH : 0x00000000 SDRAM : 0xA0000000
_text
_etext
_sdata
_edata
_sdata
_edata
_sbss
_ebss
_CODE_VMA
_CODE_LMA
LOAD_TIME_ADDR
_CODE_LMA +
SIZEOF(.text)
RUN_TIME_ADDR
DATA_VMA
CPOY
Fill with 0
0xA1000000
0xA4000000
.stack
35
PXA270 Lab Example – No Relocation
buildgnu2953lab.ldOUTPUT_ARCH(arm)
ENTRY(_reset_entry)
SECTIONS
{
.text 0xa0000000 : {
_text = .;
*(.vector)
*(.stext)
*(.mmulib)
*(.text)
*(.rodata)
. = ALIGN(4);
_etext = .;
}
.data : {
_sdata = .;
*(.data)
*(.dtors)
*(.ctors)
. = ALIGN(4);
_edata = .;
}
.bss : {
_sbss = .;
*(.bss)
*(COMMON)
. = ALIGN(4);
_ebss = .;
}
_end = .;
.stack 0xa4000000 : {
_USR_STACK = .;
_IRQ_STACK = . - 0x1000;
_FIQ_STACK = . - 0x2000;
__stack_limit = . - 0x1000;
}
}
36
Position Independent Code (PIC)
• How can multiple processes share a single copy
of a program?
– A priori dedicated address.
– Compile code so that it can be loaded and executed
at any address without being modified by the linker.
• Position Independent Code (PIC)
• PIC Data Reference
– The data segment is allocated after the code segment.
– The distance between any instruction in the code
segment and any variable in the data segment is a
run-time constant.
37
Generate PIC Code
linux:$ arm_v5t_le-gcc -c section.c –f pic -o section.o
linux:$ arm_v5t_le-ld -Ttext 0x0 section.o -e foo –o section
linux:$ arm_v5t_le-objcopy –O binary section section.bin
int a;
int b = 3;
void foo(void)
{
static int c;
int d;
int e = 5;
static int f = 6;
a = e + f;
}
38
00000000 <foo>:
0: e1a0c00d mov ip, sp
4: e92ddc00 stmdb sp!, {sl, fp, ip, lr, pc}
8: e24cb004 sub fp, ip, #4 ; 0x4
c: e24dd00c sub sp, sp, #12 ; 0xc
10: e59fa030 ldr sl, [pc, #48] ; 48 <.text+0x48>
14: e08fa00a add sl, pc, sl
18: e3a03005 mov r3, #5 ; 0x5
1c: e50b301c str r3, [fp, #-28]
20: e59f3024 ldr r3, [pc, #36] ; 4c <.text+0x4c>
24: e79a1003 ldr r1, [sl, r3]
28: e59f3020 ldr r3, [pc, #32] ; 50 <.text+0x50>
2c: e08a3003 add r3, sl, r3
30: e51b201c ldr r2, [fp, #-28]
34: e5933000 ldr r3, [r3]
38: e0823003 add r3, r2, r3
3c: e5813000 str r3, [r1]
40: e24bd010 sub sp, fp, #16 ; 0x10
44: e89dac00 ldmia sp, {sl, fp, sp, pc}
48: 00008038 andeq r8, r0, r8, lsr r0
4c: 0000000c andeq r0, r0, ip
50: 00000014 andeq r0, r0, r4, lsl r0
反組譯 .got 區段:00008054 <_GLOBAL_OFFSET_TABLE_>:
...
8060: 00008070 andeq r8, r0, r0, ror r0
反組譯 .data 區段:00008064 <__data_start>:
8064: 00000003 andeq r0, r0, r3
00008068 <f.1>:
8068: 00000006 andeq r0, r0, r6
反組譯 .bss 區段:
0000806c <c.0>:
806c: 00000000 andeq r0, r0, r
000008070 <a>:
8070: 00000000 andeq r0, r0, r0
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp ip
R3:
R2:
stackhigh
low
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl: R1:
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
R3:
R2:
stackhigh
low
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: R1:
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
stackhigh
low
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: R1:
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
stackhigh
low
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: R1:
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
stackhigh
low
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008038 R1:
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
stackhigh
low
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008038 +1C R1:
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
stackhigh
low
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054 R1:
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
5
stackhigh
low
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054 R1:
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2:
5
stackhigh
low
e
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054 R1:
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2:
stackhigh
low
e
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054
C
R1:
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2:
stackhigh
low
e
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054
C
R1: 00008070
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2:
stackhigh
low
e
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054 R1: 00008070
14
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2:
stackhigh
low
e
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054 R1: 00008070
00008068
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2: 5
stackhigh
low
e
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054 R1: 00008070
00008068
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2: 5
6
stackhigh
low
e
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054 R1: 00008070
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2: 5
stackhigh
low
e
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054 R1: 00008070
11
39
00:mov ip, sp
04:stmdb sp!, {sl, fp, ip, lr, pc}
08:sub fp, ip, #4 ; 0x4
0C:sub sp, sp, #12 ; 0xc
10:ldr sl, [pc, #48] ; 48 <.text+0x48>
14:add sl, pc, sl
18:mov r3, #5 ; 0x5
1C:str r3, [fp, #-28]
20:ldr r3, [pc, #36] ; 4c <.text+0x4c>
24:ldr r1, [sl, r3]
28:ldr r3, [pc, #32] ; 50 <.text+0x50>
2C:add r3, sl, r3
30:ldr r2, [fp, #-28]
34:ldr r3, [r3]
38:add r3, r2, r3
3C:str r3, [r1]
40:sub sp, fp, #16 ; 0x10
44:ldmia sp, {sl, fp, sp, pc}
48:00008038
4C:0000000c
50:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2: 5
stackhigh
low
e
3
6
00008064
00008068
0000806C
00008070
0000807000008060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008054 R1: 00008070
11
11
40
Constant Offset
3
6
00008064
00008068
0000806C
00008070
0000807000008060
got
data
bss
b
f
c
a
00008054
0x14
0xC
text00000000
0000001C
0x8038
41
Global Offset Table (GOT)
• The compiler creates a table called global offset
table at the beginning of the data segment.
• The GOT contains an entry for each global data
object that is referenced by the object module.
• At load time, the dynamic linker relocates each
entry in the GOT so that it contains the
appropriate absolute address.
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp ip
R3:
R2:
stackhigh
low
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl: R1:
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
R3:
R2:
stackhigh
low
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: R1:
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
stackhigh
low
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: R1:
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
stackhigh
low
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: R1:
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
stackhigh
low
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008038 R1:
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
stackhigh
low
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00008038 +101C R1:
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
stackhigh
low
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054 R1:
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
R3:
R2:
5
stackhigh
low
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054 R1:
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2:
5
stackhigh
low
e
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054 R1:
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2:
stackhigh
low
e
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054
C
R1:
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2:
stackhigh
low
e
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054
C
R1: 00008070
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2:
stackhigh
low
e
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054 R1: 00008070
14
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2:
stackhigh
low
e
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054 R1: 00008070
00009068
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2: 5
stackhigh
low
e
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054 R1: 00008070
00009068
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2: 5
6
stackhigh
low
e
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054 R1: 00008070
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3:
R2: 5
stackhigh
low
e
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054 R1: 00008070
11
42
1000:mov ip, sp
1004:stmdb sp!, {sl, fp, ip, lr, pc}
1008:sub fp, ip, #4 ; 0x4
100C:sub sp, sp, #12 ; 0xc
1010:ldr sl, [pc, #48] ; 48 <.text+0x48>
1014:add sl, pc, sl
1018:mov r3, #5 ; 0x5
101C:str r3, [fp, #-28]
1020:ldr r3, [pc, #36] ; 4c <.text+0x4c>
1024:ldr r1, [sl, r3]
1028:ldr r3, [pc, #32] ; 50 <.text+0x50>
102C:add r3, sl, r3
1030:ldr r2, [fp, #-28]
1034:ldr r3, [r3]
1038:add r3, r2, r3
103C:str r3, [r1]
1040:sub sp, fp, #16 ; 0x10
1044:ldmia sp, {sl, fp, sp, pc}
1048:00008038
104C:0000000c
1050:00000014
sp
ip
pc
lr
ip
fp
fp
5
R3: 00009070
R2: 5
stackhigh
low
e
3
6
00009064
00009068
0000906C
00009070
0000807000009060 got
data
bss
b
f
c
aNote: ip->r12, fp->r11
sl
sl: 00009054 R1: 00008070
11
11
00009070
43
Hello World from Scratch
44
Integrate All What You Learned
• You should have knowledge about how a program running.
• Write a program sending “Hello World” through UART and
displaying on terminal without any helper library.
45
Simple Hello World on PXA270
• Assumption
– Hardware devices have been initialized.
• SDRAM has been initialized.
• UART has been initialized.
• Bootloader usually initializes hardware devices.
– UBoot will do.
• In skyeye, all devices are emulated and can be used immediately.
– Notice: Your code might be capable of running successfully but can’t
run correctly on target board.
46
Simple Hello World on PXA270
.global _start
.text
_start:
ldr sp, =0xa0001000
ldr r1, =0x40100000
ldr r2, h str r2, [r1]
bl c_start
1:
b 1b
h:
.byte 'h'
void c_start(void)
{
*((unsigned long volatile*)(0x40100000)) = 'H';
*((unsigned long volatile*)(0x40100000)) = 'e';
*((unsigned long volatile*)(0x40100000)) = 'l';
*((unsigned long volatile*)(0x40100000)) = 'l';
*((unsigned long volatile*)(0x40100000)) = 'o';
*((unsigned long volatile*)(0x40100000)) = ' ';
*((unsigned long volatile*)(0x40100000)) = '!';
*((unsigned long volatile*)(0x40100000)) = 'n';
*((unsigned long volatile*)(0x40100000)) = 'r';
}
start.s
c_start.c
47
Simple Hello World on PXA270
linux:$ arm_v5t_le-as start.c -o start.o
linux:$ arm_v5t_le-gcc -c c_start.c -o c_start.o
linux:$ arm_v5t_le-ld –Ttext 0x0 -e _start start.o
c_start.o –o start
linux:$ arm_v5t_le-ld –T start.ld start.o
c_start.o –o start
ENTRY(_start)
SECTIONS
{
.text 0x0 : {*(.text)}
.data : {*(.data)}
.bss : {*(.bss)}
}
start.ld
or
linux:$ skyeye -e start
48
Conclusions
• The C runtime environment must be set before
entering C code.
• A simple CRT includes:
– Set stack pointer
– Clear BSS
– If there is a GOT, relocating the GOT.
– Jump to main
• Note: A complete CRT contains complex
functions.
– C library initialization
– Exit code handling
– …

Learning Embedded Software Development from Hello World

  • 1.
    1 Learning Embedded Software Developmentfrom Hello World Inside C Program liuken25@gmail.com
  • 2.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 3.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 4.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 5.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 6.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 7.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 8.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 9.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 10.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 11.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 12.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 13.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 14.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 15.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 16.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 17.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 18.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 19.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 20.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 21.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 22.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 23.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 24.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 25.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 26.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 27.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 28.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 29.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 30.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 31.
    2 Outline • Some Questionsbefore Class • Compilation of C Program • Program Sections • Data Storing • Linking and Loading • Simple C Program from Scratch • Conclusions
  • 32.
    22 Question ? • Whythe size of binary file is 0x8040, not 0x8048 ? linux:$ ls -l section.bin -rwxr-xr-x 1 liuken liuken 32832 2008-08-06 19:54 section.bin 反組譯 .bss 區段: 00008040 <c.0>: 8040: 00000000 00008044 <a>: 8044: 00000000 0x8040
  • 33.
    23 BSS • BSS isused to store uninitialized global or static data. • We must guarantee that the value of all uninitialized data is equal to zero. • If we store these zeros in an image or executable file, it will waste storage space. • Therefore, we only make a mark in file to reserve space for BSS. • After executing, C run time must make a room for BSS and initialize its value to zero.
  • 34.
    24 Linker Scripts • Themain purpose of the linker script is to describe how the sections in the input files should be mapped into the output file, and to control the memory layout of the output file. • You can use the “--verbose” command line option to display the default linker script. • Using “-T” to specify your own linker script. – Doing this will replace the default linker script.
  • 35.
    25 Example 00000000 <foo>: ... 18: e59f3014ldr r3, [pc, #20] ; 34 <.text+0x34> ... 34: 0000803c andeq r8, r0, ip, lsr r0 反組譯 .data 區段: 00008038 <__data_start>: 8038: 00000003 andeq r0, r0, r3 0000803c <f.1>: 803c: 00000006 andeq r0, r0, r6 • What if we load below code to 0x1000 ? linux:$ arm_v5t_le-gcc -c section.c -o section.o linux:$ arm_v5t_le-ld -Ttext 0x0 section.o -e foo –o section
  • 36.
    26 Example 00001000 <foo>: ... 1018: e59f3014ldr r3, [pc, #20] ; 34 <.text+0x34> ... 1034: 0000803c andeq r8, r0, ip, lsr r0 反組譯 .data 區段: 00009038 <__data_start>: 9038: 00000003 andeq r0, r0, r3 0000903c <f.1>: 903c: 00000006 andeq r0, r0, r6 • What if we load below code to 0x1000 ? • The variable f is at 0x903c, but we still load f from 0x803c. linux:$ arm_v5t_le-gcc -c section.c -o section.o linux:$ arm_v5t_le-ld -Ttext 0x0 section.o -e foo –o section
  • 37.
    27 Output and InputSections SECTIONS { . = 0x10000; .text : { init.o(.intvec) *(.text) } . = 0x8000000; .data : { *(.data) } } SECTIONS { output_section [address] [(type)] : [AT(lma)] { input_section } }
  • 38.
    28 Linker Script Expression •“.” means location counter – Current address • Assign value to symbol – etext = 0x500; – etext = . + 0x5; • Symbol can be access as a variable in C or assembly code.
  • 39.
    29 Example • Instead oflinker script, using linker command line option to specify address. – -Ttext address – -Tdata address – -Tbss address linux:$ arm_v5t_le-ld -Ttext 0x0 –Tdata 0x100 section.o -e foo –o section or linux:$ arm_v5t_le-ld –T section.ld section.o –o section 00000000 <foo>: ... 18: e59f3014 ldr r3, [pc, #20] ; 34 <.text+0x34> ... 34: 00000104 ... 00000104 <f.1>: 0104: 00000006 andeq r0, r0, r6 ENTRY(foo) SECTIONS { . = 0x00; .text : {*(.text)} /* . = 0x100 */ .data 0x100 : {*(.data)} } section.ld
  • 40.
    30 Load Address • Loadaddress is where data will be loaded. • Load address does not equal to execute address. – Ex: • A file including code and data sections is loaded on ROM, but the data must be reloaded to RAM. .text section .data section Binary Image .text section .data section ROMRAM Burn .text section .data section ROMRAM .data section Execute
  • 41.
    31 Example SECTIONS { .text 0x1000 : {*(.text) _etext = .; } .mdata 0x2000 : AT(ADDR(.text) + SIZEOF(.text)) {_data = .; *(.data); _edata = .; } .bss 0x3000 : {_bstart = .; *(.bss) *(COMMON); _bend = .;} } extern char _etext, _data, _edata, _bstart, _bend; char *src = _etext; char *dst = _data; while(dst < _edata) *dst++ = *src++; for(dst=_bstart; dst<_bend; dst++) *dst=0;
  • 42.
    32 PXA270 Lab Example OUTPUT_ARCH(arm) ENTRY(_reset_entry) SECTIONS { _CODE_VMA= 0x00000000 ; _CODE_LMA = 0x00000000 ; _DATA_VMA = 0xA1000000 ; .text _CODE_VMA : AT (_CODE_LMA) { _text = .; *(.vector) *(.stext) *(.mmulib) *(.text) *(.rodata) . = ALIGN(4); _etext = .; } .data _DATA_VMA : AT ( _CODE_LMA + SIZEOF(.text)){ _sdata = .; ... _edata = .; } .bss : { _sbss = .; ... _ebss = .; } _end = .; .stack 0xA4000000 : { _USR_STACK = .; _IRQ_STACK = . - 0x1000; _FIQ_STACK = . - 0x2000; __stack_limit = . - 0x1000; } } buildgnu2953demo.ld
  • 43.
    33 buildgnu2953starup.s.section ".vector" _reset_entry: b ResetHandler bUdefHnd b SwiHnd b PabtHnd b DabtHnd b Unexpected b IrqHnd b FiqHnd ... .section ".stext“ ResetHandler: bl PreInit bl low_level_init relocate: /* relocate Imag to RAM*/ ands r9, pc, #0xFF000000 bne setup_mmu_table mov r0,#0 ldr r1,=_reset_entry mov r2, r1 ldr r3,=_edata sub r2, r3, r2 add r2, r0, r2 copy_loop: ldmia r0!, {r3-r10} stmia r1!, {r3-r10} cmp r0, r2 ble copy_loop cstartup: ... ldr r0, =_etext ldr r1, =_sdata ldr r3, =_sbss ... 1: ldr r1, =_ebss mov r2, #0 2: cmp r3, r1 strcc r2, [r3], #4 bcc 2b /* Set up the stack */ stack_setup: mrs r0,cpsr bic r0,r0,#MODE_MASK orr r1,r0,#IRQ_MODE|NOINT msr cpsr,r1 @IRQMode ldr sp,=_IRQ_STACK orr r1,r0,#FIQ_MODE|NOINT msr cpsr,r1 @FIQMode ldr sp,=_FIQ_STACK mrs r0, cpsr bic r0, r0, #MODE_MASK orr r0, r0, #SVC_MODE bic r0,r0,#0xc0 @enable IRQ and FIQ msr cpsr, r0 ldr sp, =_USR_STACK 3: bl main b 3b
  • 44.
    34 PXA270 Lab Example .vector .stext .mmulib .text .rodata .text .data .dtors .ctors .data .data .dtors .ctors .data .bss COMMON .bss FLASH: 0x00000000 SDRAM : 0xA0000000 _text _etext _sdata _edata _sdata _edata _sbss _ebss _CODE_VMA _CODE_LMA LOAD_TIME_ADDR _CODE_LMA + SIZEOF(.text) RUN_TIME_ADDR DATA_VMA CPOY Fill with 0 0xA1000000 0xA4000000 .stack
  • 45.
    35 PXA270 Lab Example– No Relocation buildgnu2953lab.ldOUTPUT_ARCH(arm) ENTRY(_reset_entry) SECTIONS { .text 0xa0000000 : { _text = .; *(.vector) *(.stext) *(.mmulib) *(.text) *(.rodata) . = ALIGN(4); _etext = .; } .data : { _sdata = .; *(.data) *(.dtors) *(.ctors) . = ALIGN(4); _edata = .; } .bss : { _sbss = .; *(.bss) *(COMMON) . = ALIGN(4); _ebss = .; } _end = .; .stack 0xa4000000 : { _USR_STACK = .; _IRQ_STACK = . - 0x1000; _FIQ_STACK = . - 0x2000; __stack_limit = . - 0x1000; } }
  • 46.
    36 Position Independent Code(PIC) • How can multiple processes share a single copy of a program? – A priori dedicated address. – Compile code so that it can be loaded and executed at any address without being modified by the linker. • Position Independent Code (PIC) • PIC Data Reference – The data segment is allocated after the code segment. – The distance between any instruction in the code segment and any variable in the data segment is a run-time constant.
  • 47.
    37 Generate PIC Code linux:$arm_v5t_le-gcc -c section.c –f pic -o section.o linux:$ arm_v5t_le-ld -Ttext 0x0 section.o -e foo –o section linux:$ arm_v5t_le-objcopy –O binary section section.bin int a; int b = 3; void foo(void) { static int c; int d; int e = 5; static int f = 6; a = e + f; }
  • 48.
    38 00000000 <foo>: 0: e1a0c00dmov ip, sp 4: e92ddc00 stmdb sp!, {sl, fp, ip, lr, pc} 8: e24cb004 sub fp, ip, #4 ; 0x4 c: e24dd00c sub sp, sp, #12 ; 0xc 10: e59fa030 ldr sl, [pc, #48] ; 48 <.text+0x48> 14: e08fa00a add sl, pc, sl 18: e3a03005 mov r3, #5 ; 0x5 1c: e50b301c str r3, [fp, #-28] 20: e59f3024 ldr r3, [pc, #36] ; 4c <.text+0x4c> 24: e79a1003 ldr r1, [sl, r3] 28: e59f3020 ldr r3, [pc, #32] ; 50 <.text+0x50> 2c: e08a3003 add r3, sl, r3 30: e51b201c ldr r2, [fp, #-28] 34: e5933000 ldr r3, [r3] 38: e0823003 add r3, r2, r3 3c: e5813000 str r3, [r1] 40: e24bd010 sub sp, fp, #16 ; 0x10 44: e89dac00 ldmia sp, {sl, fp, sp, pc} 48: 00008038 andeq r8, r0, r8, lsr r0 4c: 0000000c andeq r0, r0, ip 50: 00000014 andeq r0, r0, r4, lsl r0 反組譯 .got 區段:00008054 <_GLOBAL_OFFSET_TABLE_>: ... 8060: 00008070 andeq r8, r0, r0, ror r0 反組譯 .data 區段:00008064 <__data_start>: 8064: 00000003 andeq r0, r0, r3 00008068 <f.1>: 8068: 00000006 andeq r0, r0, r6 反組譯 .bss 區段: 0000806c <c.0>: 806c: 00000000 andeq r0, r0, r 000008070 <a>: 8070: 00000000 andeq r0, r0, r0
  • 49.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip R3: R2: stackhigh low 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl: R1:
  • 50.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp R3: R2: stackhigh low 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: R1:
  • 51.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp R3: R2: stackhigh low 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: R1:
  • 52.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp R3: R2: stackhigh low 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: R1:
  • 53.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp R3: R2: stackhigh low 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008038 R1:
  • 54.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp R3: R2: stackhigh low 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008038 +1C R1:
  • 55.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp R3: R2: stackhigh low 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 R1:
  • 56.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp R3: R2: 5 stackhigh low 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 R1:
  • 57.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp 5 R3: R2: 5 stackhigh low e 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 R1:
  • 58.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp 5 R3: R2: stackhigh low e 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 C R1:
  • 59.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp 5 R3: R2: stackhigh low e 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 C R1: 00008070
  • 60.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp 5 R3: R2: stackhigh low e 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 R1: 00008070 14
  • 61.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp 5 R3: R2: stackhigh low e 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 R1: 00008070 00008068
  • 62.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp 5 R3: R2: 5 stackhigh low e 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 R1: 00008070 00008068
  • 63.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp 5 R3: R2: 5 6 stackhigh low e 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 R1: 00008070
  • 64.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp 5 R3: R2: 5 stackhigh low e 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 R1: 00008070 11
  • 65.
    39 00:mov ip, sp 04:stmdbsp!, {sl, fp, ip, lr, pc} 08:sub fp, ip, #4 ; 0x4 0C:sub sp, sp, #12 ; 0xc 10:ldr sl, [pc, #48] ; 48 <.text+0x48> 14:add sl, pc, sl 18:mov r3, #5 ; 0x5 1C:str r3, [fp, #-28] 20:ldr r3, [pc, #36] ; 4c <.text+0x4c> 24:ldr r1, [sl, r3] 28:ldr r3, [pc, #32] ; 50 <.text+0x50> 2C:add r3, sl, r3 30:ldr r2, [fp, #-28] 34:ldr r3, [r3] 38:add r3, r2, r3 3C:str r3, [r1] 40:sub sp, fp, #16 ; 0x10 44:ldmia sp, {sl, fp, sp, pc} 48:00008038 4C:0000000c 50:00000014 sp ip pc lr ip fp fp 5 R3: R2: 5 stackhigh low e 3 6 00008064 00008068 0000806C 00008070 0000807000008060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008054 R1: 00008070 11 11
  • 66.
  • 67.
    41 Global Offset Table(GOT) • The compiler creates a table called global offset table at the beginning of the data segment. • The GOT contains an entry for each global data object that is referenced by the object module. • At load time, the dynamic linker relocates each entry in the GOT so that it contains the appropriate absolute address.
  • 68.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip R3: R2: stackhigh low 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl: R1:
  • 69.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp R3: R2: stackhigh low 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: R1:
  • 70.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp R3: R2: stackhigh low 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: R1:
  • 71.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp R3: R2: stackhigh low 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: R1:
  • 72.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp R3: R2: stackhigh low 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008038 R1:
  • 73.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp R3: R2: stackhigh low 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00008038 +101C R1:
  • 74.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp R3: R2: stackhigh low 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 R1:
  • 75.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp R3: R2: 5 stackhigh low 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 R1:
  • 76.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp 5 R3: R2: 5 stackhigh low e 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 R1:
  • 77.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp 5 R3: R2: stackhigh low e 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 C R1:
  • 78.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp 5 R3: R2: stackhigh low e 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 C R1: 00008070
  • 79.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp 5 R3: R2: stackhigh low e 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 R1: 00008070 14
  • 80.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp 5 R3: R2: stackhigh low e 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 R1: 00008070 00009068
  • 81.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp 5 R3: R2: 5 stackhigh low e 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 R1: 00008070 00009068
  • 82.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp 5 R3: R2: 5 6 stackhigh low e 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 R1: 00008070
  • 83.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp 5 R3: R2: 5 stackhigh low e 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 R1: 00008070 11
  • 84.
    42 1000:mov ip, sp 1004:stmdbsp!, {sl, fp, ip, lr, pc} 1008:sub fp, ip, #4 ; 0x4 100C:sub sp, sp, #12 ; 0xc 1010:ldr sl, [pc, #48] ; 48 <.text+0x48> 1014:add sl, pc, sl 1018:mov r3, #5 ; 0x5 101C:str r3, [fp, #-28] 1020:ldr r3, [pc, #36] ; 4c <.text+0x4c> 1024:ldr r1, [sl, r3] 1028:ldr r3, [pc, #32] ; 50 <.text+0x50> 102C:add r3, sl, r3 1030:ldr r2, [fp, #-28] 1034:ldr r3, [r3] 1038:add r3, r2, r3 103C:str r3, [r1] 1040:sub sp, fp, #16 ; 0x10 1044:ldmia sp, {sl, fp, sp, pc} 1048:00008038 104C:0000000c 1050:00000014 sp ip pc lr ip fp fp 5 R3: 00009070 R2: 5 stackhigh low e 3 6 00009064 00009068 0000906C 00009070 0000807000009060 got data bss b f c aNote: ip->r12, fp->r11 sl sl: 00009054 R1: 00008070 11 11 00009070
  • 85.
  • 86.
    44 Integrate All WhatYou Learned • You should have knowledge about how a program running. • Write a program sending “Hello World” through UART and displaying on terminal without any helper library.
  • 87.
    45 Simple Hello Worldon PXA270 • Assumption – Hardware devices have been initialized. • SDRAM has been initialized. • UART has been initialized. • Bootloader usually initializes hardware devices. – UBoot will do. • In skyeye, all devices are emulated and can be used immediately. – Notice: Your code might be capable of running successfully but can’t run correctly on target board.
  • 88.
    46 Simple Hello Worldon PXA270 .global _start .text _start: ldr sp, =0xa0001000 ldr r1, =0x40100000 ldr r2, h str r2, [r1] bl c_start 1: b 1b h: .byte 'h' void c_start(void) { *((unsigned long volatile*)(0x40100000)) = 'H'; *((unsigned long volatile*)(0x40100000)) = 'e'; *((unsigned long volatile*)(0x40100000)) = 'l'; *((unsigned long volatile*)(0x40100000)) = 'l'; *((unsigned long volatile*)(0x40100000)) = 'o'; *((unsigned long volatile*)(0x40100000)) = ' '; *((unsigned long volatile*)(0x40100000)) = '!'; *((unsigned long volatile*)(0x40100000)) = 'n'; *((unsigned long volatile*)(0x40100000)) = 'r'; } start.s c_start.c
  • 89.
    47 Simple Hello Worldon PXA270 linux:$ arm_v5t_le-as start.c -o start.o linux:$ arm_v5t_le-gcc -c c_start.c -o c_start.o linux:$ arm_v5t_le-ld –Ttext 0x0 -e _start start.o c_start.o –o start linux:$ arm_v5t_le-ld –T start.ld start.o c_start.o –o start ENTRY(_start) SECTIONS { .text 0x0 : {*(.text)} .data : {*(.data)} .bss : {*(.bss)} } start.ld or linux:$ skyeye -e start
  • 90.
    48 Conclusions • The Cruntime environment must be set before entering C code. • A simple CRT includes: – Set stack pointer – Clear BSS – If there is a GOT, relocating the GOT. – Jump to main • Note: A complete CRT contains complex functions. – C library initialization – Exit code handling – …