Understand the runtime address and link address from the two-statement assembly, and the Assembly link address

Source: Internet
Author: User

Understand the runtime address and link address from the two-statement assembly, and the Assembly link address

First, let's look at two lines of assembly code:

   1:  adr r0, _start  
   2:  ldr r1, =_start 

Load the address value of a label. What is the difference between adr and ldr? Note that the ldr here is not a command ldr, but a pseudo command ldr. If you want to differentiate them, please refer to my blog "Summary of adr adrl ldr mov".

To distinguish them, we need to introduce four concepts:

1. Starting position of the runtime address: the starting position specified by the chip company. This location is related to the chip itself and cannot be changed. For 2440, it is generally the first address 0x0 of the On-Chip SRAM; for 210, It is the address 0xD0020010 in the On-Chip SRAM.

2. Starting position of the link address: it is specified by the programmer, or configured with a link script. Yes. However, this location is determined after the program Link.

3. runtime address: the runtime address is displayed right after the starting position (including the starting position) of the runtime address.

4. link address: the link address is arranged from the starting position (including the starting position) of the link address.

  

After explaining the above four points, I need to provide some prerequisites: adr r0, _ start; ldr r1, = _ start

These two sentences are directly extracted from an experiment program of instructor Zhu. The purpose of this experiment is to demonstrate the relocation. I will post this code to the end of the article. Because the Development Board is a 210 board, the runtime address starts from 0xd0020010, And the link address starts from 0xd0024000.

After the entire program is compiled and decompiled, we will find the disassembly content corresponding to adr r0, _ start; ldr r1, = _ start:

1. adr r0, _ start corresponds to: d002401c: e24f0024 sub r0, pc, #36; 0x242, ldr r1, = _ start corresponds to: d0024020: e59f1048 ldr r1, [pc, #72]; d0024070 <run_on_dram + 0x10>

  

It is also the address for loading _ start, but it is a different command after disassembly. First, we need to look at the disassembly. The leftmost is the link address, the second is the machine code, and the third is the content produced by the disassembly, next to the right semicolon, the disassembly compiler will help us annotate some additional content for our convenience.

We found that, after disassembly, there is a very different place, that is, the pc pointer. Ldr r1, = _ start indicates that the pc pointer for disassembly is put in [], but not in another disassembly. We know that for an assembly, put it in [] indicates that the value of the Register is obtained and the value of the Register is treated as an address to access the value stored in the address.

For pc, When you directly read the value of pc, you access the runtime address, and when you read the value of [pc], you access the link address.

In contrast, adr r0, _ start, and ldr r1, = _ start are both pseudo commands, meaning reading the runtime address and reading the link address respectively. It is consistent with the meaning of disassembly.

Now let's verify whether the previous analysis is correct. First, _ start is the beginning of the program. If _ start corresponds to the runtime address, the value of _ start should be the start position of the runtime address and 0xd0020010.

Observe the Disassembly and corresponding Compilation

1、adr r0, _start d002401c:   e24f0024  sub r0, pc, #36 ; 0x24

In this case, the link address of the Code is 0xd0024000 and the offset is 0x1c, according to this cheap amount, the runtime address of the code can be calculated as 0xd0020010 + 0x1c = d002002C. the previously mentioned pc value corresponds to the runtime address, so pc = d002002C.

D002002C-(36 decimal) + 8 (pipeline) = D002 0010; the runtime address of _ start is exactly correct.

Check whether the link address is incorrect. First, _ start is the beginning of the program. Therefore, if _ start corresponds to the link address, the value of _ start should be the starting position of the link address and the previously set 0xd0024000.

2、ldr r1, =_start d0024020:   e59f1048  ldr r1, [pc, #72] ; d0024070 <run_on_dram+0x10>

According to the offset, the runtime address of this sentence is d0020030. If it is the runtime address + offset (72 decimal), the result is D002 0078, plus 8 (pipeline) equals to D002 0080, obviously not.

Obviously, the [pc] Here is worth the link address corresponding to the current statement, d0024020 + offset (72 decimal) + 8 is equivalent to D002 4070 (this value is exactly the value in the comment). Isn't it strange? Why is the value not 0xd0024000? Is it an error? Actually not. If you go to the D002 4070 link, you will find that the stored value is exactly D002 4070.

The Code is as follows: d0024070: d0024000 andle r4, r2, r0

The command ldr r1, [pc, #72] is used to access the value in the address. (This jump method is actually used to cope with illegal and immediate numbers, resulting in the inability to place commands and data in a machine code)

  

The Code of instructor Zhu is as follows:

1/* 2 * file name: led. s 3 * Author: Instructor Zhu 4 * Description: Demo relocation (internal relocation in SRAM) 5 */6 7 # define WTCON 0xE2700000 8 9 # define SVC_STACK 0xd0037d8010 11. global _ start // change the _ start link attribute to external, so that other files can see _ start 12 _ start: 13 // step 1st: close the watchdog (write 0 to bit5 of WTCON) 14 ldr r0, = WTCON15 ldr r1, = 0x016 str r1, [r0] 17 18 // step 2nd: set SVC stack 19 ldr sp, = SVC_STACK20 21 // Step 4: On/Off icache22 mrc p15, 0, r0, c1, c0, 0; // read cp15 c1 to r0 23 // bic r0, r0, #(1 <12 )/ /Bit12 sets 0 to icache24 orr r0, r0, #(1 <12) // bit12 sets 1 to enable icache25 mcr p15, 0, r0, c1, c0, 0; 26 27 // Step 2: relocate 28 // The adr command is used to load the _ start current run address 29 adr r0, when _ start // adr is loaded, it is called short loading 30 // the URL of the ldr command used to load _ start: 0xd002400031 ldr r1, = _ start // during ldr loading, if the target register is pc, it is called Long Jump. If the target register is r1, it is called long loading 32 // The starting address of bss segment 33 ldr r2, = bss_start // The end address of the Code to be relocated. Only the code segment and data segment to be relocated can be 34 cmp r0, r1 // compare whether the runtime address and link address of _ start are equal 35 beq clean_bss // if they are equal, it is not required Therefore, skip copy_loop and directly go to clean_bss36 // if they are not equal, it is required to be relocated. then execute the following copy_loop to relocate 37 // after the relocation is completed, continue to execute clean_bss. 38 39 // A while LOOP 40 copy_loop: 41 ldr r3, [r0], #4 // source 42 str r3, [r1], #4 // objective the two sentences of code have completed the copying of 4 bytes of content 43 cmp r1, r2 // r1 and r2 are both loaded with ldr and are link addresses, therefore, r1 + 4 always equals to r244 bne copy_loop45 46 // clear bss segment. In fact, it is to clear all bss segments at the link address 47 clean_bss: 48 ldr r0, = bss_start 49 ldr r1, = bss_end50 cmp r0, r1 // If r0 is equal to r1, the bss segment is empty, go directly to 51 beq run_on_dram // clear the address 52 mov r2 after bss, #053 clear_loop: 54 str r2, [r0], #4 // first place the value in r2 into the memory address pointed to by r0 (the value in r0 serves as the memory address), 55 cmp r0, r1 // then r0 = r0 + 456 bne clear_loop57 58 run_on_dram: 59 // long jump to led_blink and start the second stage 60 ldr pc, = led_blink // ldr command for Long Jump 61 62 // The final loop of the Assembly cannot be lost 63 B.View Code

 

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.