Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIC code #40

Open
BenShen98 opened this issue Mar 20, 2019 · 3 comments
Open

PIC code #40

BenShen98 opened this issue Mar 20, 2019 · 3 comments

Comments

@BenShen98
Copy link

  1. Does the two instruction below means set up the stack pointer for position independent coding?
   lui    $28,%hi(__gnu_local_gp)
   addiu  $28,$28,%lo(__gnu_local_gp)
  1. What does the following mean?
  • .cprestore
  • .reloc
  • .cpload
foo:    .set    noreorder
        .cpload $25
        .set    reorder

        subu    $29, $29, 32
        .cprestore 16
.cprestore 16
.reloc 1f,R_MIPS_JALR,foo

Below are the complete code

testnif:
   .frame $fp,32,$31    # vars= 0, regs= 2/0, args= 16, gp= 8
   .mask  0xc0000000,-4
   .fmask 0x00000000,0
   .set   noreorder
   .set   nomacro
   addiu  $sp,$sp,-32
   sw $31,28($sp)
   sw $fp,24($sp)
   move   $fp,$sp
   lui    $28,%hi(__gnu_local_gp)
   addiu  $28,$28,%lo(__gnu_local_gp)
   .cprestore 16
   sw $4,32($fp)
   lw $2,%call16(foo)($28)
   move   $25,$2
   .reloc 1f,R_MIPS_JALR,foo
1: jalr   $25
   nop

   lw $28,16($fp)
   slt    $2,$2,5
   bne    $2,$0,$L2
   nop

   li $2,3         # 0x3
   b  $L3
   nop

$L2:
   li $2,1         # 0x1
$L3:
   move   $sp,$fp
   lw $31,28($sp)
   lw $fp,24($sp)
   addiu  $sp,$sp,32
   j  $31
   nop

   .set   macro
   .set   reorder
   .end   testnif
   .size  testnif, .-testnif
   .ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"
@johnwickerson
Copy link
Contributor

Ah, this is one for Google/StackOverflow in the first instance. See here and here etc.

@BenShen98
Copy link
Author

BenShen98 commented Mar 21, 2019

Hi John,
after reading the wiki, I still didn't quite get it.

So if a function is doing something that clobbers it, the value needs to be restored before returning.

Does it mean if I don't change $28, I don't need to save .cprestore?
But I still would get the warning of

Warning: no .cprestore pseudo-op used in PIC code

Also, does the 16 in .cprestore 16 have special meaning? Where is 16 calculated from?

@ymherklotz
Copy link
Member

I believe if you do not change it you will not need it, but I do not think that it hurts to add it. The 16 stands for the offset that the $gp should be stored at so that it can be restored later (https://reviews.llvm.org/D6267).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants