~yujiri/compiler

0691e6ed014200657ce75beb94a1faccb01d490c — Evin Yulo 8 months ago 6c5e189
small elf
1 files changed, 21 insertions(+), 24 deletions(-)

M elf.zig
M elf.zig => elf.zig +21 -24
@@ 6,46 6,43 @@ pub fn main() !void {
    const writer = file.writer();
    try writeElfHeader(writer);

    // addr 0x40: 1st program header entry
    // 64: 1st program header entry
    // segment type: loadable
    try writer.writeIntNative(u32, 1);
    // strange flags
    try writer.writeIntNative(u32, 4);
    // offset of the segment in file image
    try writer.writeIntNative(u64, 0x0);
    try writer.writeIntNative(u64, 0);
    // virtual addr of segment in memory
    try writer.writeIntNative(u64, 0x400000);
    // physical addr
    try writer.writeIntNative(u64, 0x400000);
    // size of segment in file image
    try writer.writeIntNative(u64, 428);
    try writer.writeIntNative(u64, 260);
    // size of segment in memory
    try writer.writeIntNative(u64, 428);
    try writer.writeIntNative(u64, 260);
    // alignment
    try writer.writeIntNative(u64, 4096);
    try writer.writeIntNative(u64, 8);

    // addr 0x78: 2nd program header entry. this is the instructions and the message.
    // 120: 2nd program header entry. this is the instructions and the message.
    // segment type: loadable
    try writer.writeIntNative(u32, 1);
    // strange flags
    try writer.writeIntNative(u32, 5);
    // offset of the segment in file image
    try writer.writeIntNative(u64, 0x1000);
    try writer.writeIntNative(u64, 176);
    // virtual addr of segment in memory
    try writer.writeIntNative(u64, 0x401000);
    try writer.writeIntNative(u64, 0x4000b0);
    // physical addr
    try writer.writeIntNative(u64, 0x401000);
    try writer.writeIntNative(u64, 0x4000b0);
    // size of segment in file image
    try writer.writeIntNative(u64, 29);
    // size of segment in memory
    try writer.writeIntNative(u64, 29);
    // alignment
    try writer.writeIntNative(u64, 4096);

    // useless filler
    try writer.writeByteNTimes(0, 3920);
    try writer.writeIntNative(u64, 8);

    // addr: 0x1000: exe
    // 176: exe
    // mov 60 to rax
    try writer.writeAll("\x48\xc7" ++ "\xc0" ++ "\x3c\x00\x00\x00");
    // mov 6 to rdi


@@ 55,11 52,11 @@ pub fn main() !void {
    // string
    try writer.writeAll("Hello, world\n");

    // addr 0x101d: strtab
    // 205: strtab
    // zero it for now.
    try writer.writeByteNTimes(0, 59);

    // addr 0x1058: 1st section header
    // 264: 1st section header
    // name offset
    try writer.writeIntNative(u32, 49);
    // type: instructions


@@ 67,11 64,11 @@ pub fn main() !void {
    // flags
    try writer.writeIntNative(u64, 6);
    // virtual addr
    try writer.writeIntNative(u64, 0x401000);
    try writer.writeIntNative(u64, 0x4000b0);
    // offset of the section content in file
    try writer.writeIntNative(u64, 0x1000);
    try writer.writeIntNative(u64, 176);
    // size
    try writer.writeIntNative(u64, 0x1d);
    try writer.writeIntNative(u64, 29);
    // link: section index of an association section (prob irrelevant)
    try writer.writeIntNative(u32, 0);
    // extra info


@@ 81,7 78,7 @@ pub fn main() !void {
    // section content entry size (unused)
    try writer.writeIntNative(u64, 0);

    // addr 0x1098: 2nd section header
    // 328: 2nd section header
    // name offset
    try writer.writeIntNative(u32, 1);
    // type: strtab


@@ 91,9 88,9 @@ pub fn main() !void {
    // virtual addr
    try writer.writeIntNative(u64, 0);
    // offset of the section content in file
    try writer.writeIntNative(u64, 0x101d);
    try writer.writeIntNative(u64, 205);
    // size
    try writer.writeIntNative(u64, 37);
    try writer.writeIntNative(u64, 59);
    // link: section index of an association section (prob irrelevant)
    try writer.writeIntNative(u32, 0);
    // extra info


@@ 126,11 123,11 @@ pub fn writeElfHeader(writer: anytype) !void {
    // another elf version field?
    try writer.writeIntNative(u32, 1);
    // entrypoint addr
    try writer.writeIntNative(u64, 0x401000);
    try writer.writeIntNative(u64, 0x4000b0);
    // start of program header
    try writer.writeIntNative(u64, 64);
    // start of section header
    try writer.writeIntNative(u64, 0x1058);
    try writer.writeIntNative(u64, 264);
    // flags
    try writer.writeIntNative(u32, 0);
    // size of elf header