Annotation of wikisrc/tutorials/how_to_use_thumb_mode_on_arm.mdwn, revision 1.3

1.2       schmonz     1: This howto explains how NetBSD **current** can use thumb mode on ARM architecture (evbarm). 
                      2: 
                      3: #   Introduction 
                      4: 
                      5: While normal ARM instructions are 32 bits wide, thumb provides a subset of ARM instructions in 16 bits, and thus reduces executable sizes both in memory and on filesystem [[1|How to reduce libc size]]. See [Wikipedia](http://en.wikipedia.org/wiki/ARM_architecture#Thumb) for more details. 
                      6: 
                      7: On large codebases like NetBSD, ARM and thumb mode code can, and must, co-exists. This is because some parts of ARM specific code can not be programmed with the 16 bit thumb instructions. Luckily GCC makes this relatively easy with the **thumb-interwork** option. 
                      8: 
                      9: As an overview, most machine independent (MI) C code can be compiled to thumb mode and most machine dependent (MD) C and assembly code can only be compiled to ARM mode. Some parts of the ARM port specific code in NetBSD have support for thumb mode, but most of them do not. 
                     10: 
                     11: NetBSD's CPUFLAGS build variable is used tell the compiler, when an object file is compiled to thumb mode and when thumb interworking needs to enabled. The GCC options are **-mthumb** and **-mthumb-interwork**. By default ARM target binaries are of course compiled to ARM mode. 
                     12: 
                     13: In a large codebase like NetBSD it becomes difficult to manually check if any one object file can be compiled to thumb mode. Luckily brute force works with the help of make option **-k**, as in keep going even one object file does not compile. By compiling whole tree with CPUFLAGS=-mthumb and MAKEFLAGS=-k, all of the build time failing machine dependent object files can be found, and marked with the help of [[Per file build options override]] to be compiled to ARM mode with thumb interworking. 
                     14: 
                     15: 
                     16: Build time failures are of course not the only things can go wrong with thumb support. At run time, some parts of the kernel or userspace are expected to be in ARM mode. In userspace, for example, this means that the dynamic linker (ld.elf_so), userspace locking and C runtime initialization (CSU) parts need to be in ARM mode. 
                     17: 
                     18: #   Userspace 
                     19: 
                     20: Userspace in **current** compiles to userspace, but doesn't work due to a linker bug [2](http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=39668)[3](http://mail-index.netbsd.org/tech-toolchain/2008/10/01/msg000437.html). 
                     21: 
                     22: 
                     23: If the binutils package is upgraded to 2.18.50 from 2.16.1, then thumb mode works for userspace. After this, only a build script marking the ARM and thumb mode files is needed. 
                     24: 
                     25: Following patches do it all for **current** snapshot from Oct 22nd 2008: 
                     26: 
                     27:   * binutils upgrade and tuning for NetBSD in [4](http://koowaldah.org/people/ash/netbsd/thumb_to_current_01/0001-Upgrade-to-binutils-2.18.50.patch) and [5](http://koowaldah.org/people/ash/netbsd/thumb_to_current_01/0002-binutils-don-t-rebuild-bfd.info.patch)
                     28:   * thumb build script [6](http://koowaldah.org/people/ash/netbsd/thumb_to_current_01/0003-A-build-script-for-thumb-userspace.patch)
                     29: 
                     30: 
                     31: After the patches have been applied on top of **current**, the build process continues like this: 
                     32: 
                     33:   * build tools 
                     34:     
                     35:     ./build.sh -U -O obj -m evbarm -j 3 tools
                     36:     
                     37: 
                     38:   * build userspace to thumb, note that the script expects to find tools directory under obj 
                     39:     
                     40:     ./thumb.sh
                     41:     
                     42: 
                     43:   * build a normal ARM mode kernel, where CONFIG is the configuration file name like TISDP2420 
                     44:     
                     45:     ./build.sh -U -O obj -m evbarm -j 3 kernel=CONFIG
                     46:     
                     47: 
                     48: The next step is to verify that the thumb mode userspace works by booting into it. On OMAP 2420 development board this requires setting up a TFTP server for the kernel executable netbsd.bin and then setting up the root filesystem over NFS (and configuring the kernel accordingly). 
                     49: 
                     50: ##   Known problems 
                     51: 
                     52: While the thumb mode userspace boots and most programs and the shell work, some programs may have issues. Known regressions at this time are: 
                     53: 
                     54:   * thumb mode gcc segfaults on target 
                     55:     
1.3     ! sevan      56:     `# cat > test.c << EOF
1.2       schmonz    57:     > int main(void){
                     58:     > return 0;
                     59:     > }
                     60:     > EOF
                     61:     # gcc --verbose -Wall test.c
                     62:     Using built-in specs.
                     63:     Target: arm--netbsdelf
                     64:     Configured with: /usr/src/tools/gcc/../../gnu/dist/gcc4/configure --enable-longtThread model: posix
                     65:     gcc version 4.1.3 20080704 prerelease (NetBSD nb1 20080202)
                     66:      /usr/libexec/cc1 -quiet -v test.c -quiet -dumpbase test.c -auxbase test -Wall s#include "..." search starts here:
                     67:     #include <...> search starts here:
                     68:      /usr/include
                     69:     End of search list.
                     70:     GNU C version 4.1.3 20080704 prerelease (NetBSD nb1 20080202) (arm--netbsdelf)
                     71:             compiled by GNU C version 4.1.3 20080704 (prerelease) (NetBSD nb1 20080.GGC heuristics: --param
                     72:             ggc-min-expand=34 --param ggc-min-heapsize=7808
                     73:     Compiler executable checksum: c67c46e1fc2de869e7015b2c172bd073
                     74:     test.c: In function 'main':
                     75:     test.c:3: internal compiler error: Segmentation fault
                     76:     Please submit a full bug report,
                     77:     with preprocessed source if appropriate.
1.3     ! sevan      78:     See <http://www.NetBSD.org/Misc/send-pr.html> for instructions.`
1.2       schmonz    79:     
                     80: 
                     81: 
                     82:   * thumb mode gdb segfaults on target 
                     83:     
                     84:     # gdb --verbose /bin/cat
                     85:     [1]   Segmentation fault (core dumped) gdb --verbose /bin/cat
                     86:     
                     87: 
                     88: These issues may be solved by upgrading to newer versions of GCC compiler and gdb debugger. 
                     89: 
                     90: Another problem is that newer binutils adds more symbols in thumb mode than older binutils, which makes thumb mode binaries file size larger than the with old binutils and larger than ARM mode binaries [7](http://sourceware.org/ml/binutils/2008-10/msg00140.html). These extra symbols only affect the file size and sections loaded to memory are smaller in thumb mode than in ARM mode. Also, all of these extra sections and symbols can be easily stripped by providing NetBSD build tools with a **STRIPFLAG=-s** build variable, though without [this patch](http://mail-index.netbsd.org/tech-toolchain/2008/10/17/msg000454.html) libraries are not stripped. 
                     91: 

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb