Sunday, November 29, 2009

Cross Toolchains : PART 1 (Binutils)

Components of crosstoolchain
===================

1. Binutils
2. Kernel header
3. C library
4. Compiler [e.g gcc]

optional : gdb debugger etc

Binutils
=====
The target architecture needs to have its own set of tools to generate binary files for it. Also it would be nice to have some tools to enable examining these.

ld ---> linker

strip --> to strip the object file of a particular section. e.g strip the comments to reduce the code size.
      strip [ -F BFDNAME | --target=BFDNAME ]
[ -I BFDNAME | --input-target=BFDNAME ]
[ -O BFDNAME | --output-target=BFDNAME ]
[ -s | --strip-all ] [ -S | -g | --strip-debug ]
[ -K SYMBOLNAME | --keep-symbol=SYMBOLNAME ]
[ -N SYMBOLNAME | --strip-symbol=SYMBOLNAME ]
[ -x | --discard-all ] [ -X | --discard-locals ]
[ -R SECTIONNAME | --remove-section=SECTIONNAME ]
[ -o FILE ] [ -p | --preserve-dates ]
[ -v | --verbose ] [ -V | --version ] [ --help ]
OBJFILE...

e.g strip --remove section=.comment

as --> assembler
objdump
objcopy
readelf: very useful tool. I invariably use it with -a switch to retrieve the max info.

nm
: lists all the symbols in an object file.

===========
Extract from IBM's website

objdump & nm

The commands objdump and nm both display information about object files. If a crash occurs and a corefile is produced, these commands help you analyze the file.

objdump
Use this command to disassemble shared objects and libraries. After you have discovered which library or object has caused the problem, use objdump to locate the method in which the problem originates. To invoke objdump, type: objdump
nm
This command lists symbol names from object files. These symbol names can be either functions, global variables, or static variables. For each symbol, the value, symbol type, and symbol name are displayed. Lower case symbol types mean the symbol is local, while upper case means the symbol is global or external. To use this tool, type: nm

You can see a complete list of options by typing objdump -H. The -d option disassembles contents of executable sections

Run these commands on the same machine as the one that produced the core files to get the most accurate symbolic information available. This output (together with the core file, if small enough) is used by the IBM® support team for Java™ to diagnose a problem.

=================================

addr2line : very important for debugging. E.g lets say you get a core dump with a backtrace. You can use the backtrace info to go to the code directly

void test_funk()
{
printf("#NEMESIS: testing funky func \n");
printf("#NEMESIS: address of test_funk [%p]", &test_funk);
}

Output: in the output the addess printed is 0x845c. Pass this as an input to the addr2line.]

nemesis@nemesis-laptop:~/test_code$ arm-none-linux-gnueabi-addr2line 0x845c
/home/adikumar/test_code/test_3.c:5 ===> Lo and behold.You have the line in the code.


strings: Lists all string in an object file. use it with grep to find a particular string

nemesis@nemesis-laptop:~/codesourcery/arm-2009q3/bin$ arm-none-linux-gnueabi-strings test2
/lib/ld-linux.so.3
libgcc_s.so.1
__aeabi_unwind_cpp_pr0
__gmon_start__
_Jv_RegisterClasses
__aeabi_unwind_cpp_pr1
libc.so.6
abort
printf
__libc_start_main
GCC_3.5
GLIBC_2.4
###!Nemesis: LittleEndian =========> my prints
###!Nemesis: BigEndian =========> my prints
0x%x

No comments: