Generate Executable from llvm::Module - llvm

My question is kinda similar to this one: How to generate an executable from an llvm::Module?. But as the answeres there are from 2012, and I'm not sure if anything has changed.
So I've got an LLVM::Module and want to produce an executable from it. I know that I can build an object file from it and invoke a linker on that .o file to get an executable, but I don't want the I/O overhead of reading and writing a intermediate .o file.
I'm wondering if there is an API in the LLVM project that I can use to generate a exec. from a module. Is the llvm::Linker class maybe a good point to start?

Related

Compiling single file from Chromium

I am trying to run codeql on Chromium among a few other open-source software, and I was wondering if it is possible to compile a single file from the full source code, because my machine takes a long time to compile such huge codebases.
For example, is it possible to compile the media/filters/ffmpeg_video_decoder.cc file only?
Compiling is not the issue. If you only compile one source file, it will be missing several functions that would be added to the final binary in the linker phase.
https://en.wikipedia.org/wiki/Linker_(computing)
Only compiling one file will (most probably) not make a working binary, as you need a main function along with all the other code.
From a quick CTRL-F through the source code, a main function, which is critical for the program to run, is absent from the file you mentioned, so you definitely won't be able to make a working executable.
If you're making a .lib (library) file, it might work, although you're almost certainly going to run into undefined references, as the code is broken apart among many files.

MSBuild relinks every native project after internal changes in native dependency dll

Let's imagine this situation:
Project Foo compiles to Foo.dll and Foo.lib.
Project Bar compiles to Bar.dll and references Foo as a dependency in VS project.
Now the problem: Almost every time I change internal details (Not API, not set of exported functions) of Foo.dll - Foo.lib gets updated, and Bar.dll gets relinked with new Foo.lib. If I turn on detailed output in VS I see:
Source compilation required: input C:\PROJECTS\FOO\RELEASEUNICODE\FOO.LIB is newer than output C:\PROJECTS\BAR\RELEASEUNICODE\BAR.DLL.
And next command starts link.exe to link new Bar.dll
The question: Why does this happen? I'm not really familiar with idea of lib file for dll (I haven't seen .a file for .so dynamic lib in linux), but isn't the main idea of dll file is to avoid any compile-time linking? Why does the lib file change every time I change internals of foo.dll ? Is there a way to avoid relinking of dependant library? In my project I have dozens of dlls depending on foo.dll, and every time I change 10 lines of code in foo.dll - all these dependencies endup relinked, and this takes a lot of time.
MSBuild simply looks time stamps for when a file was modified. It isn't smart enough to know that no public API changed.
So the rule that msbuild operates by is, if an input is newer, the dependent (in your case bar) will have to be rebuilt (in your case simply relinked).
I supposed if msbuild knew about api changes, it would have to parse all the code and keep some sort of database of the entire code, something that for a build is mostly unnecessary and very costly.

is it possible to link an exe file as a lib file to another project?

