Skip to content

Commit

Permalink
need to load GOT into EBX before calling PLT function
Browse files Browse the repository at this point in the history
- also save/restore old EBX value
  • Loading branch information
MartinNowak committed Mar 14, 2013
1 parent c8c3ba1 commit dee2fbf
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions src/backend/elfobj.c
Expand Up @@ -3206,17 +3206,38 @@ static void obj_rtinit()
assert(!buf->size());
size_t off = 0;

{
// 16-byte align for call
const size_t sizeof_dso = 6 * NPTRSIZE;
const size_t align = -(2 * NPTRSIZE + sizeof_dso) & 0xF;

// enter align, 0
buf->writeByte(0xC8);
buf->writeByte(align & 0xFF);
buf->writeByte(align >> 8 & 0xFF);
buf->writeByte(0);
off += 4;
// 16-byte align for call
const size_t sizeof_dso = 6 * NPTRSIZE;
const size_t align = I64 ?
// return address, RBP, DSO
(-(2 * NPTRSIZE + sizeof_dso) & 0xF) :
// return address, EBP, EBX, DSO, arg
(-(3 * NPTRSIZE + sizeof_dso + NPTRSIZE) & 0xF);

// enter align, 0
buf->writeByte(0xC8);
buf->writeByte(align & 0xFF);
buf->writeByte(align >> 8 & 0xFF);
buf->writeByte(0);
off += 4;

if (!I64)
{ // see cod3_load_got() for reference
// push EBX
buf->writeByte(0x50 + BX);
off += 1;
// call L1
buf->writeByte(0xE8);
buf->write32(0);
// L1: pop EBX (now contains EIP)
buf->writeByte(0x58 + BX);
off += 6;
// add EBX,_GLOBAL_OFFSET_TABLE_+3
buf->writeByte(0x81);
buf->writeByte(modregrm(3,0,BX));
buf->write32(3);
ElfObj::addrel(codseg, off + 2, RI_TYPE_GOTPC, Obj::external(Obj::getGOTsym()), 0);
off += 6;
}

int reltype = I64 ? R_X86_64_PC32 : RI_TYPE_SYM32;
Expand Down Expand Up @@ -3297,24 +3318,10 @@ static void obj_rtinit()
off += 8;
}
else
{ // see cod3_load_got() for reference
// call L1
buf->writeByte(0xE8);
buf->write32(0);
// L1: pop ECX (now contains EIP)
buf->writeByte(0x58 + CX);
off += 6;

// add ECX,_GLOBAL_OFFSET_TABLE_+3
buf->writeByte(0x81);
buf->writeByte(modregrm(3,0,CX));
buf->write32(3);
ElfObj::addrel(codseg, off + 2, RI_TYPE_GOTPC, Obj::external(Obj::getGOTsym()), 0);
off += 6;

{
// cmp foo[GOT], 0
buf->writeByte(0x81);
buf->writeByte(modregrm(2,7,CX));
buf->writeByte(modregrm(2,7,BX));
buf->write32(0);
buf->write32(0);
ElfObj::addrel(codseg, off + 2, RI_TYPE_GOT32, symidx, 0);
Expand Down Expand Up @@ -3374,6 +3381,13 @@ static void obj_rtinit()

#endif // REQUIRE_DSO_REGISTRY

if (!I64)
{ // mov EBX,[EBP-4-align]
buf->writeByte(0x8B);
buf->writeByte(modregrm(1,BX,BP));
buf->writeByte(-4-align);
off += 3;
}
// leave
buf->writeByte(0xC9);
// ret
Expand Down

0 comments on commit dee2fbf

Please sign in to comment.