How to manage automatically generated c-files from matlab coder? - c

I have an existing dll with multiple functions, let's call it mylib. I decided to use matlab coder to help me write my c-files. These c-files are compiled and linked to generate mylib using VS 2015.
So I test this theory i first started by converting funcA.m to funcA.c using matlab coder ver 3.4 (R2017b) . funcA is ust a simple mathematical function. Then I compiled and linked all the c-files generated by matlab coder to include funcA inside mylib. And this worked perfectly. Meaning, funcA which was originally a m-file was compiled, linked to mylib and had produced the accurate results.
The problem arises when I use matlab coder to generate my second function. Let's call the second function -> funcB
After converting funcA.m to funcA.c, matlab coder had generated other c-files which includes xgeqp3.c and xnmr2.c and its corresponding header files. I consider them non-readable by humans because they were automatically generated by matlab coder and it is not human friendly to read them as you can tell by the name of the c-files. However, they are being called by funcA so they have to be compiled into the library as well.
These matlab generated c-file caused me problems because when I converted funcB.m to funcB.c, it had also generated xgeqp3.c and xnmr2.c. Although the files names are identical, its contents are different, with different formal parameters and of course different c-codes, even though they have the same function name and file name.
So the same library cannot contain these two functions generated by matlab coder because the declarations for xgeqp3.c and xnmr2.c contradicts. I'm sure other people might have the same problem. Is there a way I can manually name the extra c-files generated my matlab coder so that I can avoid such duplicate names? Or is there a way I can manage the c-files with duplicate names but different codes? The easier option is to create separate libraries for the two functions. But I want all my mathematical functions to be in the same library.
How should I handle this situation? I'm open to new ideas. Thank you in advance.

You should generate the c code together for all the functions that you want compile in the same dll.
If you are using command-line you should do:
codegen funcA, funcB
If you are using the Matlab Coder App, add both functions as entry point files.

Related

Get Executable Binary for a Single C Function

I have a simple C function, called add:
int add(int a, int b) {
return a + b;
}
I need to get the optimized executable binary code for this given function without any of the other side effects of the binary. This would then be dynamically loaded and modified to include templates via a JIT system. I have experimented with trying to use objdump, but that doesn't really work for getting the actual binary as the object file is not the actual executable.
I need to link together the moving parts of functions to include their binary together at runtime and enabled PROT_EXEC to execute all of these partials together as a single function and call as a function pointer. The reason I need this is because I have a byte instruction encoding type which I would like to turn into JIT functions on the fly (similar to JIT compiling RegExp into validator functions). If there are any other viable solutions I should consider, then please describe them.
The main reason I want to use this approach is because I can see the godbolt shows the binary for functions as self-contained, so hopefully I can do something similar to include the binary in my own project.
Thank you for any help.
Note: the C function is entirely self-contained without referencing other functions
The easiest way is using IDA. You can compile your code (preferably with symbols) into ELF file/ EXE file and open the generated file in IDA.
There you can see your function in the function window and then move into the hex window to copy the hex dump of your function.
Note: There are a lot of things that can go wrong here, for example, you must compile your code with the flag -PIC so it wouldn't use absolute addresses.
It is also recommended to know at least basic assembly.
This link might be interesting: https://github.com/Neetx/Shellcode-Extractor

Where are the header functions defined? [duplicate]

