Reposition PC Relative Reference (pc-relative reference relocation)

Source: Internet
Author: User

Reposition PC Relative Reference (pc-relative reference relocation)


The sample code (MAIN.C, SWAP.C) is shown in an earlier article, which is followed by an analysis of the target file.

<pic1 gcc-c main.o, Objdump-rs main.o>


You can see that the call command calling swap in main is at section offset 0x1d, the opcode is E8, the operand is 32bit reference 0XFFFFFFFC, which is -4 (little endian stored), The next line shows the relocation entry,r.offset=0x1e of the swap symbol, R.symbol=swap, R.TYPE=R_386_PC32. This relocation entry is to tell the compiler to modify the 32-bit reference located at the section at an offset of 1e so that the runtime can point to swap.

<pic2, Readelf-a p>


<PIC3, Objdump-s p>


Depending on <PIC2> or <PIC3> you can see the final executable section of the target file. The address of text ADDR (s) = ADDR (. Text) =0x8048320, according to <PIC3> You can see that the last executable target file in swap's instruction address is ADDR (r.symbol) =addr (Swap) =0x804840c. Then reposition a PC relative reference:

Referenced run-time address:
REFADDR = ADDR (s) + R.offset = 0x8048320 + 0x1e = 0x804833e
To modify the value of a reference:
*refptr = (unsigned) (ADDR (R.symbol) + (*REFPTR-REFADDR))
= (unsigned) (0x804840c + ( -4-0x804833e))
= (unsigned) (0XCA) ( question: Why not equal to the following 0x16?? experimented with several times. )
"The meaning of the above is to correct the referenced value according to the actual address of the swap symbol," Pan "
In the final executable target file, when executing to the following instruction:
80483F1: E8 804840c <swap>

The essence of the call instruction is to stack the current EIP (=80483F6) and then load the function entry address into the EIP. To begin executing the SWAP code block instructions.


Reference:

(1) "Csapp"


Reposition PC Relative Reference (pc-relative reference relocation)

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.