We try to build tests in java from a fortran library and we would like to check COMMON variables. Is there a way to do it ?
If the symbol for it shows up in your shared library (use nm or objdump to look for it), then you can likely request its address using NativeLibrary.getGlobalVariableAddress(), then extract its value using the appropriate Pointer.getXXX() method.
Related
I am evaluating Keil Microvision IDE on STM32H753.
I am doing compiler comparison between ARMCC5 and AC6 in the different optimisation levels. AC6 is based on Clang.
My code is not using memcpy and I have unchecked "Use MicroLIB" in the project settings , However a basic byte per byte copy loop in my code is replaced by a memcpy with AC6 (only in "high" optimisation levels). It doesn't happen with ARMCC5.
I tried using compilation options to avoid that, as described here: -ffreestanding and -disable-simplify-libcalls, at both compiler and linker levels but it didn't change (for the second option, I get an error message saying that the option is not supported).
In the ARMCLANG reference guide i've found the options -nostdlib -nostdlibinc that prevent (??) the compiler to use any function of a standard lib.
However I still need the math.h function.
Do you know how to prevent clang to use functions from the Standard C Lib that are not explicitely called in the code ?
EDIT: here is a quick and dirty reproduceable example:
https://godbolt.org/z/AX8_WV
Please do not discuss the quality of this example, I know it is dumb !!, I know about memset, etc... It is just to understand the issue
gcc know a lot about the memcpy, memset and similar functions and even they are called "the builtin functions". If you do not want those functions to be used by default just use the command line option -fno-builtin
https://godbolt.org/z/a42m4j
Let's say I want to call a C function: printf, getpid, socketpair or any other standard C function from my code in Assembly language. I know how to do that in terms of implementation. However, I'll also have to know where one of those functions defined -- in what Linux file, so that I can pass the name of that of that file to the linker. I think it should be a "so" file. How would I find out in what file it's defined?
Note that my question is general and the functions I've mentioned above are just an example. How would I know in what Linux library any arbitrary C function defined?
These (printf, getpid, socketpair) are all part of the standard C library. That's the library that gets automatically linked to every C program.
I think the easiest way to solve your problem would be to link it with gcc, which will call the linker and link the appropriate version of the standard C library in.
If you want to proceed your way:
echo 'int main(){ }' |gcc -x c - && ldd ./a.out |grep libc
should give you the so file. In my case it's:
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc3a2b46000)
There's no generic solution for function x as far as I know.
You'd have to google to map a function to a library. If the library uses a library specific prefix instead of bare names (like the standard C library unfortunately does in most cases), googling it should be fairly unambiguous. (After that, you'd need to find where the library's SO file is on your system.)
If it's a standard C library function, libc.so is probably correct.
If it's another library, that library's documentation should tell you. Eg. the manpage for pthread_create says
Compile and link with -pthread
and you then need to look at the GCC documentation to see what it does with that option (eg, -lpthread plus some other stuff, so you want libpthread.so).
With third-party libraries, the onus is entirely on the library developer/distributor to tell you where to find the symbols listed in their API.
Worst case you can just find all .so files under /usr, /lib, /opt or wherever else you have libraries installed, and run nm -D on them. You're looking for entries of type t or T.
Note we're assuming that you only want shared objects (dynamic libraries) - if you're talking about arbitrary third-party libraries, they could equally ship static (.a) archives.
I have a static library, where one of the objects defines a symbol:
nm mylib.a
...
00007340 t _a_local_symbol
...
I need to access the function from my C code. Obviously, I don't have the source code for the library, so I can work only with the archive file that I have at hand.
This is further restricted by iOS linker.
A bit more context. The library is Objective-C++, the function in question is pure C. I don't have original headers, but I've got the function signature restored.
objcopy has a flag to do what you want:
--globalize-symbol <name> Force symbol <name> to be marked as a global
Not sure whether objcopy works on iOS object files though.
There is only one public library for binding Lua to Ada I have found (http://coreland.ath.cx/code/lua-ada), but how can it be used on a Windows platform? What do I need to use in my ada-project to get lua.ads.adb libraries defined in project-files working properly?
I tried to put lua sources in my ada-project directory befory compiling but that does nothing - GNAT raises an error like undefined reference to <c++ function>.
.
Windows doesn't seem to be on Lua-Ada's list of supported platforms. Still, the bindings ought to be somewhat portable to other Gnat platforms. You would need to get hold of a Windows Lua library (most likely a DLL) and graft the two together somehow though.
It's doable - I did something similar with Clips once. However, anyone doing this is going to need to be (or become) quite conversant with the C/C++ linker, Mingwin's support for Windows libraries (typically through DLLs), and how Ada interfaces to C linkages work.
Only by testing and testing once more I found how to bind safely Lua.
First of all is to unpack lua-ext.c from Ada-Lua package and all Lua-sources to main Ada-project directory. Then renaming lua.c to lual.c (or something equal) to eliminate error with same object-file name ('lua.ads->lua.o | lua.c->lua.o'). The last one is to turn on C-compiler in GNAT. It could be done via "Project - Edit project properties - Languages".
That's all I made to have my lua-files work with Ada-program.
P.S. To turn on all available Lua-libraries in Ada-program should be called those two procedures:
Lua.Lib.Open_Base(Lua.State_t); -- this will append to _G all main functions
Lua.Lib.Open_Libs(Lua.State_t); -- this will append math, string, package, etc. libraries
How can I tell if a module is being called dynamically or statically?
If you are operating on z/OS, you can accomplish this, but it is non-trivial.
First, you must trace up the save area chain and use CSVQUERY to find out which program owns each save area. Every other program will be a Cobol runtime module, like IGZCPAC. Under IMS, CICS, TSO, et al, those modules might be different. That is the easy part.
Once you know who owns all the relevant save areas, you can use the OS LOADER / BINDER / LINKER utilities to discover what artifacts are in the same modules. This is the non-easy part.
The ONLY way is to look at the output of the linkage editor (IEWL) or the load module itself. If the module is being called DYNAMICALLY then it will not exist in the main module, if it is being called STATICALLY then it will be seen in the load module. Calling a working storage variable, containing a program name, does not make a DYNAMIC call. This type of calling is known as IMPLICITE calling as the name of the module is implied by the contents of the working storage variable. Calling a program name literal.
Calling a working storage variable,
containing a program name, does not
make a DYNAMIC call.
Yes it does. Call variablename is always DYNAMIC.
Call 'literal' is dynamic or static according to the DYNAM/NODYNAM compiler option.
Caveat: This applies for IBM mainframe COBOL and I believe it is also part of the standard. It may not apply to other non-standard versions of COBOL.
For Micro Focus COBOL statically linking is controlled via call-convention on the call (bit 3) or via the compiler directive LITLINK.
When linking statically the case of the program-id/entry-point and the call itself is important, so you may want to ensure it is exact and use the CASE directive.
The reverse of LITLINK directive is the NOLITLINK directive or a call-convention without bit 3 set!
On Windows you can see the exported symbols in your .dll by using the "dumpbin /exports" utility and on Unix via the 'nm' utility.
A import .lib for the .dll created via "cbllink" can be created by using the '-K'command line option on cbllink.
Look at the call statement. If the called program is described in a literal then it's a static call. It's called a dynamic call if the called program is determined at runtime:
* Static call
call "THEPROGRAM"
* Dynamic call
call wsProgramName