0.2.0-dev Download ZIP
46 lines ·1.9 KB Raw
| 1 | using System.Text; |
| 2 | |
| 3 | namespace EvilEngine.Adapters.Tests.Support; |
| 4 | |
| 5 | public static class ElfWriter |
| 6 | { |
| 7 | public static byte[] Build(byte[] buildId, string section = ".note.gnu.build-id") |
| 8 | { |
| 9 | var note = new byte[16 + (buildId.Length + 3) / 4 * 4]; |
| 10 | BitConverter.GetBytes(4).CopyTo(note, 0); |
| 11 | BitConverter.GetBytes(buildId.Length).CopyTo(note, 4); |
| 12 | BitConverter.GetBytes(3).CopyTo(note, 8); |
| 13 | "GNU\0"u8.CopyTo(note.AsSpan(12)); |
| 14 | buildId.CopyTo(note, 16); |
| 15 | var names = Encoding.ASCII.GetBytes("\0.shstrtab\0" + section + "\0"); |
| 16 | const int header = 64; |
| 17 | var noteAt = header; |
| 18 | var namesAt = noteAt + note.Length; |
| 19 | var table = (namesAt + names.Length + 7) / 8 * 8; |
| 20 | var image = new byte[table + 3 * 64]; |
| 21 | "\u007fELF"u8.CopyTo(image); |
| 22 | image[4] = 2; |
| 23 | image[5] = 1; |
| 24 | image[6] = 1; |
| 25 | BitConverter.GetBytes((ushort)2).CopyTo(image, 0x10); |
| 26 | BitConverter.GetBytes((ushort)0x3E).CopyTo(image, 0x12); |
| 27 | BitConverter.GetBytes((ulong)table).CopyTo(image, 0x28); |
| 28 | BitConverter.GetBytes((ushort)64).CopyTo(image, 0x34); |
| 29 | BitConverter.GetBytes((ushort)64).CopyTo(image, 0x3A); |
| 30 | BitConverter.GetBytes((ushort)3).CopyTo(image, 0x3C); |
| 31 | BitConverter.GetBytes((ushort)2).CopyTo(image, 0x3E); |
| 32 | note.CopyTo(image, noteAt); |
| 33 | names.CopyTo(image, namesAt); |
| 34 | Section(image, table + 64, 11, 7, noteAt, note.Length); |
| 35 | Section(image, table + 128, 1, 3, namesAt, names.Length); |
| 36 | return image; |
| 37 | } |
| 38 | |
| 39 | private static void Section(byte[] image, int at, uint name, uint type, long offset, long size) |
| 40 | { |
| 41 | BitConverter.GetBytes(name).CopyTo(image, at); |
| 42 | BitConverter.GetBytes(type).CopyTo(image, at + 4); |
| 43 | BitConverter.GetBytes((ulong)offset).CopyTo(image, at + 24); |
| 44 | BitConverter.GetBytes((ulong)size).CopyTo(image, at + 32); |
| 45 | } |
| 46 | } |