#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>

enum { EH_SIZE = 64, PH_SIZE = 56, MAX_INPUT = 64 * 1024 * 1024 };

static uint16_t le16(const unsigned char *p) {
    return (uint16_t)((uint16_t)p[0] | (uint16_t)p[1] << 8);
}
static uint32_t le32(const unsigned char *p) {
    return (uint32_t)p[0] | (uint32_t)p[1] << 8 |
           (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
}
static uint64_t le64(const unsigned char *p) {
    return (uint64_t)le32(p) | (uint64_t)le32(p + 4) << 32;
}
static int reject(const char *reason) {
    fprintf(stderr, "ELF rejected: %s\n", reason);
    return 1;
}
static int parse_elf(const unsigned char *data, size_t size) {
    if (size < EH_SIZE) return reject("short header");
    if (memcmp(data, "\177ELF", 4) != 0) return reject("magic");
    if (data[4] != 2 || data[5] != 1)
        return reject("only ELF64 little-endian is supported");
    if (data[6] != 1 || le32(data + 20) != 1)
        return reject("version");
    if (le16(data + 52) != EH_SIZE) return reject("e_ehsize");
    uint64_t phoff = le64(data + 32);
    uint16_t count = le16(data + 56);
    if (count == 0xffff) return reject("PN_XNUM is unsupported");
    if (count != 0 && le16(data + 54) != PH_SIZE)
        return reject("e_phentsize");
    // Check offset first, then remaining bytes / entry size. No wrapping addition/multiplication.
    if (phoff > size || count > (size - (size_t)phoff) / PH_SIZE)
        return reject("program header file bounds");
    for (size_t i = 0; i < count; ++i) {
        const unsigned char *ph = data + (size_t)phoff + i * PH_SIZE;
        if (le32(ph) != 1) continue; // PT_LOAD
        uint64_t offset = le64(ph + 8), vaddr = le64(ph + 16);
        uint64_t filesz = le64(ph + 32), memsz = le64(ph + 40);
        uint64_t align = le64(ph + 48);
        if (offset > size || filesz > size - (size_t)offset)
            return reject("segment file bounds");
        if (filesz > memsz) return reject("p_filesz > p_memsz");
        if (memsz > UINT64_MAX - vaddr) return reject("virtual address overflow");
        if (align > 1 && ((align & (align - 1)) != 0 ||
                          offset % align != vaddr % align))
            return reject("segment alignment");
        printf("PT_LOAD[%zu] offset=0x%" PRIx64 " vaddr=0x%" PRIx64
               " filesz=0x%" PRIx64 " memsz=0x%" PRIx64 "\n",
               i, offset, vaddr, filesz, memsz);
    }
    puts("Inventory checks passed; loadability was not tested.");
    return 0;
}
int main(int argc, char **argv) {
    if (argc != 2) { fprintf(stderr, "Usage: %s ELF\n", argv[0]); return 2; }
    FILE *file = fopen(argv[1], "rb");
    if (!file) { perror("fopen"); return 2; }
    if (fseek(file, 0, SEEK_END) != 0) { fclose(file); return 2; }
    long length = ftell(file);
    if (length < 0 || (uintmax_t)length > MAX_INPUT ||
        (uintmax_t)length > SIZE_MAX) {
        fclose(file); return reject("file size / 64 MiB limit");
    }
    rewind(file);
    size_t size = (size_t)length;
    unsigned char *data = malloc(size ? size : 1);
    if (!data) { fclose(file); return 2; }
    int bad = fread(data, 1, size, file) != size || ferror(file);
    if (fclose(file) != 0) bad = 1;
    int result = bad ? reject("could not read file") : parse_elf(data, size);
    free(data);
    return result;
}
