Working with PE Section Headers in linux - c++

I have successfully written a c++ code for finding the names of section headers and their range of virtual addresses in visual studio (working in windows XP) by reading an executable file.
Problem - Now I want to implement the same code in Linux environment.I am not an experienced Linux user , so therefore I am not aware of all the tools Linux provides. Is it possible to execute windows environment specific code in Linux . If yes how will it recognize header files such as windows.h & winNt.h.And of-course I have to make the executable file available in Linux too.

You should be able to write the code you describe portably, using only functions described in the C++ standard. Use std::ifstream, not CreatFile(), for example.
If you can write your program in visual studio with no reference to <windows.h>, then it will very likely run under Linux, too.
On the other hand, if all you are trying to do is to list the section headrs, try objdump -x foo.exe.

Related

Use Shared Object (.so) in a C++ project develop in Windows with Visual Studio for Linux using WSL

For a task I have I must develop C++ code for Linux but from Windows. I'm using Visual Studio 2019 with WSL 2 and the tests I've run worked well, it compile .out/.so/.a and giving me the ability to debug the code as if it was a regular project.
The problem is when I need to link external .so to my project. In regular Windows-VS-c++ development I define the input linker to the directory of the .lib. But in Linux their no .lib (or is their? I'm not familiar with Linux-C++).
In short how do I consume Shared Object (.so) files from another project using Visual Studio 2019 compiling for Linux using WSL 2 ?
Ask your boss permission to install Linux on your work computer
(e.g. some genuine Linux distribution such as Debian or Ubuntu in some separate disk partition). This is -in terms of effort and your time- the cheapest route to follow.
Read of course Advanced Linux Programming, syscalls(2), How to write shared libraries, dlopen(3), proc(5), elf(5), ld.so(8), the Program Library HowTo, the C++ dlopen minihowto, the documentation of GCC, the documentation of GNU make, the documentation of GNU binutils, the documentation of GDB.
Of course, read more about programming in C++. C++ is a difficult language (on both Linux and Windows). Refer to this website. Read later the C++11 standard n3337.
If you code a single C++ translation unit foo.cc (you could use GNU emacs to edit it), compile it first into a shared object foo.so using a command g++ -Wall -Wextra -g -fPIC -shared foo.cc -o foo.so (all warnings, DWARF debug information, position independent code).
If your C++ shared library is built from several C++ translation units, learn how to use a build automation tool such as GNU make or ninja. And use it in a terminal emulator on the command line. Be aware of ASLR. Use strace(1), ltrace(1), gdb(1) to understand the dynamic behavior of your or others software.
In some cases, generating parts of your C++ code (e.g. with ANTLR or SWIG) is worthwhile. Notice that Qt is doing so.
Consider using some cross-platform C++ frameworks such as Qt or POCO.
For some projects, writing your GCC plugin could be useful. See this draft report.
In short how do I consume Shared Object (.so) files from another project using Visual Studio 2019 compiling for Linux using WSL 2 ?
Don't use Visual Studio (I never used it myself, but according to rumors it is unfit for cross-compilation from Windows to Linux). Perhaps use Visual Studio Code (to which I prefer GNU emacs, but you might try gedit, geany, vim, kate etc...)
On Linux, all IDE for C++ programming would run a GCC or Clang compiler (and you'll need to understand what compilation command they are running for you). You could also be interested by the Clang static analyzer or by Frama-C++
Take inspiration from existing open source C++ projects on Linux
Look on github or gitlab - e.g. libonion, Qt, FLTK, fish, icecream) ... Read also Linux From Scratch.
above all, approach Linux programming with a fresh and open mindset.
Read about the Unix philosophy, it is relevant on Linux. And very different from Microsoft design ideas related to Windows.
I never used Windows (and I am coding since 1974), but my biased opinion is that WSL is targeted for Linux gurus who (sadly for them) have to use Windows. I believe WSL is not targeting Linux newbies.

Converting a Linux Library Project into a Library Project usable in windows

I am attempting to convert the ndn library project found at "https://github.com/named-data/ndn-cpp" into something that can be imported into several existing mfc/wpf/forms Visual Studio Projects (building it as .lib or .dll would work).
As a note, it appears to have previously been converted into c# for windows for a older build of ndn, but is no long supported and will not connect with the current ndn network.(https://github.com/named-data/ndn-dot-net)
I have looked into the using the WSL features that they have added to windows 10, and the Visual studio Linux Cross Platform projects, but these all seem to only be able to make .exes that will run in windows not a .lib or .dll that can be imported into another project.
I have also look into the shared items project but what I was able to find didn't seem like it would work for what I'm trying to do.
Lastly, I tried using cygwin. I was able to compile and generate the linux style libraries(.a) on my windows 10 machine, but when i attempted to generate windows style dlls off the .o files(gcc -shared -o mydll.dll mydll.o) I ran into a large number of reference errors that I was unable to resolve.
Does anyone have any recommendations on which of these methods I should be using or if I should be attempting some other method entirely?
Does anyone have any good references or examples of how to do this for someone with limited Linux experience?
Thanks
Ok. I've tried going about this several ways now, and here's what I've learned that might be useful to someone else trying to do this and also where I stand so far:
If you have a simple Linux dll that you have written it's possible to compile it as a Windows dll using MSYS2 or MINGW, instructions are here: http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs
If you have something a bit more complicated like the program I'm trying to convert above, there is no quick fix to convert from Linux to windows, however you still might be able to compile your program for windows using Visual Studio.
Here's how you do it:
Download vcpkg from https://github.com/microsoft/vcpkg This is a linux package manager for windows, it will allow you to download windows equivalents to many common Linux packages (for the above I had to download boost and sqlite3)
Create your own unistd.h, here's a link to that: Is there a replacement for unistd.h for Windows (Visual C)?
Get dirent.h for windows, here's a link to that: https://github.com/tronkko/dirent
replace instances of gmtime with _mkgmtime or redefine gmtime as _mkgmtime: timegm cross platform
This got me about 90% of the way there (and from about 13,000 compiler errors to about 30), The rest of the errors so far have been for calls where there is no easy linux to windows conversion and those sections of code have had to be re-written. (In the code above this would be the socket code for the tcp/udp connections in the tranport files, Linux and Windows handle it pretty differently, boost does have a good guide for how to use it's sockets in windows though: https://www.boost.org/doc/libs/1_60_0/more/getting_started/windows.html)
So that's it. Hopefully this helps someone else down the line. I'll be adding more to this answer as I discover new things.

How can use BSDS500 benchmark?

I want to use the Berkeley Segmentation Data Set and Benchmarks 500 (BSDS500) for my segmentation algorithm.
When I run build.m in the source files, I get an error about "sys/times.h doesn't exist in MSVC". There is also another header file that needs ieee754.h.
I googled a lot and find that running the code on Linux may could help, But I doubt. Now I have some questions:
Is there a way to use sys/times.h and ieee754.h in Windows? Does MinGW have these header files?
Has someone run BSDS500 benchmark on windows? How?
The code that comes with BSDS500 is written for MATLAB on Linux or MacOS (these are the POSIX-compatible systems that MATLAB runs on).
If you are on Windows, you can try to use Cygwin to build these binaries. Cygwin adds a POSIX layer in Windows. MinGW by itself doesn't do this, it only provides the GNU tools on Windows. The file sys/times.h is defined in the POSIX standard.
But, IMO, you're better off switching to Linux. Not just for BSDS500, but for everything else too... :)

Function variants between different platforms when working with audio files

I was following a programming example for writing a small application the details of the application itself are not important. The issue or concern here is that within that application the provider is using the POSIX function popen().
The line of code looks like this:
static FILE* fp = popen( "resample mr1789800 r48000 | aplay -fdat 2>/dev/null", "w" );
And they were compiling their code with g++, I don't know what version they were using.
I am working on a Windows 7 x64 platform in Visual Studio 2017 with compiler set to latest drafted standard, all other options are default or standard settings. This is the general scenario.
All of the other code in the providers example is seems to be portable, this is the only piece of code that I've seen that is native to POSIX functionality.
It appears that they are using popen to load a file to an external program and by the looks from the code above it appears that the program is unix based aplay.
If I'm trying to mimic this behavior on Windows using Visual Studio what would be the required tools to use?
Will _popen() do the same thing, meaning are they nearly equivalent? If so, then the second half of the question is if aplay is the program that is opening the audio file, what options are available to me that are for windows that will handle the audio file the same way as the one that is found in a typical Unix type system? If not; what are the differences between the two and where would I go from here?

Importing a GNU/Linux C++ package into Visual Studio on Windows

I have no experience with Linux, so this question might seem weird.
I am trying to use a c++ toolbox which has been developed on Debian GNU/Linux Lenny. According to the authors it should work on any recent Linux distribution. However, I need to import it into Visual Studio on Windows.
I have no idea how to get this package configured and built on Visual Studio. I see that there is a "configure" file in the package, but I don't how to use it on Windows. Any help would be appreciated.
You can use either MinGW or Cygwin to mock a *nix system enough to build most *nix software. The configure script is generally a (*nix) shell script which will check to see what interfaces and libraries are available to the application during compilation.
Depending on the library you may not be able to use it outside of a unix library, especially if the library depends on the POSIX (*nix) APIs beyond the very small set which Windows provides.
From your information the package is meant for linux.
This means that if the package contains precompiled code (such as .so or .a - shared libraries / static libraries)you will not be able to run it under windows no matter what (make abstraction of Linux simulated enviroments, I believe you want to run it on a Windows environment).
If the package contains only sources, then you can include the package inside your solution (I would personally go with a static library approach) and try to compile it. You will get some errors when linux specific headers are included (for example linux threading headers).
After finding the windows equivalent for those functions you will be able to compile and use the package on windows as well.
You basically have to do a manual port.