Dynamic Loading of Cortex-M3 1 (address-independent code implementation)

Source: Internet
Author: User

This article explains how to implement address independence. Then, you can check the Assembly and CPU instruction manual and analyze and solve your doubts.

The -- acps/ropi option needs to be set when compiling C code, as shown in the following example:

SystemInit (fun_for_sub (j =; j >=; j -- main ((}C-example

 

Compile:

armcc  -c --cpu Cortex-M3 -O0 --apcs=interwork --apcs /ropi/rwpi -o main.o main.c

 

Use fromelf to view assembly code

fromelf.exe -s -c main.o

 

The assembly code generated by text segments is as follows:

** Section Size: 14 bytes (alignment 2 Address: 0x00000000 0x00000000: 4770 0x00000002: 4770 0x00000004 0x00000006: f7fffffe .... BL fun_for_sub; 0x2 Section 0x0000000a: 205a z movs r0, 0x0000000c: bd00 .. POP {pc}Assembly instructions

 

View the compilation code for calling the function fun_for_sub:

0x00000006:    f7fffffe       ....    BL       fun_for_sub ; 0x2 Section 

 

Find the arm ddi0403d_arm_ubunture_v7m_reference_manual_errata_markup_00000.pdf. The explanation of the BL command is as follows:

Branch with Link (immediate) calls a subroutine at a PC-relative address.

We know that BL is a PC-related instruction.

For details, refer to the composition of the BL command:

 

 

According to the command f7fffffe,

Corresponding:

f7ff  :  15  14  13  12  11  10  9  8  7  6  5  4  3  2  1        1   1   1   1   0   1  1  1  1  1  1  1  1  1  1
fffe  :  15  14  13  12  11  10  9  8  7  6  5  4  3  2  1        1   1   1   1   1   1  1  1  1  1  1  1  1  1  0

 

Symbol bit S = 1, J1 = 1, J2 = 1, imm10 = 11 1111 1111, imm11 = 111 1111 1110

 

So I1 =! (J1 ~ S) = 1, I2 =! (J2 ~ S) = 1,

Imm32 = SignExtend (S: I1: I2: imm10: imm11: '0', 32) = SignExtend (1: 1: 1: 11 1111 11: 111 1111 1110: '0 ', 32) = 1111 1111 1111 1111 1111 1111 1111 1100 = 0 xfffffffc.

0xfffffffc is the complement code of-4, and the current PC is 0x00000006,

Then, based on the last step of the above Operation, BranchWritePC (PC + imm32)

The final jump jumps to the address 0x6 + (-4) = 0x2, that is, the address of the function fun_for_sub. Therefore, the address-independent code is implemented based on the current PC.

Similar principles are also applied in the X86 platform, which is based on PC-related jump commands. Programmer self-cultivation-links, loads and libraries.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.