The question is in the title.
I have coded an .exe project, I would like to use one of the function of this project in another project.
Maybe it is a silly question, but if it is possible this would limit the number of projects in my solution...
I have given a simple try, I get an LNK1107 error.
I would say it is not possible, but it is hard to find a clear answer on the net.
No, it is not possible.
An executable is a standalone entity. It is the result of linking object files together to produce a self-contained, well, executable.
Linking two executables together will, at best, result in duplicate definitions of main (in reality it's a little more complicated, but…).
What you want to do is share the object files before they become an executable, and this is typically accomplished by moving your shared/common code into a "library" then link the library into both projects.
Alternatively, you could keep the executables all separate, but share the code at the version-control level, e.g. with SVN externals.

Need help building a DLL for JNI call. Unresolved external references. Borland Compiler

I need to build a DLL that can then be loaded with JNI from a java program. I was able to do this last year and it worked fine. I'm trying to recompile my same .cpp file now though that I make the dll with and it is failing because of an included dll dependency that gets brought in.
I have a c++ program that calls about 5 functions from some existing C++ code here. These functions are part of a huge codebase that are normally all linked together to build a set of 5 dlls.
I use dependency walker to view my dll and last year it compiled with just 2 dependent system dlls being put in my dll. Today I'm trying to recompile the same dll, but it brings in a 3rd dll file if I link with the .lib file from our existing codebase that contains the functions I use.
Basically I know my dll will work fine with JNI if I can avoid that 3rd dll showing up in my program. The problem is I don't know how to reference the functions I need in my code from our existing code base without linking to the lib file.
I can get this to work with standard c++ files and methods. This problem only happens when I reference this preexisting code from our huge codebase.
If I do not link my .obj file with a .lib file from our code I get unresolved reference messages from my Borland 5.5 compiler I have to use.
The overall issue is that my dll file works fine when I call it from a c++ exe file, but Java cannot handle something in it. Also, if I compile my code into a .so file in unix instead of a windows dll, Java JNI works fine and can load it. I know the problem is related to how Windows uses dlls, and I know if this 3rd dll doesn't load as part of my dll it will also work. I just have no idea what I did last year to build my dll without this 3rd one showing as a dependency.
If I comment out the functions from our existing code it compiles fine and loads because the 3rd dll dependency doesn't get put in my dll.
More Details
I had a message about missing _strcopy, so I linked in the cw32mti.lib file and that went away, but then that cw32mti.dll shows up in my dll file. How do I prevent the missing reference message for something like that and prevent it from putting the dll in my dll?
My link command looks like this.
ilink32 mydll.obj, mydll.dll,,cw32mti
The only way I get the other missing references to work is adding the other dll to my link command like:
ilink32 mydll.obj, mydll.dll,,cw32mti.lib other.lib
Where other.dll contains functions I call from mydll.dll such as calculate(int a, int b), so my code has a link it in like calculate(num1, num2); The problem is when I use the lib that contains the calculate method, it also brings in other dlls linked to the other.dll that I do not want to load. I need to be able to call calculate(num1, num2) without adding other.dll to mydll.dll. This worked before without dynamically calling calculate and using the getprocaddress type of coding.
Update - I eventually had to give up on getting the windows dll to work with the smartheap memory manager. Since this code was deployed in unix, I was able to just build the .so files and get those to work with JNI. For the Windows dll compile, I put some conditional compiler statements around the JNI code that was causing the smartheap dll to be loaded, so then when it compiles in windows it does not use that code. Instead I just had it print out a statement saying it was not executing in windows.
We also ran into issues later with getting our 64 bit JBoss server to run and load these 32 bit .so files. I ended up running a parallel JBoss server next to the 64bit one and called the methods that referenced the 32 bit library on the 32 bit instance of JBoss.
It may evolve into more later, but for now this task is working for us after months of trying many different things. I appreciate all of the help and input here.
If you can get your hands on the application DLL built from last year, use TDUMP to see what the module dependencies are. (You might have to analyze all the DLLs to get a good picture.) Then endeavor to reproduce that in the new DLL, probably by adjusting the linker's configuration .DEF file.
Without any code or .DEF file shown in your question, it is very difficult to be more specific.

How to build midas.obj from the midas source code

Recently I discovered a problem on the midas and I fixed it, the problem now is that I want to use MidasLib not the midas.dll and with the source code I'm only able to build the DLL.
The source is C++ and I have very few knowledge with it. I know the MidasLib.pas uses internally midas.obj, so I need to create it to statically link the midas to my application. How to do it on C++ Builder? (XE)
When you compile C++ code, the compiler creates an .OBJ file for each .CPP/.C file you have and saves them somewhere on your computer. What happens in most cases is that one would run a linker on all of those .OBJ files to join them into a single EXE or DLL, but in your case you don't need those results. Your C++ Builder is, like most programming IDEs, automatically doing both the compilation and linking.
If you just want the .OBJ, you need to find where in your project folder C++ Builder is placing its .OBJ files (called its "intermediate output", typically, as it is the intermediate step between compilation and linking). So you must have a source file called midas.cpp or midas.c that produces a corresponding output file called midas.obj.