How to generate dwarf information? - c++

I'm new to dwarf format and trying to generate dwarf info.
I'm using windows.
I used this command gcc -c --debug=dwarf file_name and it is generating file in binary format.
So is this the right way?
is there any efficient way?
also how can I convert this binary file to readable format?
I need to access only function and variable details, So is there a way to only generate this info?

Related

Getting build-id from elf binary in a compact, script capable form

I want to read a build id from an elf binary for use in a shell script. Manually, I use readeelf -n but this has decoration and I'd like to print the build id and nothing else so I don't have to parse it in the script.
Any ideas?
Regards

How to display .cfg file in the graph form using GCC compiler

steps executed.
Created C/C++ program and kept in CYGWIN64.
gcc -fdump-tree-cfg Hello.c executed. (in CygWin64 command)
Hello.cfg file generated.
After this I am not able to show the Hello.cfg file in the form graphical form.
You can use -fdump-tree-cfg-graph to get a graphviz .dot file, on which you can then use the dot tool from graphviz to convert a to a graphical format.
If you happen to have an X server running, you can also use dot -Tx11 Hello.c.013t.cfg.dot (or whatever the name of the file ands up being) to display the graph directly without converting it to another file format first.

How to store and retrieve data from linux binary file

I'm using arm-none-linux-gnueabi-g++ in order to compile a c++ code that will run on an embedded Linux device.
I'm using the arm-none-linux-gnueabi-g++ under windows and get as output the binary file that will run on the Linux machine.
In order to set the embedded device with a new binary, I need to create an archive file (zip) with the binary file and with some more settings files.
till far it all OK.
I need to automate that so that the archive file will be created automatically at name of the version of the binary file.
Currently, we keep the version as just a simple constant std::string variable in the code. We use that string when printing diagnostic, logging, etc.
How can I read that from the version binary file?
Or may other methods to achieve that goal?
I thought may to store it in some constant place in the binary file and read it from there but really don't know how to do that without making the binary corrupted.
You are creating the file automatically, so I assume you are first compiling it and then making an archive with the resulting binary.
You could store the version in a text file, and #include that file in your code:
const std::string version =
#include "version.txt"
;
In the version.txt:
"version string"
And when making the archive, you can easily parse the version from the text file.
Ville is correct.
You're currently doing it backwards!
Your build system should provide the version to the executable, not the other way around. Once this is fixed, your build system can provide the same version to other elements, such as your ZIP filename.
Ideally the version would be generated from version control autonomously, but you could specify it in the build command if really necessary.
It's possible to pull some string from the binary (think nm, if there's a Windows equivalent), but that's really the reverse way to do it.

How to retrieve flat binary from an executable file in C

How to retrieve a block of binary from .text section in an executable?
I know objcopy can help by using:
objcopy --only-section=.text --output-target binary a.out a.out.bin
But it would be much better if I can realize the same goal within a function call using BFD library. Is there any way to call objcopy using function calls?
You probably are looking for function in binutils / bfd libs. You can find doc at http://www.delorie.com/gnu/docs/binutils/bfd_toc.html and I think the function you are looking for is:
boolean bfd_get_section_contents (bfd *abfd, asection *section,
PTR location, file_ptr offset,
bfd_size_type count);
whose doc can be find at http://www.delorie.com/gnu/docs/binutils/bfd_57.html

How could I query binary's source code version

My environment is Linux CentOS 6.2. And I've a source control system like svn/hg/git etc. My source code is C/C++.
I want to check in the build binary to keep which binary is release to customer.
And I assume build binary's checksum will different when source code changed.
So, I could reverse trace which binary is build from which version.
Is it possible, what's the tricks I must follow?
I've seen some executable display the revision when execute with -version option.
But I'm wonder how to prevent write wrong -version string into the executable.
If I keep a md5.txt and check-in it instead of check in binary.
How could I make sure I can build the same md5 executable again?
Sorry, for clearing my question and preventing another unexpected answer, I prefer a answer like:
Keep a md5sum.txt in scm when release a new version to user.
Keep binary separate from your SCM.
To rebuild the same md5sum binary you should make sure
write symbol into binary when make(eg. by -DVERSION="1.x")
show the VERSION string to user
remove all $Id, that let your SCM run slower.
keep same CPU & OS & compiler & library environment
...
Create strings within a .cpp file as thus:
static const char version[] = "#(#) $Id$";
where $Id$ is obtained from SVN
Use the what command (see the manual page). It will obtain these strings from the binary so you can check.
Is this an executable or a shared library? If the latter, you could export a function that would return the version (number, string, your choice). Then dlopen(), dlsym(), and execute the function.
For executable ELF binaries, you might be able to implant some data in the binary that can be queried using the 'nm' utility.
If you'll use Subversion, SvnRev will do most work for you (no md5 in repos, repo hold sources, binary - resource with revision-id)
For Mercurial, you can get idea for version sting from VersioningWithMake wiki, and in order to get string like result of git describe, instead of simple template {node|short} for HGVERSION you can use something as {latesttag}+{latesttagdistance}:{node|short}, showing (example) 1.3+11:8a226f0f99aa