NAME=32: Elf header size
FILE=malloc://1024
CMDS=<<EOF
pfo elf32
pfs.elf_header
EOF
EXPECT=<<EOF
52
EOF
RUN

NAME=64: Elf header size
FILE=malloc://1024
CMDS=<<EOF
pfo elf64
pfs.elf_header
EOF
EXPECT=<<EOF
64
EOF
RUN

NAME=32: Elf program header size
FILE=malloc://1024
CMDS=<<EOF
pfo elf32
pfs.elf_phdr
EOF
EXPECT=<<EOF
32
EOF
RUN

NAME=64: Elf program header size
FILE=malloc://1024
CMDS=<<EOF
pfo elf64
pfs.elf_phdr
EOF
EXPECT=<<EOF
56
EOF
RUN

NAME=32: Elf section header size
FILE=malloc://1024
CMDS=<<EOF
pfo elf32
pfs.elf_shdr
EOF
EXPECT=<<EOF
40
EOF
RUN

NAME=64: Elf section header size
FILE=malloc://1024
CMDS=<<EOF
pfo elf64
pfs.elf_shdr
EOF
EXPECT=<<EOF
64
EOF
RUN

NAME=32: Multiple section headers
FILE=malloc://1024
CMDS=<<EOF
pfo elf32
pf [2]? (elf_shdr)sections
EOF
EXPECT=<<EOF
 sections : 
[
                struct<elf_shdr>
                name : 0x00000000 = 0x00000000
                type : 0x00000004 = type (enum elf_s_type) = 0x0 ; SHT_NULL
               flags : 0x00000008 = flags (enum elf_s_flags_32) = 0x0 ; SF32_None
                addr : 0x0000000c = 0x00000000
              offset : 0x00000010 = 0x00000000
                size : 0x00000014 = 0x00000000
                link : 0x00000018 = 0x00000000
                info : 0x0000001c = 0x00000000
           addralign : 0x00000020 = 0x00000000
             entsize : 0x00000024 = 0x00000000

                struct<elf_shdr>
                name : 0x00000028 = 0x00000000
                type : 0x0000002c = type (enum elf_s_type) = 0x0 ; SHT_NULL
               flags : 0x00000030 = flags (enum elf_s_flags_32) = 0x0 ; SF32_None
                addr : 0x00000034 = 0x00000000
              offset : 0x00000038 = 0x00000000
                size : 0x0000003c = 0x00000000
                link : 0x00000040 = 0x00000000
                info : 0x00000044 = 0x00000000
           addralign : 0x00000048 = 0x00000000
             entsize : 0x0000004c = 0x00000000
]
EOF
RUN

NAME=64: Multiple section headers
FILE=malloc://1024
CMDS=<<EOF
pfo elf64
pf [2]? (elf_shdr)sections
EOF
EXPECT=<<EOF
 sections : 
[
                struct<elf_shdr>
                name : 0x00000000 = 0x00000000
                type : 0x00000004 = type (enum elf_s_type) = 0x0 ; SHT_NULL
               flags : 0x00000008 = flags (enum elf_s_flags_64) = 0x0 ; SF64_None
                addr : 0x00000010 = (qword)0x0000000000000000
              offset : 0x00000018 = (qword)0x0000000000000000
                size : 0x00000020 = (qword)0x0000000000000000
                link : 0x00000028 = 0x00000000
                info : 0x0000002c = 0x00000000
           addralign : 0x00000030 = (qword)0x0000000000000000
             entsize : 0x00000038 = (qword)0x0000000000000000

                struct<elf_shdr>
                name : 0x00000040 = 0x00000000
                type : 0x00000044 = type (enum elf_s_type) = 0x0 ; SHT_NULL
               flags : 0x00000048 = flags (enum elf_s_flags_64) = 0x0 ; SF64_None
                addr : 0x00000050 = (qword)0x0000000000000000
              offset : 0x00000058 = (qword)0x0000000000000000
                size : 0x00000060 = (qword)0x0000000000000000
                link : 0x00000068 = 0x00000000
                info : 0x0000006c = 0x00000000
           addralign : 0x00000070 = (qword)0x0000000000000000
             entsize : 0x00000078 = (qword)0x0000000000000000
]
EOF
RUN

