简体   繁体   中英

Force entries in PLT section

For reasons, I want to force some entries in the PLT section. I 've managed to do this for some entries, however some functions are never inserted in PLT.

so, lets say that I have main.c :

int main(){
// some code
strcmp(a,b); //suppose a, b strings
// more code
// suppose that result of strcmp is used, so it won't be optimised away
}

and help.c :

// declaration
void forceplt() __attribute__((__used__));
// definition
void forceplt(){
abs(-1);
__aeabi_dadd(0, 0);
}

Then, I compile and link the above two relocatable objects, into an executable object. To make sure that the abs and dadd functions will remain in the PLT section, I compile helper.o using -O0 . main.o can be compiled with a higher level, lets say `-O1'.

On the executable object I would expect to see entries of all 3 functions: strcmp , abs , and dadd . However, this is not the case.

Am I missing something here? Is it possible that linker omits some stuff because it figures out that they are never called? Despite using O0 for helper, and used attribute for forceplt function?

Help me do a proper celebration of.. ten. million. questions!

Cheers!

The abs and strcmp are recognized specially by GCC and it seems the compiler figures out that those calls are useless even in -O0 . If you look at the generate assembly ( gcc foo.c -S -o- ), you see that there is no corresponding call instruction.

PLT entries are not generated by the compiler: they are not present in the .o files. They are generated by the link editor when they find a corresponding relocation in one of their input .o files. In order to generate a PLT entry, you need to generate a suitable relocation entry in one of your input .o files. A .o relocation always apply to a part of a section of the .o file: you need to have some memory bytes in some section of the some .o that you will relocate with a PLT-aware relocation. It can by either an (possibly useless) instruction or some other (possibly useless) bytes.

A solution is to create (in assembly) instructions which explicitly use the expected PLT entries. With x86_64 this can be done with:

  .text
  .type forceplt2,@function
forceplt2:
  callq abs@plt

I guess it's written like this in ARM:

  .text
  .type forceplt2,%function
forceplt2:
  bl abs@plt

Or you can generate raw bytes for your relocation (on x86_64 again):

   .data
   .align  8
   .type   forceplt3, @object
   .size   forceplt3, 8
 forceplt3:
   .quad   socket@GOTPLT

On x_86_64 , you should be able to use either @GOT , @GOTPLT , @GOTOFF , @GOTPCREL , @PLT or @PLTOFF . I'm not so sure how this is supposed to be written for ARM .

Any of these solutions will emit a suitable relocation entry which will cause the link editor to emit a PLT entry and a PLT GOT entry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM