4

I have got a 16-bit MPU which is different from x86_16 in size of size_t, ptrdiff_t etc. Can anybody give me details and clear instructions about how to customize machine dependency in Frama-C for my MPU?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Max P.
  • 55
  • 6
  • Do not forget to take into account the toolchain used: some machdep constants depend on the compiler, which explains why there are different machdeps for MSVC and GCC, both on x86_64 machines. This is especially relevant when using the Frama-C standard C library. – anol Jan 24 '17 at 15:09

1 Answers1

6

There is currently no way to do that directly from the command line: you have to write a small OCaml script that will essentially define a new Cil_types.mach (a record containing the necessary information about your architecture) and register it through File.new_machdep. Assuming you have a file my_machdep.ml looking like that:

let my_machdep = {
  Cil_types.sizeof_short = 2;
  sizeof_int = 2;
  sizeof_long = 4;
  (* ... See `cil_types.mli` for the complete list of fields to define *)
}

let () = File.new_machdep "my_machdep" my_machdep

You will then be able to launch Frama-C that way to use the new machdep:

frama-c -load-script my_machdep.ml -machdep my_machdep [normal options]

If you want to have the new machdep permanently available, you can make it a Frama-C plugin. For that, you need a Makefile of the following form:

FRAMAC_SHARE:=$(shell frama-c -print-share-path)

PLUGIN_NAME=Custom_machdep
PLUGIN_CMO=my_machdep

include $(FRAMAC_SHARE)/Makefile.dynamic

my_machdep must be the name of your .ml file. Be sure to choose a different name for PLUGIN_NAME. Then, create an empty Custom_machdep.mli file (touch Custom_machdep.mli should do the trick). Afterwards, make && make install should compile and install the plug-in so that it will be automatically loaded by Frama-C. You can verify this by launching frama-c -machdep help that should output my_machdep among the list of known machdeps.

UPDATE If you are using some headers from Frama-C's standard library, you will also have to update $(frama-c -print-share-path)/libc/__fc_machdep.h in order to define appropriate macros (related to limits.h and stdint.h mostly).

Virgile
  • 9,724
  • 18
  • 42
  • I changed the file custom_machdep.ml and run Frama-C in this way:
    $ frama-c -load-script custom_machdep.ml -machdep custom [normal options]
    I got follows:
    [kernel] Registering machdep 'mach' as 'custom'
    [kernel] Parsing .opam/4.02.3+mingw64c/share/frama-c/libc/__fc_builtin_for_no‌​rmalization.i (no preprocessing)
    [kernel] warning: machdep custom has no registered macro. Using __FC_MACHDEP_CUSTOM for pre-processing.
    As I understood, it is OK.
    – Max P. Jan 24 '17 at 17:12
  • Yes. The warning about `__FC_MACHDEP_CUSTOM` is related to what I've said about the headers in `libc`: if you ever happen to use them, you'll have to add a section `#else #ifdef __FC_MACHDEP_CUSTOM` in `__fc_machdep.h`. – Virgile Jan 24 '17 at 18:15
  • I would like to clarify something. In the manual is written: "Frama-C will predefine the macro __FC_MACHDEP_CUSTOM_NAME". But you propose to modify __fc_machdep.h manually. Is that so? By the way, I got an error message on the files that include . The error was thrown by line 267 of the file __fc_machdep.h . So it seems I have to do this. But perhaps is there another way to solve this problem? – Max P. Jan 25 '17 at 09:21
  • @MaxP. the quote from the manual means that Frama-C will call the preprocessor with option `-D__FC_MACHDEP_CUSTOM_NAME`. However, if `__fc_machdep.h` does not have a case for it, this won't do anything. The error message from the preprocessor comes from the fact that `__fc_machdep.h` will complain if it is not given a `__FC_MACHDEP_*` macro that it can recognize. – Virgile Jan 25 '17 at 13:30
  • I added a section #else #ifdef __FC_MACHDEP_CUSTOM in __fc_machdep.h . After that I have got error: [kernel] syntax error at (frama-c -print-share-path)/libc/__fc_define_wchar_t.h:28: Why? I have defined the __WCHAR_T as "unsigned short" in __fc_machdep.h and wchar_t as "unsigned short" in custom_machdep.ml . – Max P. Jan 25 '17 at 14:13
  • That's indeed strange. It should work. I'm afraid that in order to get an appropriate answer, you'll have to post another question with the exact setup that triggers the issue. Comments are too limited to investigate the case properly. – Virgile Jan 25 '17 at 15:37
  • Finally, this issue has been resolved [here](http://stackoverflow.com/a/41983384/7405388). – Max P. Feb 02 '17 at 14:08