Using readelf we can separate the data part from elf file(using shell)Is it possible to do the same with a C program?
Use can use libelf for this purpose.
http://mdsp.googlecode.com/files/libelf-by-example-20100112.pdf
readelf itself is program written in C. So the answer is yes.
If you are on a debian-like linux distribution, you can probably get the source of readelf by typing apt-get source binutils and see how it is done.
Related
I am trying to print all the Undefined function calls from a shared object file along with file name.
I tried with "nm" command, It print all the undefined function calls .But could not get the file name.
Example:
bash$ nm -u my_test.so
:
U _ZNSs4_Rep20_S_empty_rep_storageE##GLIBCXX_3.4
:
Environment : Ubuntu 18.04 , X86 Arch (Intel processor)
Study in details the specification of the DWARF format (which is the format used by debugging information on Linux). So you could extract the information (but it is not exactly simple) by parsing the DWARF inside your ELF binary.
Consider looking inside the source code of Ian Taylor's libbacktrace. It is doing this extraction of file name from DWARF inside ELF.
Perhaps your real problem is getting precise backtrace information, and then that libbacktrace is exactly what you need!
You might also use gdb : it is extensible and scriptable in Python (or Guile) and you could write your own specialized script.
Perhaps you'll better solve your real problem with some GCC plugin working when you compile your code.
Read How to write shared libraries by Drepper and read more about ELF.
You could for example collect all the undefined symbols in your shared library using nm (or readelf). Then a second script will find the occurrences of these in your source code. It could be even a simple awk script (or some for shell loop using grep), or something as sophisticated as a GCC plugin.
Your example shows (probably) a mangled C++ name. You could use nm -C to get it unmangled. And later write a GCC plugin to find all the GIMPLE CALL instructions using it.
Writing a GCC plugin may take some time, in particular if you are not familiar with GCC internals.
I am looking for the unix command to display the header portion in Hex for any excutable that has been compiled by the cc compiler.
I had this once and now I cant remember it.
I just want to see what the compiler code that is at the start of any c programs that I compile
I am aware that I can use 'hexdump [filename]' however that doesnt isolate the header portion .
Hope i have explained myself well enough.....
The command readelf is available on most Linux systems and has the ability to display many parts of an ELF file. You can use readelf -H to get a short synopsis of the various options.
To get just the file header you can use readelf -h or readelf --fileheader to display the file header.
To see it in hex, you can use the command xxd. Given that the elf header is 64 bytes (on a 64-bit machine), you can use xxd -l 64
Objdump command in Linux is used to provide thorough information on object files. This command is mainly used by the programmers who work on compilers, but still its a very handy tool for normal programmers also when it comes to debugging. In this article, we will understand how to use objdump command through some examples.
Basic syntax of objdump is :
objdump [options] objfile...
There is a wide range of options available for this command.
For example, factorial is the c program that I have to compiled.
1.Display object format specific file header contents using
-p option
The following example prints the object file format specific information.
$ objdump -p factorial
Display the contents of all headers using -x option
Information related to all the headers in the object file can be retrieved using the -x option.
objdump -x factorial
I need to do some Trace32 debugging and i need to see the symbols of the IFS image.
I generate the IFS image for my ARM A9 platform but dont know how to generate the ELF file.
can somebody help me out ?
thanks.
IFS is a file system with many binaries and scripts. For trace32, when you try to debug any process (including procnto), you'd need the symbols of that process of interest. There is not such thing as ELF for IFS. It's like asking for the ELF of your harddisk.
Use dumpifs. Example:
dumpifs shell.ifs
check this
the -x option can extract the files specified after the image.
how to separate data part of an elf file using a C program
Thanks
You can browse the source code of elfdump or use the elfio library http://elfio.sourceforge.net/
I have checked the assembler options of GNU assembler as and I didn't find an option to specify the output object file format. If I am using the following command
as -o foobar.o foobar.s
What object file format will I get?
The as manual says that "The GNU as can be configured to produce several alternative object file formats." But how? How can I change it to a ELF format?
Many thanks.
On linux the output from gas is ELF already, it is unlikely you have a version which is building the old a.out format, or that you are using a cross compiler to say build to MachO or PE.
Use a cross-compiler for specific formats (ARM,MIPS etc.)
Get the entire CODESOURCERY toolchain here for free:
www.codesourcery.com/sgpp/lite_edition.html
GoodLUCK!!