Files
reverse-engineering-anything/docs/guides/native/native-toolchain.md
T
Eliran Zachandmorluto 73582aa342 feat(native): identify Nim, Zig, GHC, OCaml and D toolchains (#16252)
* feat(native): identify Nim, Zig, GHC, OCaml and D toolchains

Add identify_native_toolchain, a static, read-only reader that reports
whether a local ELF, PE or thin or universal Mach-O image was built by
Nim, Zig, Haskell (GHC), OCaml or D, including stripped images without
a DWARF producer.

The reader searches a bounded part of the selected image for a fixed
table of runtime symbol names, runtime messages, mangled names, source
paths and producer strings, and returns each observed marker with its
file offset, section or segment, matched text and occurrence count.
Data sections are searched before bytes outside sections and before
instruction sections, so a bounded scan reaches strings first;
coverage reports the bytes examined.

The verdict is derived from the listed markers only: a language needs
two distinct markers, one strong. Conflicting, too few or no markers
give unknown, and a version is reported only when a marker states it.

* perf(native): order toolchain scan ranges in linear time

Intersect instruction sections with the bytes outside data sections in one pass, so an image with tens of thousands of sections does not take quadratic time before the scan starts.

* fix(native): bound stated toolchain versions and describe scan order

Track distinct versions per marker in a Set and list at most eight,
counting the rest in versions_omitted, so an image with many version
strings costs constant time per match and a bounded result. A candidate
reports a version only when exactly one was stated.

Describe max_scan_bytes and examined_bytes as a total in scan order
rather than a prefix from the image start, and place the chunk-boundary
test's marker across an actual read boundary.

* fix(native): keep every stated toolchain version in the report

* fix(native): bound stated toolchain version bytes before keeping them

* feat(native): make the stated version budget a per-call control

* fix(native): give no larger-limit retry once a toolchain control is at its maximum

* fix(native): do not match toolchain markers on truncated runs

* fix(native): keep exact markers on a final run at the image end

* style(native): format the toolchain changes

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-12-04 12:09:21 -05:00

6.3 KiB

Native toolchain identification

identify_native_toolchain / identify-native-toolchain reports whether one local ELF, PE, or Mach-O image was built by Nim, Zig, Haskell (GHC), OCaml, or D. It works on stripped images that carry no DWARF producer. It does not execute the image and does not need the compiler.

rea identify-native-toolchain /absolute/path/to/program --json

The CLI resolves relative paths against the caller's working directory. MCP requires an absolute filesystem path:

{
  "name": "identify_native_toolchain",
  "arguments": { "path": "/artifacts/program" }
}

A universal Mach-O needs architecture, as for DWARF compile units. Offsets are always in the original file.

Go and Rust have their own readers: Go binaries and Rust binaries. When the image keeps DWARF, its producer and language attributes come from DWARF compile units.

Observations

The reader searches printable ASCII runs in the image bytes for a fixed list of markers. Symbol and import/export names are found where they are stored as strings, such as .dynstr, .strtab, or Mach-O __LINKEDIT; symbol tables are not decoded. Each observed marker is returned with:

  • id, language, and description;
  • kind: runtime_symbol, mangled_symbol, runtime_string, source_path, or version_string;
  • strength: strong for names and text the runtime defines and other toolchains are not known to emit, weak for text that only supports a strong marker;
  • occurrences, and up to four locations with file offset, containing section (or Mach-O segment outside every section), and the matched text;
  • versions: every distinct version stated by a version-bearing marker.

Symbol markers match with or without one leading underscore, so Mach-O _NimMain and __Dmain count. One string counts as at most one marker per language, preferring a strong one.

Toolchain Strong markers Weak markers
Nim NimMain, NimMainModule, nimGC_*, the nil-access SIGSEGV message Error: unhandled exception: text, system.nim / fatal.nim / excpt.nim / chcks.nim paths
Zig std.builtin.* and start.* entry symbols, attempt to unwrap error, integer cast truncated bits, a zig X.Y.Z producer other std.* symbols, reached unreachable code, attempt to use null value
Haskell (GHC) hs_init, hs_main, z-encoded ghczmprim_GHCzi* / ghczminternal_GHCzi* / base_GHCzi* symbols, GHCRTS, a Glasgow Haskell Compilation System producer stg_* symbols, +RTS text
OCaml caml_main, caml_startup, caml_start_program, camlStdlib* symbols, OCAMLRUNPARAM, the The OCaml runtime, version banner other caml_* symbols, Fatal error: exception text
D _d_run_main, _Dmain, D-mangled core.* and std.* symbols, --DRT- options, core.exception.*Error names, an LDC, DMD (Digital Mars D), or GDC (GNU D) producer other D-mangled symbols, druntime/Phobos .d source paths

Verdict

candidates counts distinct strong and weak markers per language. A language is supported by two or more distinct markers, at least one strong. The verdict is:

  • identified, with confidence high (two or more strong markers) or medium (one strong and at least one more), when exactly one language is supported;
  • unknown with unknown_reason conflicting_markers when more than one is supported, insufficient_markers when markers were seen but none is supported, or no_markers when none was seen.

version is set only when the chosen language's version markers state exactly one value. GHC and the OCaml native runtime do not store such a string in ordinary executables, so their version is usually null.

A marker shows that its text is stored in the image, not that the code using it runs. A program that links a runtime as a library can carry its markers. Missing markers do not establish that the image was built by another toolchain.

Coverage and limits

max_scan_bytes (default 64 MiB, at most 512 MiB) bounds the total image bytes searched. Sections that hold no instructions are searched first, then bytes outside every section (headers and Mach-O __LINKEDIT), then instruction sections. The budget is counted in that order, so it is not a file-offset window: a small budget can find markers far into the file while earlier code stays unread. coverage reports examined_bytes, image_bytes, strings_examined, and strings_truncated (runs longer than the 4096 bytes matched). It is partial, with a reason, when the budget stopped the search; a verdict from partial coverage is based on the bytes examined. max_version_bytes (default 64 KiB, at most 16 MiB) bounds the JSON bytes of the versions of all markers together. A compiler states one version, so only an image with many distinct version strings reaches it: the search then stops before the string that would pass the limit, keeping every version it reports. coverage.stopped_by names the control that stopped a partial search, max_scan_bytes or max_version_bytes, and the reason gives the stopping file offset and a retry with a larger value, or says that the search cannot finish when the control is already at its maximum. max_file_bytes (default and maximum 512 MiB) refuses a larger file.

UTF-16 text is not searched, and Go and Rust markers are not listed here.