NAME=Issue 5640 pfj produce wrong json
FILE=bins/elf/analysis/main
ARGS=-nn
CMDS=pfj {1}? (elf_phdr) @ 0x0000000000000040
EXPECT=<<EOF
[{"name":"","type":"elf_phdr","offset":64,"value":[{"name":"type","type":"E","offset":64,"value":6,"label":"PT_PHDR","enum":"elf_p_type"},{"name":"flags","type":"E","offset":68,"value":5,"label":"PF_Read_Exec","enum":"elf_p_flags"},{"name":"offset","type":"q","offset":72,"value":64},{"name":"vaddr","type":"q","offset":80,"value":4194368},{"name":"paddr","type":"q","offset":88,"value":4194368},{"name":"filesz","type":"q","offset":96,"value":448},{"name":"memsz","type":"q","offset":104,"value":448},{"name":"align","type":"q","offset":112,"value":8}]}]
EOF
RUN

NAME=32: Elf header
FILE=bins/elf/analysis/tiny1
CMDS=<<EOF
pfo elf32
pf.elf_header @ 0x8048000
EOF
EXPECT=<<EOF
     ident : 
                struct<elf_ident>
           magic : 0x08048000 = "\x7fELF"
           class : 0x08048004 = class (enum elf_class) = 0x1 ; ELFCLASS32
            data : 0x08048005 = data (enum elf_data) = 0x1 ; ELFDATA2LSB
         version : 0x08048006 = version (enum elf_hdr_version) = 0x1 ; EV_CURRENT
      type : 0x08048010 = type (enum elf_type) = 0x2 ; ET_EXEC
   machine : 0x08048012 = machine (enum elf_machine) = 0x3 ; EM_386
   version : 0x08048014 = version (enum elf_obj_version) = 0x1 ; EV_CURRENT
     entry : 0x08048018 = 0x08048054
     phoff : 0x0804801c = 0x00000034
     shoff : 0x08048020 = 0x00000000
     flags : 0x08048024 = 0x00000000
    ehsize : 0x08048028 = 52
 phentsize : 0x0804802a = 32
     phnum : 0x0804802c = 1
 shentsize : 0x0804802e = 0
     shnum : 0x08048030 = 0
  shstrndx : 0x08048032 = 0
EOF
RUN

NAME=64: Elf header
FILE=bins/elf/analysis/x64-simple
CMDS=<<EOF
pfo elf64
pf.elf_header @ `i~baddr[1]`
EOF
EXPECT=<<EOF
     ident : 
                struct<elf_ident>
           magic : 0x00400000 = "\x7fELF"
           class : 0x00400004 = class (enum elf_class) = 0x2 ; ELFCLASS64
            data : 0x00400005 = data (enum elf_data) = 0x1 ; ELFDATA2LSB
         version : 0x00400006 = version (enum elf_hdr_version) = 0x1 ; EV_CURRENT
      type : 0x00400010 = type (enum elf_type) = 0x2 ; ET_EXEC
   machine : 0x00400012 = machine (enum elf_machine) = 0x3e ; EM_X86_64
   version : 0x00400014 = version (enum elf_obj_version) = 0x1 ; EV_CURRENT
     entry : 0x00400018 = (qword)0x0000000000400080
     phoff : 0x00400020 = (qword)0x0000000000000040
     shoff : 0x00400028 = (qword)0x00000000000000a8
     flags : 0x00400030 = 0x00000000
    ehsize : 0x00400034 = 64
 phentsize : 0x00400036 = 56
     phnum : 0x00400038 = 1
 shentsize : 0x0040003a = 64
     shnum : 0x0040003c = 3
  shstrndx : 0x0040003e = 2
EOF
RUN

