I remember seeing in the past a program that would take any file and generate a C array representing that file as output; it would prevent distribution of a separate file in some cases. Which Unix/Linux program does that?
xxd -i
For large files, converting to text and then making the compiler parse it all over again is inefficient and unnecessary. Use objcopy instead:
objcopy -I binary -O elf32-i386 stuff stuff.o
(Adjust the output architecture as necessary for non-x86 platforms.) Then once you link it into your program, you can access it like so:
extern char _binary_stuff_start[], _binary_stuff_end[];
#define SIZE_OF_STUFF (_binary_stuff_end - _binary_stuff_start)
...
foo(_binary_stuff_start[i]);
hexdump -v -e '16/1 "0x%x," "\n"'
would generate a C like array from stdin, but there is no declaration, no braces or good formatting.
I know this is Unix/Linux question, but anyone viewing this that wants to do the same in Windows can use Bin2H.
The easiest way is:
xxd -i -a filename
Related
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
Say I want to embed a file called data in my C executable.
The result which comes up from google is this linuxjournal page which says use objdump like this
objcopy --input binary \
--output elf32-i386 \
--binary-architecture i386 data data.o
However this is dependent on the architecture of the computer, for example when compiling the object from the previous command it gives i386 architecture of input file 'data.o' is incompatible with i386:x86-64 output and I have to change the arguments.
However with the unix tool xxd, I can simply make a c source code with the data in a unsigned char array and an integer with its length and obtain the same result with device independent compilation commands.
data.o: data.c
gcc -c data.c
data.c: data
xxd -i data > data.c
What is the preferred method and why?
The xxd is not a standard UNIX tool. It is actually part of VIM and is used for implementing its hex editor function. VIM is an optional tool and is not universally available.
The GNU objcopy, on the other hand, is part of GNU binutils and generally is preinstalled on all GNU systems.
In general, when one needs to include a binary file into a program, something simple (as you do with xxd) is preferred over the objcopy. Mainly, for the simple reason that objcopy is heavily under-documented and leaves impression of being an unpolished front-end to the BFD, the underlying library of the binutils. Another reason is that along with the .c file, you can also create the .h file, and make the generated files an integral part of your project.
The article you link already contains a number of examples how to accomplish that. Probably the most popular tool for the purpose is the hexdump, preinstalled on literally all systems. For example, from the top of my head:
# .c
echo 'char data[] = {' > data.c
hexdump -v -e '1/1 "0x%02X,"' < data.bin >> data.c
echo >> data.c
echo '};' >> data.c
echo 'size_t data_size = sizeof(data);'
# .h
echo 'extern char data[];' > data.h
echo 'extern size_t data_size;' >> data.h
When I write C programs in Linux, and then compile them using gcc, I am always curious about where those header files are. For example, where stdio.h is. More generally, where is stdbool.h?
What I want to know is not only where it is, but also how to get those places, for example, using shell command or using the C programming language.
gcc -H ... will print the full path of every include file as a side-effect of regular compilation. Use -fsyntax-only in addition to get it not to create any output (it will still tell you if your program has errors). Example (Linux, gcc-4.7):
$ cat > test.c
#include <stdbool.h>
#include <stdio.h>
^D
$ gcc -H -fsyntax-only test.c
. /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stdbool.h
. /usr/include/stdio.h
.. /usr/include/features.h
... /usr/include/x86_64-linux-gnu/bits/predefs.h
... /usr/include/x86_64-linux-gnu/sys/cdefs.h
.... /usr/include/x86_64-linux-gnu/bits/wordsize.h
... /usr/include/x86_64-linux-gnu/gnu/stubs.h
.... /usr/include/x86_64-linux-gnu/bits/wordsize.h
.... /usr/include/x86_64-linux-gnu/gnu/stubs-64.h
.. /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stddef.h
.. /usr/include/x86_64-linux-gnu/bits/types.h
... /usr/include/x86_64-linux-gnu/bits/wordsize.h
... /usr/include/x86_64-linux-gnu/bits/typesizes.h
.. /usr/include/libio.h
... /usr/include/_G_config.h
.... /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stddef.h
.... /usr/include/wchar.h
... /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stdarg.h
.. /usr/include/x86_64-linux-gnu/bits/stdio_lim.h
.. /usr/include/x86_64-linux-gnu/bits/sys_errlist.h
The dots at the beginning of each line count how deeply nested the #include is.
If you use gcc, you can check a specific file with something like:
echo '#include <stdbool.h>' | cpp -H -o /dev/null 2>&1 | head -n1
-H asks the preprocessor to print all included files recursively. head -n1 takes just the first line of output from that, to ignore any files included by the named header (though stdbool.h in particular probably doesn't).
On my computer, for example, the above outputs:
. /usr/lib/gcc/x86_64-linux-gnu/4.6/include/stdbool.h
locate stdio.h
or
mlocate stdio.h
but locate relies on a database, if you have never updated it
sudo updatedb
you can also enquire gcc to know what are the default directories that are scanned by gcc itself:
gcc -print-search-dirs
During the preprocessing all preprocessor directives will be replaced with the actuals. Like macro expansion, code comment removal, including the header file source code etc...
we can check it by using the cpp - C PreProcessor command.
For example in the command line:
cpp Filename.c
displays the preprocessed output.
One approach, if you know the name of the include file, would be to use find:
cd /
find . -name "stdio.h"
find . -name "std*.h"
That'll take a while as it goes through every directory.
Use gcc -v and you can check the include path.
Usually, the include files are in /usr/include or /usr/local/include depending on the library installation.
Most standard headers are stored in /usr/include. It looks like stdbool.h is stored somewhere else, and depends on which compiler you are using. For example, g++ stores it in /usr/include/c++/4.7.2/tr1/stdbool.h whereas clang stores it at /usr/lib/clang/3.1/include/stdbool.h.
I think the generic path is:
/usr/lib/gcc/$(ls /usr/lib/gcc/)/$(gcc -v 2>&1 | tail -1 | awk '{print $3}')/include/stdbool.h
When I was looking for (on Fedora 25) I used "whereis stdio.h" For me, It was in /usr/include/stdio.h, /usr/shar/man/man3/stdio,3.gx. But when you are looking for the file, use whereis or locate
Use vim to open your source file and put the curses on stdio.h and in normal mode, command 'gf' will let vim open the stdio.h file for you.
'Ctr + g' will let vim display the absolute path of stdio.h
I need a way to analyze output file of my GCC compiler for ARM. I am compiling for bare metal and I am quite concerned with size. I can use arm-none-eabi-objdump provided by the cross-compiler but parsing the output is not something I would be eager to do if there exists a tool for this task. Do you know of such a tool existing? My search turned out no results.
One more thing, every function in my own code is in its own section.
You can use nm and size to get the size of functions and ELF sections.
To get the size of the functions (and objects with static storage duration):
$ nm --print-size --size-sort --radix=d tst.o
The second column shows the size in decimal of function and objects.
To get the size of the sections:
$ size -A -d tst.o
The second column shows the size in decimal of the sections.
The readelf utility is handy for displaying a variety of section information, including section sizes, e.g.:
arm-none-eabi-readelf -e foo.o
If you're interested in the run-time memory footprint, you can ignore the sections that do not have the 'A' (allocate) flag set.
When re-visiting this question 10 years later one must mention the little Python-based wrapper for readelf and nm that is elf-size-analyze:
puncover uses objdump and a few other gcc tools to generate html pages you can easily browse to figure out where your code and data space is going.
It's a much nicer frontend than the text output of the gcc tools.
How can I dump all the global variables and the address offsets in my executable?
This is on os x, app developed with xcode compiled with gcc.
Thank you
If Compiled to Mach-O
Either use otool or the cctools.
If Compiled to ELF
You should be able to do this with objdump and/or readelf.
I don't have a *nix system at hand here, but objdump -s -j .data should be getting you rather close enough.
An easy way to get a list of global variables and their offsets from an executable on macOS (original question) is to use the nm tool. Without any additional keys, it gives the variables and their offsets.
Example:
$ nm <some binary>
...
U __ZTVN10__cxxabiv117__class_type_infoE
U __ZTVN10__cxxabiv120__si_class_type_infoE
000000010006e248 s __ZTVN7testing17TestEventListenerE
000000010006e1c0 s __ZTVN7testing22EmptyTestEventListenerE
000000010006dfa8 s __ZTVN7testing31TestPartResultReporterInterfaceE
000000010006d860 S __ZTVN7testing32ScopedFakeTestPartResultReporterE
000000010006d8d8 S __ZTVN7testing4TestE
...
Check man nm for the explanation of the codes like U, s, S, etc.
In case you also want to look for string constants, there is another tool strings:
$ strings <some binary> -o -t x
will give you a list of string literals and their offsets.
See man strings for more details.