flowchart LR pkg["package.so<br/>(compiled binary)"] loader["dynamic linker<br/>(ld.so)"] symtab["R's exported symbols<br/>(libR.so or R.dll)"] exec["package code can<br/>call the C API"] pkg -->|load| loader loader -->|look up<br/>Rf_*, R_*| symtab symtab -->|fill in<br/>addresses| exec
7.1 What Is an ABI?
7.1.1 Motivation
When you write R code, the interpreter reads your text and runs it. When someone compiles a package with C code, the compiler translates that text to machine instructions and burns them into a binary file. An API is an agreement at the source level: if your code matches the documented interface, recompiling it connects it to the new version. An ABI is the same kind of promise at the binary level: if an old compiled package still runs without recompilation when you upgrade, the ABI stayed compatible.
R users expect to download prebuilt packages. Thousands of CRAN packages contain compiled C, C++, or Fortran code (5,569 of 25,474 had NeedsCompilation set to yes in tools::CRAN_package_db() on 2026-10-21), and CRAN distributes sources for Unix-alikes and binaries for Windows and macOS. So Windows and macOS users lean on binary compatibility, because they download pre-compiled binaries, while Linux users lean on source compatibility, because they usually compile on install. CRAN builds a separate set of those binaries for each R release series. The binary packages that do ship depend on R’s exported symbols and their calling conventions. Rename an entry point and the loader fails at load time, which is the safe outcome because it is loud. Change a signature and it does not fail: the old binary pushes the wrong arguments into a function that reads them as though they were right, and you get a crash somewhere else entirely, or quiet corruption.
7.1.2 Mental model: source, binary, and the gap between them
A function call in C compiles to instructions that load arguments into specific registers, jump to a specific address, and clean up the stack afterward. A compiled package carries those instructions and none of the source they came from.
Consider a simple R C function:
SEXP add(SEXP a, SEXP b) {
return ScalarInteger(INTEGER(a)[0] + INTEGER(b)[0]);
}This text is human-readable. The compiled version is a machine-code routine at a specific address, and what it contains for INTEGER(a) is a call instruction, not arithmetic on a. The header declares INTEGER as a function on purpose, so the work of finding the payload happens inside R instead of inside the package. What the binary freezes is the call: the symbol’s name, the argument it passes, and the register convention that carries it.
An ABI specifies:
- Symbol names: the exact spelling by which the linker finds functions.
- Calling conventions: which registers carry arguments, which hold return values, who cleans the stack.
- Type sizes and alignment: how wide is an int, where does a double start in memory.
- Struct layouts, for any struct whose definition the header publishes. R publishes very few, and
SEXPRECis not among them.
Source-level compatibility means recompilation works. Binary-level compatibility means compiled code still works.
7.1.3 The SEXP as a de facto ABI constraint
R’s core data type is the SEXP. Every R object in C is a pointer to a SEXPREC structure. The structure is opaque to packages. The public header Rinternals.h provides only a typedef:
typedef struct SEXPREC *SEXP;The struct definition itself is not published. Instead, Rinternals.h provides accessor functions that extract fields:
int (TYPEOF)(SEXP x);
int (LENGTH)(SEXP x);
double *(REAL)(SEXP x);
int *(INTEGER)(SEXP x);
SEXP (VECTOR_ELT)(SEXP x, R_xlen_t i);The parentheses around function names are deliberate. R Core documents it in a comment: “Accessor functions. Many are declared using () to avoid the macro definitions in the internal headers.” This means INTEGER(x) compiles to a function call, not a direct memory offset. R’s maintainers can rearrange fields inside SEXPREC, widen sxpinfo, or add new fields, and a conforming package needs no change to its source. The package never knew where the fields were.
This opacity is the mechanism that lets R evolve its object representation. What shapes the ABI is the surface: the published accessor functions, their signatures, the calling conventions, and the symbols exported.
CRAN packages depend on R’s exported symbol surface and the signatures of accessor functions. The constraint lives in the C API, not in struct field offsets. R’s maintainers can change the internal layout of SEXPREC as long as the accessor functions and their calling conventions stay the same. The exception is a package that reaches past the API, by defining USE_RINTERNALS before R 4.2.0 or by copying R’s internal definitions. Such a package takes a direct dependence on the layout and does break.
7.1.4 Linking packages to the interpreter
A typical native R package includes R headers, compiles C code against those headers, and produces a .so or .dll file. At load time, R calls dyn.load() or library(). The dynamic linker reads the binary and resolves symbols against R’s runtime.
Here is the loading path:
Package loading: the linker resolves symbol names to function addresses in the running R process.
The compiled code calls functions like Rf_allocVector, Rf_protect, and Rf_eval. These are published entry points in R’s C API. The dynamic linker finds them by name in the running R process and writes their addresses into a table that the package’s calls go through (the global offset table on Linux, the import address table on Windows). If R changes the name of a function, the linker fails at load time. That is safer than silent corruption. Names in the documented API rarely change: “Writing R Extensions” says the entry points it documents “will only be changed with considerable notice”.
GNU R publishes a C API documented in “Writing R Extensions”. R 4.6.1 on Windows exports 1,413 symbols from R.dll. “Writing R Extensions” documents a much smaller subset as the API that packages may use. The rest are exported because the Windows build exports every global symbol in R’s object files except those on a hide list (src/gnuwin32/Rdll.hide), not because they are promised to anyone. Packages link dynamically at load time. The linker resolves all symbol references, or loading fails and the package is unavailable.
7.1.5 Changes that break binary compatibility
The ABI constraint lives in the exported surface, not in struct layout. These are the changes that break a compiled package:
A removed or renamed exported symbol. When you load a compiled package, the linker searches R’s symbol table for function names. If a function disappears or changes name, the loader fails at load time. This is the safe failure mode.
A changed function signature or calling convention. A package compiles against an old header that declares Rf_allocVector(SEXPTYPE type, R_xlen_t length). If R changes the signature to Rf_allocVector(SEXPTYPE type, R_xlen_t length, int flags), the old binary still sets up two arguments. The new function reads its third from a register the old binary never set (rdx on x86-64 Linux), so flags holds whatever was left there, and the call misbehaves or crashes.
A changed public constant or typedef. Headers define constants like REALSXP (a #define for 14 in Rinternals.h) and types like R_xlen_t. Packages compile these constants into binaries. If you change REALSXP to 15 or redefine R_xlen_t as 32-bit instead of 64-bit, old binaries use the wrong values. Mismatched type codes break dispatch. Wrong sizes break array indexing.
Changed documented behavior behind an unchanged signature. A function can change how it computes results. Scripts see different output. Packages that depend on the old behavior break silently.
The serialization format for .rds files. Packages read and write R objects to disk. If the format changes, older versions of R cannot read files written in the new one. R handles this by versioning the format: R 3.5.0 added version 3, kept writing version 2 by default until R 3.6.0, and current R still reads both. This couples the C runtime to on-disk formats that last decades.
Packages that reach past the API. Before R 4.2.0, a package could define USE_RINTERNALS and get macros that read SEXPREC fields directly. Since 4.2.0 that macro has no effect on package code, but a package can still copy R’s internal definitions. Either way it couples itself to the layout and breaks when the layout changes.
The same binary reaches the same symbol whatever layout sits behind it. Only code that reads the struct directly, instead of calling accessors, sees a field move.
The packages this does not protect are the ones that opted out, through USE_RINTERNALS before R 4.2.0 or a copy of R’s internal headers. They took a direct dependence on the layout that the accessors were hiding, so a change like this breaks their source as well as their binary.
R did change sxpinfo within living memory. R 3.5.0 widened it from 32 to 64 bits, to make room for ALTREP and a 16-bit named field, and the NEWS for that release says the change “requires re-installing packages that use compiled code”. R enforces such rebuilds itself. Each installed package records R’s internals ID, and loadNamespace() refuses a compiled package whose ID differs from the running R’s, saying it “was installed by an R version with different internals”. So GNU R does not promise that an old binary survives a layout change. What the opaque accessor surface bought is that a package using only the API needed a rebuild, not a rewrite. R’s compatibility burden is real and large, and it sits on the exported surface, not on struct layout.
7.1.6 Calling conventions and symbol resolution
An ABI also specifies how functions receive arguments and return results. This is the calling convention. On x86-64 Linux (System V ABI), integer arguments pass in registers: rdi, rsi, rdx, rcx, r8, r9. Additional arguments go on the stack. Return values come back in rax (and rdx:rax for 128-bit results).
R’s C functions follow this convention. When a package calls Rf_allocVector(SEXPTYPE type, R_xlen_t length), the compiled binary loads type into rdi and length into rsi. It then issues a call instruction to the address of Rf_allocVector. The linker filled in that address when the package loaded. If the calling convention changes, the binary still passes arguments the old way, but R’s code expects them in different registers. Data corruption and crashes follow.
In practice, calling conventions are stable, and R does not choose its own. The C compiler follows the platform’s ABI (System V on x86-64 Linux and macOS, Microsoft’s x64 convention on Windows), and every other C library on the system depends on the same one.
Symbol names are part of the ABI too. The linker finds Rf_allocVector by that exact string. If R’s maintainers rename it to R_allocVector, old binaries still ask the linker for Rf_allocVector. The linker fails. The package does not load. This is a hard break, which is why R keeps its names consistent.
7.1.7 The graveyard of R reimplementations
Five other R implementations set out to replicate or improve on GNU R: Renjin, FastR, Rho, pqR and Riposte. Renjin and FastR, which did not start from GNU R’s code, found native packages harder than R code.
For an R implementation that is not GNU R, the hardest barrier is the exported C API surface that compiled CRAN packages are built against: the symbol names, the signatures, and the behavior behind them. Meeting it means building a large compatibility layer or giving up native packages, and both paths are expensive.
Running pure R code is the part these projects did well. A CRAN package with compiled code expects more: the symbol names GNU R exports, the signatures behind them, and the behavior of each call. A new implementation has to reproduce all of that, which is expensive and error-prone, or turn native packages away, which makes it unsuitable for most R users.
Renjin, an interpreter on the Java virtual machine, compiles a package’s C and Fortran code to JVM bytecode with its own GCC-based compiler, so native packages have to be rebuilt for it. FastR, on GraalVM, implements much of the C API itself and pays for each crossing between native and managed code. Both shipped and found users, and FastR is now in maintenance mode. pqR and Rho kept GNU R’s own C code. pqR is a fork of R 2.15.0 and fell behind as packages began to require newer versions of R. Rho, a C++ refactor of GNU R, did not support packages that define USE_RINTERNALS and was suspended in 2017. Riposte was a research virtual machine for vector code that implemented a subset of R and no C API.
7.1.8 The case for forking GNU R
The threaded-r project is a fork of GNU R 4.6.1, not a rewrite. The choice comes from the need to keep R’s C API.
threaded-r runs several interpreters in one process, which means moving global mutable state into per-interpreter structures. The effort is large: the census found 1,574 mutable globals in upstream R. The documented API survives it. Function names, signatures and calling conventions stay as they are, so a CRAN package that sticks to the documented API compiles against threaded-r unchanged.
Binary compatibility with stock R does not survive, and threaded-r gives it up on purpose. Some exported variables, such as R_GlobalEnv, now belong to each interpreter, so threaded-r’s headers reach them through an accessor function and the old data symbol is gone. A stock-R binary that uses one would fail to load. To refuse every stock-R binary up front, threaded-r carries its own R_INTERNALS_UUID, and loadNamespace() refuses a compiled package built for stock R and asks for a rebuild.
A rewrite has to implement that interface instead of inheriting it. rrs is written from scratch, with no global mutable state in its interpreter and no SEXPREC, and most of the thirty most downloaded compiled CRAN packages still build against its headers and load (28 of 30 in its census of 2026-08-18), because what a package’s code calls is a set of accessor functions. rrs and threaded-r solve different problems, and both depend on the same fact about opacity.
7.1.9 Rust FFI and layout guarantees
Rust cannot change C’s calling conventions, but it can match them. By default it matches neither C’s struct layout nor C’s calling convention.
The Rust Reference <https://doc.rust-lang.org/reference/type-layout.html#the-rust-representation> promises little about the default layout beyond alignment and fields that do not overlap: “There are no other guarantees of data layout made by this representation.” The compiler may reorder fields to reduce padding, which saves memory, and a Rust struct written to mirror a C struct can then hold its fields in a different order.
To guarantee C layout, Rust provides the #[repr(C)] attribute:
#[repr(C)]
pub struct MyStruct {
pub field_a: i32,
pub field_b: f64,
pub field_c: *mut u8,
}The #[repr(C)] attribute tells the compiler to use C’s layout rules: fields stay in declaration order and padding follows C, so the struct matches its C counterpart.
FFI functions must also be declared with extern "C":
extern "C" {
pub fn some_c_function(arg: i32) -> i32;
}The extern "C" block tells the compiler that these functions follow the C calling convention and go by their plain C symbol names, with no Rust name mangling, so calls to them and pointers to them work with C code.
Rust needs the annotation because its own calling convention is unspecified. The Rust Reference <https://doc.rust-lang.org/reference/items/external-blocks.html#abi> says so in one line: “The Rust ABI offers no stability guarantees.” Code that calls C, or is called from C, has to opt in to the C convention, which the Reference defines as the one that “matches the default ABI chosen by the dominant C compiler for the target”.
7.1.10 Compatibility is a spectrum
Compatibility comes in levels, each with different costs.
Source compatibility: the code compiles without change. This is the cheapest. You just need the same API. Recompilation is fast if source is available.
Recompilation-required compatibility: the API is stable, but internal changes require recompilation. Package maintainers recompile against the new version, and CRAN’s build machines do it for the binaries they ship, which is why CRAN keeps a separate set of Windows and macOS binaries for each R release series.
Binary compatibility: compiled packages run without change. This is the most expensive. You must freeze the exported surface: symbol names, signatures, calling conventions, public constants and type sizes. You cannot improve performance if it changes a signature. You cannot simplify the API if it changes what the linker sees.
R’s production constraint is compatibility with thousands of compiled CRAN packages. This constraint makes incremental improvement within the GNU R codebase cheaper than reimplementation. It also constrains any R implementation aimed at real-world use. There are three ways to meet it. Answer the same exported surface with your own implementation behind it. Put a translation layer in front of a foreign representation, which costs on every crossing. Or accept incompatibility with native packages and lose CRAN.
The r-in-rust project took the first route, which only the opacity makes available. rrs exports R’s symbols with R’s signatures, ships headers in R’s function-accessor style, and implements them over its own representation. Packages compile from their own source against those headers. There is no layout being translated, because a conforming package never depended on one. What it costs is a function call where R itself might have inlined a macro internally, and that price is paid at the boundary, not on every operation.
7.1.11 Relevance to my work
Both threaded-r and r-in-rust must contend with the ABI constraint, but in different ways.
threaded-r keeps the API and gives up the binary. The project moves global state into per-interpreter structures behind an unchanged accessor surface, so a package that sticks to the documented API compiles against it as it is and calls the same functions with the same signatures. A package built against stock R is refused at load and has to be rebuilt. The project pays in engineering complexity (tracking globals, refactoring state management) and in that one rebuild.
r-in-rust implements it. The Rust runtime has no SEXPREC and its own object representation, and a package compiled against its headers still runs, because what the package needs is an implementation of INTEGER, Rf_allocVector and the rest, not a struct. rrs implements those over its own heap and lets the linker resolve the names. Where a real cost appears is REAL(x), which must hand back a genuine double* into contiguous storage, so the payload representation is constrained even though the header is not.
Further reading
- Writing R Extensions <https://cran.r-project.org/doc/manuals/r-release/R-exts.html>, chapters 5 (System and foreign language interfaces) and 6 (The R API: entry points for C code).
- R Internals <https://cran.r-project.org/doc/manuals/r-release/R-ints.html#SEXPs>, section 1.1 (SEXPs).
- System V AMD64 ABI <https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI>, formal specification of the calling convention and struct layout guarantees on x86-64.
- Rustonomicon FFI chapter <https://doc.rust-lang.org/nomicon/ffi.html>, Rust’s guide to foreign function interface and
repr(C).
Where this appears
- threaded-r Keeping R’s C API is why threaded-r patches GNU R instead of rewriting it. It keeps source compatibility with CRAN’s compiled packages and gives up binary compatibility with stock R on purpose.
- r-in-rust (rrs) rrs exports R’s C API over its own representation, which only the opacity of SEXP makes possible.