NAME=32: -nn elf header
FILE=bins/elf/analysis/tiny1
ARGS=-nn
CMDS=<<EOF
e io.va=0
pf.elf_header @ elf_header
?e
pfj.elf_header @ elf_header ~{}
EOF
EXPECT=<<EOF
     ident : 
                struct<elf_ident>
           magic : 0x00000000 = "\x7fELF"
           class : 0x00000004 = class (enum elf_class) = 0x1 ; ELFCLASS32
            data : 0x00000005 = data (enum elf_data) = 0x1 ; ELFDATA2LSB
         version : 0x00000006 = version (enum elf_hdr_version) = 0x1 ; EV_CURRENT
      type : 0x00000010 = type (enum elf_type) = 0x2 ; ET_EXEC
   machine : 0x00000012 = machine (enum elf_machine) = 0x3 ; EM_386
   version : 0x00000014 = version (enum elf_obj_version) = 0x1 ; EV_CURRENT
     entry : 0x00000018 = 0x08048054
     phoff : 0x0000001c = 0x00000034
     shoff : 0x00000020 = 0x00000000
     flags : 0x00000024 = 0x00000000
    ehsize : 0x00000028 = 52
 phentsize : 0x0000002a = 32
     phnum : 0x0000002c = 1
 shentsize : 0x0000002e = 0
     shnum : 0x00000030 = 0
  shstrndx : 0x00000032 = 0

[
  {
    "name": "ident",
    "type": "elf_ident",
    "offset": 0,
    "value": [
      {
        "name": "magic",
        "type": "z",
        "offset": 0,
        "value": "\u007fELF"
      },
      {
        "name": "class",
        "type": "E",
        "offset": 4,
        "value": 1,
        "label": "ELFCLASS32",
        "enum": "elf_class"
      },
      {
        "name": "data",
        "type": "E",
        "offset": 5,
        "value": 1,
        "label": "ELFDATA2LSB",
        "enum": "elf_data"
      },
      {
        "name": "version",
        "type": "E",
        "offset": 6,
        "value": 1,
        "label": "EV_CURRENT",
        "enum": "elf_hdr_version"
      }
    ]
  },
  {
    "name": "type",
    "type": "E",
    "offset": 16,
    "value": 2,
    "label": "ET_EXEC",
    "enum": "elf_type"
  },
  {
    "name": "machine",
    "type": "E",
    "offset": 18,
    "value": 3,
    "label": "EM_386",
    "enum": "elf_machine"
  },
  {
    "name": "version",
    "type": "E",
    "offset": 20,
    "value": 1,
    "label": "EV_CURRENT",
    "enum": "elf_obj_version"
  },
  {
    "name": "entry",
    "type": "x",
    "offset": 24,
    "value": 134512724
  },
  {
    "name": "phoff",
    "type": "x",
    "offset": 28,
    "value": 52
  },
  {
    "name": "shoff",
    "type": "x",
    "offset": 32,
    "value": 0
  },
  {
    "name": "flags",
    "type": "x",
    "offset": 36,
    "value": 0
  },
  {
    "name": "ehsize",
    "type": "N2",
    "offset": 40,
    "value": 52
  },
  {
    "name": "phentsize",
    "type": "N2",
    "offset": 42,
    "value": 32
  },
  {
    "name": "phnum",
    "type": "N2",
    "offset": 44,
    "value": 1
  },
  {
    "name": "shentsize",
    "type": "N2",
    "offset": 46,
    "value": 0
  },
  {
    "name": "shnum",
    "type": "N2",
    "offset": 48,
    "value": 0
  },
  {
    "name": "shstrndx",
    "type": "N2",
    "offset": 50,
    "value": 0
  }
]
EOF
RUN

NAME=64: -nn elf header
FILE=bins/elf/analysis/x64-simple
ARGS=-nn
CMDS=<<EOF
e io.va=0
pf.elf_header @ elf_header
EOF
EXPECT=<<EOF
     ident : 
                struct<elf_ident>
           magic : 0x00000000 = "\x7fELF"
           class : 0x00000004 = class (enum elf_class) = 0x2 ; ELFCLASS64
            data : 0x00000005 = data (enum elf_data) = 0x1 ; ELFDATA2LSB
         version : 0x00000006 = version (enum elf_hdr_version) = 0x1 ; EV_CURRENT
      type : 0x00000010 = type (enum elf_type) = 0x2 ; ET_EXEC
   machine : 0x00000012 = machine (enum elf_machine) = 0x3e ; EM_X86_64
   version : 0x00000014 = version (enum elf_obj_version) = 0x1 ; EV_CURRENT
     entry : 0x00000018 = (qword)0x0000000000400080
     phoff : 0x00000020 = (qword)0x0000000000000040
     shoff : 0x00000028 = (qword)0x00000000000000a8
     flags : 0x00000030 = 0x00000000
    ehsize : 0x00000034 = 64
 phentsize : 0x00000036 = 56
     phnum : 0x00000038 = 1
 shentsize : 0x0000003a = 64
     shnum : 0x0000003c = 3
  shstrndx : 0x0000003e = 2
