5 Header Compilation
This library provides facilities for extracting and compiling C header
information to generate architecture-specific binary layout information.
This is particularly useful for writing code that interacts with the PLT
Scheme foreign library—
Specifically, the foreign library’s pointer-manipulation procedures such as ptr-ref and ptr-set! can be used to read and write to arbitrary addresses, which can be computed using layout information.
(require c/header) | package: c-utils |
5.1 Headers
> (define example.h (make-header (include/reader "example.h" (make-program-reader)))) make-program-reader: undefined;
cannot reference undefined identifier
> (define time.h (header (struct tm ([int tm_sec] [int tm_min] [int tm_hour] [int tm_mday] [int tm_mon] [int tm_year] [int tm_wday] [int tm_yday] [int tm_isdst]))))
Using the Scribble @-reader, the latter example could be rewritten with embedded C syntax as:
(define time.h (make-header @program{ struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; }))
5.2 Compilation
procedure
(compile-header header compiler) → abi?
header : header? compiler : ((listof query?) -> (listof uint))
A header compiler must recognize the following types of queries.
struct
(struct query:sizeof (type) #:extra-constructor-name make-query:sizeof) type : any
struct
(struct query:offset (type field) #:extra-constructor-name make-query:offset) type : any field : symbol?
struct
(struct query:expr (expr) #:extra-constructor-name make-query:expr) expr : any
procedure
(system-compiler [ #:include<> include<> #:include include exe]) → ((listof query?) -> (listof uint)) include<> : (listof string?) = '() include : (listof string?) = '() exe : ((-> any) -> any) = gcc
5.3 Layouts
procedure
(primitive-layout? x) → boolean?
x : layout?
procedure
(ref-layout? x) → boolean?
x : layout?
procedure
(struct-layout? x) → boolean?
x : layout?
procedure
(union-layout? x) → boolean?
x : layout?
procedure
(array-layout? x) → boolean?
x : layout?
procedure
(pointer-layout? x) → boolean?
x : layout?
procedure
(enum-layout? x) → boolean?
x : layout?
procedure
(layout-size x) → uint
x : layout?
procedure
(layout-offset x path) → uint
x : (or/c struct-layout? union-layout? enum-layout?) path : (or/c symbol? (listof symbol?))
procedure
(struct-layout-lookup x name) → layout?
x : struct-layout? name : symbol?
procedure
(union-layout-lookup x name) → layout?
x : union-layout? name : symbol?
procedure
(deref-layout x) → layout?
x : layout?
5.4 Application Binary Interfaces (ABI’s)
An Application Binary Interface (ABI) provides information about the binary representation of C datatypes on a particular architecture.
struct
(struct abi (typedefs tags) #:extra-constructor-name make-abi) typedefs : (hasheqof symbol? layout?) tags : (hasheqof symbol? layout?)
As a convenience, ABI structures can be used as procedures, which is equivalent to calling abi-lookup with the ABI structure as the first argument.
procedure
(abi-lookup abi name) → layout?
abi : abi? name : symbol?
procedure
(abi-lookup-typedef abi name) → layout?
abi : abi? name : symbol?
procedure
(abi-lookup-tag abi name) → layout?
abi : abi? name : symbol?
procedure
(serialize-abi abi) → sexp?
abi : abi?
procedure
(deserialize-abi input) → abi?
input : sexp?
procedure
in : input-port? = (current-input-port)
procedure
abi : abi? out : output-port? = (current-output-port)