private void SetupEntry(string library, string entryPoint) { if (!libraries.Contains(library)) { Require.False(entryPoints.Contains(entryPoint)); libraries.Add(library); mainRegion.WriteNumber(1); //DT_NEEDED if (!library.EndsWith(".so")) { library = "lib" + library.Replace('.', '-') + ".so"; } mainRegion.WriteNumber(dynstr.Get(library)); } if (!entryPoints.Contains(entryPoint)) { entryPoints.Add(entryPoint); Placeholder p = pltgot.CurrentLocation; int symbolIndex = dynsym.Write(Placeholder.Null, entryPoint, 16); plt.Align(16, 0x90); // nop pltgotEntry.Add(entryPoint, p); pltEntry.Add(entryPoint, plt.CurrentLocation); if (plt.Is64Bit) { plt.Write(new byte[] { 0xff, 0x25 }); // jmp [rip+imm32] plt.WritePlaceholderDisplacement32(p); pltgot.WritePlaceholder(plt.CurrentLocation); plt.WriteByte(0x68); // push IMMS32 plt.WriteInt32(relocator.AddJumpSlot(p, symbolIndex)); plt.WriteByte(0xe9); // jmp rip+imm32 plt.WritePlaceholderDisplacement32(plt0); } else { plt.Write(new byte[] { 0xff, 0x25 }); // jmp [imm32] plt.WritePlaceholder(p); pltgot.WritePlaceholder(plt.CurrentLocation); plt.WriteByte(0x68); // push IMMS32 plt.WriteInt32(relocator.AddJumpSlot(p, symbolIndex)); plt.WriteByte(0xe9); // jmp imm32 plt.WritePlaceholder(plt0); } } }
private void WriteProgramHeaders() { output.Align(16, 0); programHeaderTable_fo.SetValue(output.Length); long phdrfo = output.Length; //phdr WriteProgramHeader(6, 4, phdrfo, phdrma, phdrsz, 16); //interp WriteProgramHeader(3, 4, interpSection.FileOffset, interpSection.MemoryAddress, interpSection.Length, interpSection.RegionAlignment); //readonly // WriteProgramHeader(1, 4, interpSection.FileOffset, interpSection.MemoryAddress, readonlyLength, MaxPageSize); //fileimage (readonly + executable) WriteProgramHeader(1, 5, 0, imageBase, fileImageLength, MaxPageSize); // //executable // WriteProgramHeader(1, 5, codeSection.FileOffset, codeSection.MemoryAddress, execLength, MaxPageSize); //readwrite WriteProgramHeader(1, 6, dataSection.FileOffset, dataSection.MemoryAddress, readWriteLength, MaxPageSize); //dynamic WriteProgramHeader(2, 6, dynamicSection.FileOffset, dynamicSection.MemoryAddress, dynamicSection.Length, dynamicSection.RegionAlignment); }
public Importer(Sections sections) { relapltRegion = sections.GetSection(".rela.plt").AllocateRegion(); relocator = new Relocator(relapltRegion); mainRegion = sections.GetSection(".dynamic").AllocateRegion(); dynstrRegion = sections.GetSection(".dynstr").AllocateRegion(); dynstr = new StringTable(); plt = sections.GetSection(".plt").AllocateRegion(); pltgot = sections.GetSection(".got.plt").AllocateRegion(); dynsym = new DynamicSymbols(sections, dynstr); dynamicTokenSize = dynsym.Write(mainRegion.CurrentLocation, "_DYNAMIC"); mainRegion.WriteNumber(3); // dt_pltgot mainRegion.WritePlaceholder(pltgot.BaseLocation); mainRegion.WriteNumber(4); // dt_hash mainRegion.WritePlaceholder(dynsym.HashRegion.BaseLocation); mainRegion.WriteNumber(5); // dt_strtab mainRegion.WritePlaceholder(dynstrRegion.BaseLocation); mainRegion.WriteNumber(6); // dt_symtab mainRegion.WritePlaceholder(dynsym.SymbolRegion.BaseLocation); mainRegion.WriteNumber(11); // dt_syment mainRegion.WriteNumber(dynsym.ElementSize); // mainRegion.WriteNumber(15); // dt_rpath // mainRegion.WriteNumber(dynstr.Get(".:/lib64:/lib")); plt0 = plt.CurrentLocation; pltgot.WriteNumber(0); if (plt.Is64Bit) { plt.Write(new byte[] { 0xff, 0x35 }); // push [rip+imm32] plt.WritePlaceholderDisplacement32(pltgot.CurrentLocation); pltgot.WriteNumber(0); plt.Write(new byte[] { 0xff, 0x25 }); // jmp [rip+imm32] plt.WritePlaceholderDisplacement32(pltgot.CurrentLocation); pltgot.WriteNumber(0); } else { plt.WriteByte(0x68); // push IMM32 plt.WritePlaceholder(pltgot.CurrentLocation); pltgot.WriteNumber(0); plt.Write(new byte[] { 0xff, 0x25 }); // jmp [imm32] plt.WritePlaceholder(pltgot.CurrentLocation); pltgot.WriteNumber(0); } plt.Align(16, 0x90); // nop }
public override Placeholder AddTextLengthPrefix(string text) { if (textData.ContainsKey(text)) { return(textData[text]); } if (textRegion == null) { textRegion = AllocateDataRegion(); } Placeholder result = textRegion.CurrentLocation; byte[] bytes = (new System.Text.UTF8Encoding()).GetBytes(text); textRegion.WriteNumber(bytes.Length); textRegion.Write(bytes); textRegion.WriteByte(0); textRegion.Align(16, 0); textData[text] = result; return(result); }