When I include some function from a header file in a C++ program, does the entire header file code get copied to the final executable or only the machine code for the specific function is generated. For example, if I call std::sort from the <algorithm> header in C++, is the machine code generated only for the sort() function or for the entire <algorithm> header file.
I think that a similar question exists somewhere on Stack Overflow, but I have tried my best to find it (I glanced over it once, but lost the link). If you can point me to that, it would be wonderful.
You're mixing two distinct issues here:
Header files, handled by the preprocessor
Selective linking of code by the C++ linker
Header files
These are simply copied verbatim by the preprocessor into the place that includes them. All the code of algorithm is copied into the .cpp file when you #include <algorithm>.
Selective linking
Most modern linkers won't link in functions that aren't getting called in your application. I.e. write a function foo and never call it - its code won't get into the executable. So if you #include <algorithm> and only use sort here's what happens:
The preprocessor shoves the whole algorithm file into your source file
You call only sort
The linked analyzes this and only adds the source of sort (and functions it calls, if any) to the executable. The other algorithms' code isn't getting added
That said, C++ templates complicate the matter a bit further. It's a complex issue to explain here, but in a nutshell - templates get expanded by the compiler for all the types that you're actually using. So if have a vector of int and a vector of string, the compiler will generate two copies of the whole code for the vector class in your code. Since you are using it (otherwise the compiler wouldn't generate it), the linker also places it into the executable.
In fact, the entire file is copied into .cpp file, and it depends on compiler/linker, if it picks up only 'needed' functions, or all of them.
In general, simplified summary:
debug configuration means compiling in all of non-template functions,
release configuration strips all unneeded functions.
Plus it depends on attributes -> function declared for export will be never stripped.
On the other side, template function variants are 'generated' when used, so only the ones you explicitly use are compiled in.
EDIT: header file code isn't generated, but in most cases hand-written.
If you #include a header file in your source code, it acts as if the text in that header was written in place of the #include preprocessor directive.
Generally headers contain declarations, i.e. information about what's inside a library. This way the compiler allows you to call things for which the code exists outside the current compilation unit (e.g. the .cpp file you are including the header from). When the program is linked into an executable that you can run, the linker decides what to include, usually based on what your program actually uses. Libraries may also be linked dynamically, meaning that the executable file does not actually include the library code but the library is linked at runtime.
It depends on the compiler. Most compilers today do flow analysis to prune out uncalled functions. http://en.wikipedia.org/wiki/Data-flow_analysis

Lua: compile a script with all dependencies

Is there a way one can compile a script in which everything except standard Lua libraries is linked statically?
I am embedding a script in my C program, the functionality is split between modules, which are then loaded in main module. I would like to compile the main module into Lua bytecode, convert it to hex code, so it can easily be stored in my program. The problem is that main module still requires source code of other modules to be present either in LUA_PATH or in current directory. Is there a way how to override this behaviour? Sure, I could simply merge all files (automatically, during the compilation) into one lua source file, but since I am a beginning with Lua, I would like to know if there are some other solutions to this problem, and perhaps expand my horizons.
I was looking at luac and luajit but I could not make them to do what I need.
Any hints?
There are tools like srlua that may help. You may also check this presentation on luawrap and this discussion for ideas.
serialise lua_state
In C if you iterate through the loaded lua_state for functions and values you could create an as-if lua representation.
This could be serialised to lua form and then luac compiled.
You would have to ignore C bound functions and userdata.
You would need to walk meta tables.
encapsulated form
By changing the loader, you could have a single resource which has each of lua files by name and treats load requests as seeks and reads in resource.

Force macro definition in C-code generated with SImulink

I am building a Simulink model in order it generates the C-code equivalent. Actually, the C-code doing that already exists but I want to visualize it with Simulink. I know that the code Simulink will generate will not be the same than the one it is based on, but there are some functionalities I want to keep.
For example, I use a lot of Macro (#define) in the C code, to be more readable and I really do not know how to force Simulink to define these macro's. So my question is, which object/structure/data should I create and where (model workspace or Matlab workspace?) such that when C code will be generated, all my macro's will be defined with preprocessor commands.
I thank you a lot for any lead you could propose.
Finally I found my answer in this huge user guide
https://engineering.purdue.edu/~dionysis/EE452/Lab2/Lab2_Supporting_Materials/Embedded%20Coder_Users%20Guide.pdf
I had to use mpt.Parameter Imported from file and check Inline parameter option.

Using of two Simulink/MATLAB static libraries

I have C application which uses two static libraries. Source code for these libraries are generated and built by Simulink/MATLAB.
The problem is that Simulink generates extern non-static function
real_T rt_urand_Upu32_Yd_f_pw(uint32_T *u) for block "Uniform Random Number" into the file MyModel.c for each model. And when I include these libraries in my C application I got linker error of function redefinition.
Is there any way to change code generation process in Simulink?
Your problem as I understand it: Your 2 libraries are generated from 2 different Simulink models that both use this block, so obviously Simulink Coder cannot know that you want to use both of them and just generates the same function twice.
Here are some proposals off the top of my head if it is OK for you to have only 1 library instead of 2.
Proposal:
Put both models as model references in one top model. Generate code. Simulink Coder realizes that the block is used in both models and generates the function only once.
Proposal2:
Wrap the Uniform Random Number block in a subsystem with Function packaging to a separate C file. Compile the generated source files of both models together, deleting one of the copies of the C file that you generated separately.

Resources