EOF
RUN

NAME=pf elf_header times fix
FILE=-
CMDS=<<EOF
pfo elf32
pf? elf_header
pfs.elf_header
?e
wz `pf? elf_header`
(pfelf num; pf.elf_header {$0}`psz`; pf? elf_header; pfs.elf_header)
.(pfelf 1)
?e
.(pfelf 2)
EOF
EXPECT=<<EOF
?[2]E[2]E[4]ExxxxN2N2N2N2N2N2 (elf_ident)ident (elf_type)type (elf_machine)machine (elf_obj_version)version entry phoff shoff flags ehsize phentsize phnum shentsize shnum shstrndx
52

{1}?[2]E[2]E[4]ExxxxN2N2N2N2N2N2 (elf_ident)ident (elf_type)type (elf_machine)machine (elf_obj_version)version entry phoff shoff flags ehsize phentsize phnum shentsize shnum shstrndx
52

{2}?[2]E[2]E[4]ExxxxN2N2N2N2N2N2 (elf_ident)ident (elf_type)type (elf_machine)machine (elf_obj_version)version entry phoff shoff flags ehsize phentsize phnum shentsize shnum shstrndx
104
EOF
RUN

NAME=32: normal elf header
FILE=bins/elf/hello_world32
CMDS=<<EOF
pfo elf32
pf.elf_header @ segment.ehdr
EOF
EXPECT=<<EOF
     ident : 
                struct<elf_ident>
           magic : 0x00000000 = "\x7fELF"
           class : 0x00000004 = class (enum elf_class) = 0x1 ; ELFCLASS32
            data : 0x00000005 = data (enum elf_data) = 0x1 ; ELFDATA2LSB
         version : 0x00000006 = version (enum elf_hdr_version) = 0x1 ; EV_CURRENT
      type : 0x00000010 = type (enum elf_type) = 0x3 ; ET_DYN
   machine : 0x00000012 = machine (enum elf_machine) = 0x3 ; EM_386
   version : 0x00000014 = version (enum elf_obj_version) = 0x1 ; EV_CURRENT
     entry : 0x00000018 = 0x000004d0
     phoff : 0x0000001c = 0x00000034
     shoff : 0x00000020 = 0x0000182c
     flags : 0x00000024 = 0x00000000
    ehsize : 0x00000028 = 52
 phentsize : 0x0000002a = 32
     phnum : 0x0000002c = 9
 shentsize : 0x0000002e = 40
     shnum : 0x00000030 = 29
  shstrndx : 0x00000032 = 28
EOF
RUN

NAME=64: normal elf header
FILE=bins/elf/hello_world
CMDS=<<EOF
pfo elf64
pf.elf_header @ segment.ehdr
EOF
EXPECT=<<EOF
     ident : 
                struct<elf_ident>
           magic : 0x00000000 = "\x7fELF"
           class : 0x00000004 = class (enum elf_class) = 0x2 ; ELFCLASS64
            data : 0x00000005 = data (enum elf_data) = 0x1 ; ELFDATA2LSB
         version : 0x00000006 = version (enum elf_hdr_version) = 0x1 ; EV_CURRENT
      type : 0x00000010 = type (enum elf_type) = 0x3 ; ET_DYN
   machine : 0x00000012 = machine (enum elf_machine) = 0x3e ; EM_X86_64
   version : 0x00000014 = version (enum elf_obj_version) = 0x1 ; EV_CURRENT
     entry : 0x00000018 = (qword)0x00000000000006a0
     phoff : 0x00000020 = (qword)0x0000000000000040
     shoff : 0x00000028 = (qword)0x0000000000001a08
     flags : 0x00000030 = 0x00000000
    ehsize : 0x00000034 = 64
 phentsize : 0x00000036 = 56
     phnum : 0x00000038 = 9
 shentsize : 0x0000003a = 64
     shnum : 0x0000003c = 29
  shstrndx : 0x0000003e = 28
EOF
RUN
