WinDbg: Unable to verify checksum for EXE file - visual-c++

I realize a question with the exact title has already been answered, but the steps there requires running the compiler and linker manually, whereas I want to use cmake.
I am trying to debug a C program with WinDbg. But I'm getting this error:
*** WARNING: Unable to verify checksum for main.exe
Reading a mailing list thread1, I'm guessing I need to add a few flags, namely '/Zi' and '/Release'. But I'm building my project with cmake, and I don't know how to add those flags properly so that I can build my program using GNU toolchain with debug symbols too.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.00)
project(Hello LANGUAGES C)
add_executable(main src/main.c)
With the above cmake file, my program is built properly. Even a pdb file is generated, which is read by WinDbg no problem. But I can't see the line information with .lines and no source file is shown when debugging the EXE; only assembly commands are shown.
After the reading the mail thread (mentioned above), I checked the checksum value of my EXE. It's zero. Now I need to know how to set up a cmake file so it produces EXE with debug symbols with proper checksum.

The checksum-verification warning turned out not to be the issue (it was just a warning after all, not an error). WinDbg didn't load line information. Either it's the default (although I don't know why that would be) or I mistakenly turned it off myself. Whatever the case, here is how you turn it on:
.lines -e
After that, WinDbg was able to bring up the source window by its own accord when I started debugging.

Related

Linux randomly deleted my file while compiling what do I do?

gcc -L/root/Desktop - Wall -o prog3.c -pthread -lcopy
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.0: In function '_start': (.text+0x20): undefined reference to 'main'
collect2: error: ld returned 1 exit status
This is my error code. prog3.c is nowhere to be found, what on earth happened is there any way to get my file back?? The bold is the command I ran and the rest is the resultant console output
Your problem is here: -o prog3.c. gcc’s -o option is used to tell gcc which name it should give to the executable it generates. So here, you’re basically asking your compiler to replace your prog3.c source file by an executable. Sadly your code is gone...
Little addendum of your options in such scenario:
It was Git (or any other version control) repository. In such case, you can simply bring it from previous commit
Your editor/IDE has some back-up system. Sometimes I need to bring back a file I've thought was needless. For such case, my favourite text editor should have create already back-up file in appropriate location (e.g. $XDG_DATA_HOME/vim/backup in my case).
If none of above, but you still have previously correctly compiled binary file
You can try to decompile, but this process - even if successful - isn't lossless (e.g. code is basically spaghetti).
Had you compiled with -g flag, you could possibly retrieve the code from debug info.
You can at least de-assemble to Assembly code.

CMake save stripped debug information

It's a usual practice to compile with debug symbols and then separate the binary using objcopy into the release executable and the file with debug information (then wrap that into separate packages or store on the symbol server).
How to separate debug symbols properly in CMake? I've seen just some discussions and incomplete code samples.
Platform is Linux and GCC.
CMake doesn't have direct support for this, but you can use some POST_BUILD and INSTALL steps to achieve the result you want. It is, however, worth noting that using objcopy isn't the only way to do this sort of thing. You can also make use of the build-id and this may well be easier to implement robustly with CMake.
Rather than repeat the whole thing here, there's a pretty good description of your choices and the methods that was posted to the CMake mailing list few years ago by Michael Hertling. I'll just pick out the working alternative here for reference, but I recommend reading that link. There's also an even more complete discussion of the two alternatives in the GDB documentation which should fill in any remaining blanks about the two approaches (debug link versus build-id). Here's Michael's general build-id approach (the build-id is explicitly given in his example, read the referenced articles for an explanation of what it is expected to represent):
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(BUILDID C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(BUILDID "abcdef1234")
STRING(SUBSTRING "${BUILDID}" 0 2 BUILDIDPREFIX)
STRING(SUBSTRING "${BUILDID}" 2 8 BUILDIDSUFFIX)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
SET_TARGET_PROPERTIES(main PROPERTIES
LINK_FLAGS "-Wl,--build-id=0x${BUILDID}")
ADD_CUSTOM_COMMAND(TARGET main POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:main>
${CMAKE_BINARY_DIR}/main.debug
COMMAND ${CMAKE_STRIP} -g $<TARGET_FILE:main>)
INSTALL(FILES ${CMAKE_BINARY_DIR}/main.debug
DESTINATION ${CMAKE_BINARY_DIR}/.build-id/${BUILDIDPREFIX}
RENAME ${BUILDIDSUFFIX}.debug)
Configure with CMAKE_BUILD_TYPE==debug and build; subsequently, invoke
gdb -ex "set debug-file-directory ." -ex "file main"
from within CMAKE_BINARY_DIR, and you will read "no debugging symbols
found" as expected. Now, issue "make install", re-invoke gdb and read:
"Reading symbols from .../.build-id/ab/cdef1234.debug"
As you can see, the debug info file is connected with the stripped
executable solely by the build ID; no objcopy in sight.
The above makes use of the fact that the .debug file is expected to be a normal executable with debug info not stripped.

GDB - Loading Debug information from external ".sym" files

I am attempting to carry out postmortem analysis of a crashed binary, "TestApp", on a linux system.
I have a copy of the binaries and and shared objects that are copied onto the device in a path:
/usr/public/target
This folder contains all the binaries in question in the directory structure used on the system under test, ie:
/usr/public/target/sbin/TestApp
/usr/public/target/lib/TestAppLib.so
/usr/public/target/usr/lib/TestAppAPILib.so
The automated build process strips the debug information from the binaries, and stores them in external, symbol files, all under:
/usr/public/target_external_symbols
So the symbol information for the above binaries would exist in files named:
/usr/public/target_external_symbols/sbin/TestApp.sym
/usr/public/target_external_symbols/lib/TestAppLib.so.sym
/usr/public/target_external_symbols/usr/lib/TestAppAPILib.so.sym
How do I get GDB to be aware of the existence of these external symbols and to load them?
I typically invoke GDB via:
gdb TestApp TestApp.core
I've referred to other articles on creating a test .gdbinit file and passing it to GDB via the -command argument, but it doesn't appear to work. Every time I attempt to get a backtrace from my core file, I get an indication from GDB that it cannot open the debug symbols. Any help in resolving this is appreciated.
(gdb) info shared
From To Syms Read Shared Object Library
0x78000000 0x780061e8 Yes (*) /usr/public/target/lib/TestAppLib.so
0x78010000 0x7806e60c Yes (*) /usr/public/target/usr/lib/TestAppAPILib.so
0x78070000 0x78091d2c Yes (*) /usr/public/target/lib/libm.so.2
(*): Shared library is missing debugging information.
Thank you.
There are commands set debug-file-directory, symbol-file and add-symbol-file to load debugging symbols from within a gdb session. The latter one might require the address where the shared library was loaded into memory.
Maybe during your build process 'gnu debuglinks' have been added to your binaries. This would mean that in the executables there's a path coded that directs gdb where to look for debug symbols. More can be found here.

Compiling alsa-lib with Buildroot: C preprocessor fails sanity check

Trying to compile a Linux image for LPC3250 ARM using Buildroot.
Stock settings, it compiles fine.
I'm getting this error when I try to add alsa-lib and alsa-utils:
configure: error: in `/home/user/projects/buildroot/output/build/alsa-lib-1.0.26 ':
configure: error: C preprocessor "/home/user/projects/buildroot/output/host/usr/ bin/arm-buildroot-linux-uclibcgnueabi-cpp" fails sanity check
See `config.log' for more details
Excerpt of alsa-lib's config.log. It kinda sucks because half the error messages have been cut off for some reason:
/home/user/projects/buildroot/output/host/usr/arm-buildroot-linux-uclibcgnueabi/sysroot/usr/include/features.h:219:5: error: #error It appears you have defined _FILE_OF$
In file included from /home/user/projects/buildroot/output/host/usr/arm-buildroot-linux-uclibcgnueabi/sysroot/usr/include/stdio.h:72,
from conftest.c:12:
/home/user/projects/buildroot/output/host/usr/arm-buildroot-linux-uclibcgnueabi/sysroot/usr/include/bits/uClibc_stdio.h:61:2: error: #error Sorry... uClibc was built wi$
In file included from conftest.c:12:
/home/user/projects/buildroot/output/host/usr/arm-buildroot-linux-uclibcgnueabi/sysroot/usr/include/stdio.h:83: error: expected '=', ',', ';', 'asm' or '__attribute__' $
In file included from conftest.c:12:
/home/user/projects/buildroot/output/host/usr/arm-buildroot-linux-uclibcgnueabi/sysroot/usr/include/stdio.h:721: error: expected declaration specifiers or '...' before $
/home/user/projects/buildroot/output/host/usr/arm-buildroot-linux-uclibcgnueabi/sysroot/usr/include/stdio.h:723: error: expected ';', ',' or ')' before '*' token
Similar question here says something about "unsetting" CPP and CPPFlags.
Not sure where I would do this or check for it within the Buildroot environment. I'm not sure if this is even the same issue. I'm guessing its not related since in that other question it's talking about compiling GCC, which I'm not doing.
My #1 hunch right now is that I have to edit the configure file for lib-alsa. I'm just not sure what edits to make.
Edit: Did some aggressive Googling and came up with what I think is the full version of one of my errors: ...features.h:329:2: #error It appears you have defined _FILE_OFFSET_BITS=64. Unfortunately, uClibc was built without large file support enabled.
Checked my uClibc config file and UCLIBC_HAS_LFS=y is present. ???
The root of the issue here is Buildroot is using the uClibc config file located at: output/toolchain/uClibc-0.9.33.2/.config
rather than toolchain/uClibc/uClibc-0.9.33.config as specified in the Buildroot configuration menu.
I ran make uclibc-menuconfig (which defaults to saving the configuration in .config) and selected General Library Settings->Large File Support and everything compiled just fine.
Well, output/toolchain/uClibc-0.9.33.2/.config is generated from toolchain/uClibc/uClibc-0.9.33.config when Buildroot configures and builds uClibc. So it doesn't make any sense to use output/toolchain/uClibc-0.9.33.2/.config as the uClibc configuration. You should leave the default of toolchain/uClibc/uClibc-0.9.33.config.
Regarding Large File Support, normally all the packages have a proper dependency on it. So if a package needs large file support, the package cannot be selected until you enable large file support in Buildroot. If it is not the case, then it is a bug, and the Buildroot community would be interested in having a bug report about this (which details the Buildroot version being, the Buildroot configuration file being used, and the build error that you get).
Now, if Large File support is indeed needed, you should enable it at the Buildroot level rather than at the uClibc level. If you enable it only at the uClibc level, then Buildroot will not now about this, and will not show you all the packages that have a dependency on large file support.
So, what you should do is enable the BR2_TOOLCHAIN_BUILDROOT_LARGEFILE configuration option in Buildroot, and then do a complete rebuild: make clean all.

Get rid of "gcc - /usr/bin/ld: warning lib not found"

I have the following warning during link:
/usr/bin/ld: warning: libxxx.so.6, needed by /a/b/c/libyyy.so, not found (try using -rpath or -rpath-link)
Setting environment variable LD_LIBRARY_PATH=path_to_libxxx.so.6 silence the warning (adding -Lpath_to_libxxx.so.6 doesn't help).
I have a separate compilation server, where the resulting binary is only compile.
The binary is executed on other server and there the libxxx.so.6 is seen by the binary (checked with ldd executable).
Is there're other way to get rid of the warning at compilation time (I have it several times and it's very annoying)?
You need to add the dynamic library equivalent of -L:
-Wl,-rpath-link,/path/to/lib
This will cause the linker to look for shared libraries in non-standard places, but only for the purpose of verifying the link is correct.
If you want the program to find the library at that location at run-time, then there's a similar option to do that:
-Wl,-rpath,/path/to/lib
But, if your program runs fine without this then you don't need it.
Make sure the paths to the needed libraries are known to the runtime linker. This is done by adding a file in /etc/ld.so.conf.d/ with the needed path. For example, /etc/ld.so.conf.d/foo with the following contents:
/usr/local/lib/foo/
If you have a very old Linux version, /etc/ld.so.conf.d/ might not be supported, in which case you might have to add the paths directly into the /etc/ld.so.conf file.
After you've done that, you need to update the linker's database by executing the "ldconfig" command.
I know this is old, but here's a better fix:
The root cause:
The problem actually happens when LD invoked by GCC starts resolving
library dependencies. Both GCC and LD are aware of the sysroot
containing libraries, however LD may be missing one critical
component: the /etc/ld.so.conf file. Here’s an exampleld.so.conf file
from a Raspberry PI system:
include /etc/ld.so.conf.d/*.conf
The /etc/ld.so.conf.d directory contains the following files:
00-vmcs.conf:
/opt/vc/lib
arm-linux-gnueabihf.conf:
/lib/arm-linux-gnueabihf /usr/lib/arm-linux-gnueabihf
libc.conf:
/usr/local/lib
The universal solution
 
The problem can be easily solved by copying the LD configuration files
to a location where the cross-toolchain’s LD can find them. There’s
one pitfall however: if your cross-toolchain was built with MinGW
(most are), it probably did not have access to the glob() function so
it won’t be able to parse a wildcard-enabled include statement
like *.conf. The workaround here is to just manually combine the
contents of all .conf files from /etc/ld.so.conf.d and paste them
into /etc/ld.so.conf
*/opt/vc/lib
/lib/arm-linux-gnueabihf
/usr/lib/arm-linux-gnueabihf
/usr/local/lib*
Once you create the ld.so.conf file in the correct folder, your
toolchain will be able to resolve all shared library references
automatically and you won’t see that error message again!
The only way to silence these warning using command line options would be the -L flag which curiously does not work for you (maybe you can post more details on this). Since the warning is generated by ld we could try to use -Wl,option to disable a linker warning but from the documentation of GNU ld however there is no option for (de)activating this warnings.
So this leaves us with writing a wrapper script filtering out this warning or compile a custom version of ld.

Resources