Cross compiling and Linking

  • Compilation
    arm-uclinuxeabi-gcc  -g  -Os   -g2 -mthumb -mcpu=cortex-m3 -fsigned-char -O2 -fno-builtin-puts -fno-common -ffixed-r8 -D__KERNEL__ -I -fno-builtin -ffreestanding -isystem  -pipe  -DCONFIG_ARM -D__ARM__ -D<MACRO="(VALUE)"> -Wall -Wstrict-prototypes -fno-stack-protector   -o   -c
    
  • De-assemble
    arm-uclinuxeabi-objdump -S <O_FILE>
    
  • Library
    arm-uclinuxeabi-ar crv <A_FILENAME> <O_FILES> 
    
  • Linking
    arm-uclinuxeabi-ld -Bstatic -T<LDS_FILENAME> -u<UNDEFINED_SYMBOLS> <O_FILES> <A_FILES> -lgcc -Map <MAP_FILE> -o <OUT_FILENAME>
    

    Example linker script:

    OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
    OUTPUT_ARCH(arm)
    ENTRY(main)
    
    MEMORY
    {
     /* SmartFusion internal eSRAM */
     ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64k
    }
    
    SECTIONS
    {
     .text :
     {
       . = ALIGN(4);
        *(.text*)
    	. = ALIGN(4);
        _etext = .;
     } >ram
    }
    end = .;
  • Other format output
    arm-none-eabi-objcopy --gap-fill=0xff -O ihex myprog  "myprog.hex"
    arm-none-eabi-objcopy --gap-fill=0xff -O binary myprog  "myprog.bin"
    arm-none-eabi-objcopy -O srec myprog "myprog.srec"
    arm-none-eabi-objdump -h -S myprog > "myprog.lst"

Linux Kernel – Tools/Scripts

Here are few tools/scripts which are useful for static analysis of code:

  • Checkpatch
    # ./script/checkpath.pl <patch_file(s)>
    # ./script/checkpatch.pl --file <file_name(s)>
    
  • kmemleak
    Check kernel memory leak using kmemleak:

    # cppcheck <source-dir>

    Kmemleak usage

  • Coccicheck
    • make coccicheck MODE=<mode>

    Where,

    ‘patch’ proposes a fix, when possible.

    ‘report’ generates a list in the following format: file:line:column-column: message

    ‘context’ highlights lines of interest and their context in a diff-like style.Lines of interest are indicated with ‘-‘

    ‘org’ generates a report in the Org mode format of Emacs.

    • To apply Coccinelle to a specific directory, M= can be used.
      For example, to check drivers/net/wireless/ one may write:
    make coccicheck M=drivers/net/wireless/
    • To apply Coccinelle on a file basis, instead of a directory basis, the
      following command may be used:

      make C=1 CHECK="scripts/coccicheck"
    • To check only newly edited code, use the value 2 for the C flag, i.e.
      make C=2 CHECK="scripts/coccicheck"
  • kselftest
    • Run kselftest install tool in tools/testing/selftests
       # cd tools/testing/selftests
       # ./kselftest_install.sh [ install_location ]
    • Default install tool: in tools/testing/selftests
    • Specify install location:
    # ./kselftest_install.sh /tmp
    • Kselftest install creates run_kselftest.sh
    • Run tests:
# cd install_dir
# ./run_kselftest.sh

Saving file with sudo without reopening

It happens many time that you open file in vim, make major changes and when you try to save, you realize that you needed to open it as a root.

Well, you do not need to reopen that file and you can save it as sudo with below command:

:w !sudo tee %

Hit enter after typing that in, you will be prompted for a password if required and then you will then get the following message.

Press ENTER or type command to continue

Press ENTER and assuming you actually made some changes, you will then get the following message.

Press ENTER or type command to continue
W12: Warning: File "/etc/vim/vimrc" has changed and the buffer was changed in Vim as well
See ":help W12" for more info.
[O]K, (L)oad File:

Press L and quit the file.

That’s all!

Ask for confirmation before running bash command

Sometimes we need to confirm or give warning before running any command.

In my case, I need to check JAVA version before building android source.
Android L needs to be built with Java 1.7 and Android KK needs to be built with Java 1.6. So it is required to change JAVA version while switching between Android K and L.

If we forget to change JAVA version, it tries to build source with improper version consuming lots of time (as java version changes, it tries to rebuild all the things) and results in failure at some point.

To avoid this, I updated bash script so that before build command it checks for JAVA version and asks user if it is proper or not. Upon confirmation from user it proceeds further.

Below is script I wrote and included in bashrc using “source” command at end of bashrc.


#!/bin/bash

function jdkmsg {
echo -e "\e[38;5;82m"
echo "########################################################################"
echo -e "\tHope You are using correct JDK version\n"
echo -e "JAVA_HOME = $JAVA_HOME"
java -version
javac -version
echo -e "You can change java and javac versions using below commands:"
echo -e "sudo update-alternatives --config java and"
echo -e "sudo update-alternatives --config javac"
echo "########################################################################"
echo -e "\e[39;5;82m"
}

debug_trap () {
	if [[ "$BASH_COMMAND" == *"build.sh"* ]]
	then
		jdkmsg
		echo "Allow?"
		select choice in yes no
		do
			if [ "$choice" = "yes" ]
				then break
			elif [ "$choice" = "no" ]
			then return 1
			fi
		done
	fi
}

shopt -s extdebug
trap debug_trap DEBUG