Skip to content

Commit

Permalink
Add support for "soft link interface", useful for binfmt_misc
Browse files Browse the repository at this point in the history
In order to be able to execute `.bmir` files directly from the command line,
a "soft link interface" was added (similar to busybox),
where the links `c2m-ei`, `c2m-eg` and `c2m-el` are created, pointing to `c2m`.

These links, to correspond with the binfmt nomenclature, are referred as `interpreters`

The c2mir driver source was adapted to check if the argv[0] is any of those links.

Invoking `c2m` through those links is similar to invoking using
```
$ c2m /path/to/binary {-ei | -eg | -el} <args>...
```

To use with `binfmt_misc`, the driver expects the `P` flag on the binfmt format,
that is, the first argument should be the full path to the executable binary.

```
./a.bmir <args>...
c2m-ei /path/to/a.mir ./a.mir <args...>
```

or if the binary is in PATH

```
a.bmir
c2m-ei /usr/local/bin/a.mir a.mir
```

The binfmt line is
```
:mir:M::MIR:/usr/local/bin/c2m-el:P
```

Do adapt the path where the `c2m-*` are and possibly change `-el` to your favorite interface
(interpeted, generated or lazy)
  • Loading branch information
Wiguwbe committed Jan 10, 2024
1 parent d62b209 commit db5157a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
14 changes: 14 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ L2M_TEST += l2m-test$(EXE)
endif

EXECUTABLES=$(BUILD_DIR)/c2m$(EXE) $(BUILD_DIR)/m2b$(EXE) $(BUILD_DIR)/b2m$(EXE) $(BUILD_DIR)/b2ctab$(EXE) $(L2M_EXE)
LINKS=$(BUILD_DIR)/c2m-ei$(EXE) $(BUILD_DIR)/c2m-eg$(EXE) $(BUILD_DIR)/c2m-el$(EXE)

Q=@

Expand Down Expand Up @@ -157,6 +158,9 @@ else
endif
endif
install -m a+rx $(EXECUTABLES) $(PREFIX)/bin
-ln -s c2m $(PREFIX)/bin/c2m-ei
-ln -s c2m $(PREFIX)/bin/c2m-eg
-ln -s c2m $(PREFIX)/bin/c2m-el

$(PREFIX)/include $(PREFIX)/lib $(PREFIX)/bin:
mkdir -p $@
Expand All @@ -174,6 +178,7 @@ else
endif
endif
$(RM) $(EXECUTABLES:$(BUILD_DIR)/%=$(PREFIX)/bin/%)
$(RM) $(LINKS:$(BUILD_DIR)/%=$(PREFIX)/bin/%)
-rmdir $(PREFIX)/include $(PREFIX)/lib $(PREFIX)/bin
-rmdir $(PREFIX)

Expand Down Expand Up @@ -226,6 +231,15 @@ $(BUILD_DIR)/c2mir/%.$(OBJSUFF): $(SRC_DIR)/c2mir/%.c | $(BUILD_DIR)/c2mir
$(BUILD_DIR)/c2m$(EXE): $(C2M_BUILD) $(BUILD_DIR)/libmir.$(LIBSUFF) | $(BUILD_DIR)
$(LINK) $^ $(LDLIBS) $(EXEO)$@

$(BUILD_DIR)/c2m-ei$(EXE): $(BUILD_DIR)/c2m$(EXE)
ln -s c2m$(EXE) $@

$(BUILD_DIR)/c2m-eg$(EXE): $(BUILD_DIR)/c2m$(EXE)
ln -s c2m$(EXE) $@

$(BUILD_DIR)/c2m-el$(EXE): $(BUILD_DIR)/c2m$(EXE)
ln -s c2m$(EXE) $@

$(BUILD_DIR)/c2mir:
mkdir -p $@

Expand Down
33 changes: 30 additions & 3 deletions c2mir/c2mir-driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,43 @@ static VARR (input_t) * inputs_to_compile;

static void init_options (int argc, char *argv[]) {
int incl_p, ldir_p = FALSE; /* to remove an uninitialized warning */
VARR_CREATE (char, temp_string, 0);
VARR_CREATE (char_ptr_t, headers, 0);
VARR_CREATE (macro_command_t, macro_commands, 0);

// see if called from a linked interface
{
char *slash_ptr = strrchr(argv[0], '/');
if(slash_ptr == NULL)
slash_ptr = argv[0];
else
slash_ptr++;

if(!strcmp(slash_ptr, "c2m-ei"))
interp_exec_p = TRUE;
else if(!strcmp(slash_ptr, "c2m-eg"))
gen_exec_p = TRUE;
else if(!strcmp(slash_ptr, "c2m-el"))
lazy_gen_exec_p = TRUE;

if(interp_exec_p || gen_exec_p || lazy_gen_exec_p) {
// (probably) called from binfmt_misc
// argv[1] should be full path to binary
// argv[2:] should be args, including binary name
VARR_PUSH (char_ptr_t, source_file_names, argv[1]);
VARR_TRUNC (char_ptr_t, exec_argv, 0);
for (int i=2; i < argc; i++) VARR_PUSH (char_ptr_t, exec_argv, argv[i]);

return;
}
}

options.message_file = stderr;
options.output_file_name = NULL;
options.debug_p = options.verbose_p = options.ignore_warnings_p = FALSE;
options.asm_p = options.object_p = options.no_prepro_p = options.prepro_only_p = FALSE;
options.syntax_only_p = options.pedantic_p = FALSE;
gen_debug_level = -1;
VARR_CREATE (char, temp_string, 0);
VARR_CREATE (char_ptr_t, headers, 0);
VARR_CREATE (macro_command_t, macro_commands, 0);
optimize_level = -1;
threads_num = 1;
curr_input.code = NULL;
Expand Down

0 comments on commit db5157a

Please sign in to comment.