Yardanico/cosmonim

Exec format error

m4z-0xf00 opened this issue · 2 comments

Hello, I wrote a little code to hexdump files in colors, but the produced executable seems not to be valid:

OSError: [Errno 8] Exec format error: './chex.com'

import terminal
import os
import strutils

proc reset() {.noconv.} =
  stdout.write("\u001b[0m\n")
  quit()

setControlCHook(reset)

let cols = terminalWidth()

var
  f: File
  i:int = 0
  l: array[1024, uint8]
  mono = false
  r: int

if isatty(stdin):
  case paramCount():
  of 0:
    echo "filename expected"
    quit()
  of 1:
    f = open(paramStr(1), fmRead)
  else:
    mono = true
    f = open(paramStr(2), fmRead)
else:
  f = stdin

r = f.readBytes(l,0,cols*2)
while r>0:
  if mono:
    for j in 0..cols*2-1:
      if j >= r: break
      stdout.write("\u001b[38;5;" & $l[j] & "m█")
      i += 1
      if (i mod cols) == 0:
        stdout.write("\n")
  else:
    for j in 0..cols-1:
      if j >= r:
        stdout.write("\u001b[0m" & " ".repeat(cols-j)) # for urxvt bug
        break
      var bg = "\u001b[48;5;" & $l[cols+j] & "m"
      if r <= cols:
        bg = "\u001b[49m"
      stdout.write( bg & "\u001b[38;5;" & $l[j] & "m▀")
      i += 1
      if (i mod cols) == 0:
        stdout.write("\n")

  r = f.readBytes(l,0,cols*2)

reset()

Hi, sorry, I missed your issue. Did you follow the instructions in the README? Specifically this part:

objcopy -SO binary hello.elf hello.com

This is needed to extract the actual binary from the compiled artifact.
After that you execute ./hello.elf, not ./hello.com, just bear in mind that hello.elf will stop being a portable executable after a single execution on Linux since it modifies itself.

Closing for now, if the comment above didn't solve your issue, please comment and we'll figure it out :)