| 1 | (* Views: reads the ELF header of the program itself, and its program |
| 2 | headers, which say how the system loads it into memory. The file is |
| 3 | read whole into bytes; each header is a record laid over them with a |
| 4 | view, b[i].(T), which the program checks lies inside the bytes. *) |
| 5 | module Elf; |
| 6 | import Out, Fmt, Buf, Heap, Files; |
| 7 | |
| 8 | type |
| 9 | (* the file's first 64 bytes, as the ELF specification lays them |
| 10 | out: a C struct, so a record lays them out the same way *) |
| 11 | Header = record |
| 12 | ident: [16]U8; (* 7FH 'E' 'L' 'F', the class, the byte order, ... *) |
| 13 | kind: U16; (* 2, an executable; 3, a shared object *) |
| 14 | machine: U16; (* 62, x86-64 *) |
| 15 | version: U32; |
| 16 | entry: U64; (* where the program starts *) |
| 17 | phoff: U64; (* where the program headers are in the file *) |
| 18 | shoff: U64; |
| 19 | flags: U32; |
| 20 | ehsize: U16; (* this header's size *) |
| 21 | phentsize: U16; (* a program header's size *) |
| 22 | phnum: U16; (* how many there are *) |
| 23 | shentsize, shnum, shstrndx: U16; |
| 24 | end; |
| 25 | (* one piece of the file, loaded into memory or telling the loader |
| 26 | something *) |
| 27 | Segment = record |
| 28 | kind: U32; (* 1, loaded *) |
| 29 | flags: U32; (* 1, executable; 2, writable; 4, readable *) |
| 30 | offset, vaddr, paddr, filesz, memsz, align: U64; |
| 31 | end; |
| 32 | |
| 33 | (* "r-x" and the like, for flags *) |
| 34 | proc rights(flags: U32): [3]U8; |
| 35 | var r: [3]U8; |
| 36 | begin |
| 37 | r := "---"; |
| 38 | if flags & 4 # 0 then r[0] := 'r'; end; |
| 39 | if flags & 2 # 0 then r[1] := 'w'; end; |
| 40 | if flags & 1 # 0 then r[2] := 'x'; end; |
| 41 | return r; |
| 42 | end; |
| 43 | |
| 44 | proc main*; |
| 45 | var b: Buf.Buf; f: []U8; h: Header; i, at, loaded, exec: INT; r: [3]U8; |
| 46 | begin |
| 47 | Buf.init(b, Heap.heap); |
| 48 | defer Buf.done(b); |
| 49 | if not Files.read("/proc/self/exe", b) then |
| 50 | Out.str("can't read the program\n"); |
| 51 | return; |
| 52 | end; |
| 53 | f := Buf.bytes(b); |
| 54 | |
| 55 | (* the header, copied out of the bytes whole *) |
| 56 | h := f[0].(Header); |
| 57 | if h.ident[0..4] # "\x7FELF" then Out.str("not ELF\n"); return; end; |
| 58 | Fmt.print("class {}, byte order {}, machine {}, header {} bytes\n", |
| 59 | [Fmt.int(I64(h.ident[4])), Fmt.int(I64(h.ident[5])), Fmt.int(I64(h.machine)), |
| 60 | Fmt.int(I64(h.ehsize))]); |
| 61 | if h.phentsize # SIZE(Segment) then Out.str("odd program headers\n"); return; end; |
| 62 | |
| 63 | (* each program header, read in place: no copy of the record *) |
| 64 | loaded := 0; |
| 65 | exec := 0; |
| 66 | for i := 0 to INT(h.phnum) - 1 do |
| 67 | at := INT(h.phoff) + i * SIZE(Segment); |
| 68 | if f[at].(Segment).kind = 1 then |
| 69 | INC(loaded); |
| 70 | r := rights(f[at].(Segment).flags); |
| 71 | if r = "r-x" then INC(exec); end; |
| 72 | end; |
| 73 | end; |
| 74 | Fmt.print("{} segments loaded, {} of them code\n", [Fmt.int(loaded), Fmt.int(exec)]); |
| 75 | end; |
| 76 | |
| 77 | end Elf. |