Skip to content

Commit

Permalink
Add Zinc RuntimeID Mask for portability
Browse files Browse the repository at this point in the history
  • Loading branch information
dra27 committed Oct 22, 2024
1 parent 8498694 commit 4c503dd
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ ___________

- #13???: Introduce a RuntimeID for use in filename mangling to allow different
configurations and different versions of ocamlrun and the shared runtime
libraries to coexist harmoniously on a single system.
libraries to coexist harmoniously on a single system. A portable version is
used in launcher header for bytecode executables.
(David Allsopp, review by ???)

### Code generation and optimizations:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defaultentry: $(DEFAULT_BUILD_TARGET)

include stdlib/StdlibModules

RUNTIME_NAME = $(BINDIR)/ocamlrun-$(BYTECODE_RUNTIME_ID)$(EXE)
RUNTIME_NAME = $(BINDIR)/ocamlrun-$(ZINC_RUNTIME_ID)$(EXE)
CAMLC = $(BOOT_OCAMLC) $(BOOT_STDLIBFLAGS) \
-use-prims runtime/primitives \
-use-runtime '$(subst ','\'',$(RUNTIME_NAME))'
Expand Down Expand Up @@ -2703,7 +2703,7 @@ install::
$(LN) "$(TARGET)-$(1)-$(BYTECODE_RUNTIME_ID)$(EXE)" "$(1)$(EXE)"
cd "$(INSTALL_BINDIR)" && \
$(LN) "$(TARGET)-$(1)-$(BYTECODE_RUNTIME_ID)$(EXE)" \
"$(1)-$(BYTECODE_RUNTIME_ID)$(EXE)"
"$(1)-$(ZINC_RUNTIME_ID)$(EXE)"
endef
define INSTALL_RUNTIME_LIB
ifeq "$(2)" "BYTECODE"
Expand Down
1 change: 1 addition & 0 deletions Makefile.build_config.in
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,4 @@ TSAN_NATIVE_RUNTIME_C_SOURCES = @tsan_native_runtime_c_sources@

# OCaml release number, relative to 3.12 (used for RuntimeID)
OCAML_RELEASE_NUMBER = @OCAML_RELEASE_NUMBER@
ZINC_RUNTIME_ID = @zinc_runtime_id@
2 changes: 1 addition & 1 deletion bytecomp/bytelink.ml
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ let write_header outchan =
else
let runtime =
Printf.sprintf "ocamlrun%s-%s"
!Clflags.runtime_variant Config.bytecode_runtime_id
!Clflags.runtime_variant Config.zinc_runtime_id
in
Filename.concat runtime_info.bindir runtime
in
Expand Down
3 changes: 3 additions & 0 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ AC_SUBST([QS])
AC_SUBST([ar_supports_response_files])
AC_SUBST([bytecode_runtime_id])
AC_SUBST([native_runtime_id])
AC_SUBST([zinc_runtime_id])

## Generated files

Expand Down Expand Up @@ -2723,6 +2724,7 @@ quintet3_native="$(echo "${alphabet}" | cut -c $(expr ${quintet3_native} + 1))"

bytecode_runtime_id="${quintet3_byte}${quintet2}${quintet1}QUINTET0"
native_runtime_id="${quintet3_native}${quintet2}${quintet1}QUINTET0"
zinc_runtime_id="00${quintet1}QUINTET0"

# Do not permanently cache the result of flexdll.h
unset ac_cv_header_flexdll_h
Expand Down
29 changes: 27 additions & 2 deletions runtime/Mangling.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ OCaml 5.x series.
## Masks

A particular configuration of the compiler has one RuntimeID, but this is used
in two different contexts where certain bits are masked out:
in three different contexts where certain bits are masked out:

1. Bytecode Mask (0xe7fff) is used by `libcamlrun` and masks out the frame
pointers and spacetime bits since these affect neither the runtime nor code
generation in bytecode.
2. Native Mask (0xfffff) is used by `libasmrun`. Note that in native code, the
only place where name mangling is used is natdynlink, and bit 8 is therefore
_never_ set.
3. Zinc Mask (0x001ff) is used by `ocamlc` for bytecode executables which do not
contain their own runtime.

## File Name Mangling

Expand All @@ -73,10 +75,33 @@ Mangling is applied to the name of any file which will be loaded at runtime:

- `ocamlrun` (and variants) are triplet-prefixed and Bytecode-suffixed. A
symbolic is created both for `ocamlrun` and for `ocamlrun` Byte-suffixed but
not triplet-prefixed.
not triplet-prefixed. See below for additional symbolic links which are
created.
- C stub libraries loaded by both the bytecode runtime and bytecode `Dynlink`.
These are triplet-prefixed and Bytecode-suffixed. The effect is that any
runtime attempts to load stub libraries compiled for exactly the same runtime.
- Shared versions of the bytecode and native runtimes (`libcamlrun_shared.so`
and `libasmrun_shared.so`). These are triplet-prefixed and
Bytecode/Native-suffixed respectively.

Bytecode executables compiled without their runtime pose an additional challenge
as these are portable. When a bytecode image is produced, the Zinc runtime ID is
used to mangle the name of the runtime with Bit 7 set if the bytecode image does
not require 63 bit `int` support (i.e. if the image compiles without error with
`ocamlc -compat-32` then Bit 7 will be set in the RuntimeID, even with a 64-bit
`ocamlc`) and Bit 8 unset. The runtime variant is then Zinc-suffixed to create
the runtime name for the executable's header. For example, if OCaml 5.1 is
configured with `--disable-flat-float-array --disable-shared` (Runtime suffix
`8093`) then an executable not requiring 63-bit `int`s will use `ocamlrun-0053`
as the runtime name. The "Zinc" RuntimeID here creates a portable and
reproducible runtime name. In order to facilitate this, the following additional
symbolic names are created, for each variant `<ocamlrun>`, with no
triplet-prefix:

- Zinc-suffixed
- For runtimes supporting 63-bit `int` values, Zinc-suffixed but with Bit 7 set
(i.e. a 64-bit OCaml runtime also advertises that it works for 32-bit)

Additionally, the header for the bytecode executable will initially search for a
runtime with Bit 8 un-set (i.e. with a preference for runtimes supporting shared
libraries) and fallback to searching for one with Bit 8 set otherwise.
1 change: 1 addition & 0 deletions utils/config.fixed.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ let align_int64 = true
let function_sections = false
let afl_instrument = false
let bytecode_runtime_id = ""
let zinc_runtime_id = ""
let native_runtime_id = ""
let native_compiler = false
let tsan = false
Expand Down
1 change: 1 addition & 0 deletions utils/config.generated.ml.in
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ let function_sections = @function_sections@
let afl_instrument = @afl@

let bytecode_runtime_id = {@QS@|@bytecode_runtime_id@|@QS@}
let zinc_runtime_id = {@QS@|@zinc_runtime_id@|@QS@}
let native_runtime_id = {@QS@|@native_runtime_id@|@QS@}

let native_compiler = @native_compiler@
Expand Down
6 changes: 6 additions & 0 deletions utils/config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ val bytecode_runtime_id : string
@since 5.3 *)

val zinc_runtime_id : string
(** {!bytecode_runtime_id} with the Zinc Mask (used in headers to refer portably
to ocamlrun)
@since 5.3 *)

val native_runtime_id : string
(** The RuntimeID for libasmrun_shared
Expand Down

0 comments on commit 4c503dd

Please sign in to comment.