gnu arm7t micro cross

Upload: tnath20051972

Post on 30-May-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Gnu Arm7t Micro Cross

    1/28

    Getting Started with GNU:

    A Tutorial Introduction Using the ARM Evaluator-7T

    William Gatliff

    Table of ContentsCopyright................................................................................................................................3Why GNU? ............. .............. .............. .............. .............. ............... .............. .............. .............3Why not GNU? ............. .............. .............. .............. ............... .............. .............. .............. ......4If GNU tools are free, then why buy a CD?.....................................................................4So why arent we buying a CD?.........................................................................................5Building GNU Tools.............................................................................................................5Compiling and Analyzing a Program...............................................................................9

    Debugging a "Hello, World!" Program .............. ............... .............. .............. .............. ....14Graphical Debugging ........................................................................................................18What about an IDE?............................................................................................................20What Next? ............. .............. .............. .............. .............. ............... .............. .............. ...........21Commercial GNU Offerings.............................................................................................23Resources..............................................................................................................................25About the Author................................................................................................................26

  • 8/9/2019 Gnu Arm7t Micro Cross

    2/28

  • 8/9/2019 Gnu Arm7t Micro Cross

    3/28

    If you have been following any news related to embedded systems developmentrecently, you will have heard that GNU tools are growing increasingly popular formainstream embedded systems development. To say that they are just now becoming

    popular for all types of embedded development would be untrue GNU tools have been used to produce embedded applications since their creation, but havent seenwidespread use in the greater embedded community until recently.This article provides you an opportunity to evaluate GNU tools for embedded de-velopment in a low-cost setting using either the GNU debuggers ARM instructionset simulator, or the ARM Evaluator-7T single board computer. The Evaluator-7T isa good choice for an introductory GNU experience, because the ARM family of chipsis well-supported by GNU, and this inexpensive board in particular comes ready-to-play with the entire GNU development tool chain, including the GNU debugger.The following paragraphs discuss some of the advantages and disadvantages of us-ing GNU tools for embedded development, then dive right in and show you how toset up and explore your own GNU development environment using a simple "hello,world!" program. I then run the program on the GNU debuggers ARM instruction

    set simulator, and on the Evaluator-7T board. Finally, I conclude with links to sourcesof pre-congured commercial GNU tool distributions that support lots of other hard-ware platforms, and a list of resources you can turn to for even more information.This article is scheduled to appear in the December 2001 Issue of Circuit Cellar Ink 1Magazine.

    CopyrightThis article is Copyright (c) 2001 by Bill Gatliff. All rights reserved. Reproduction forpersonal use is encouraged as long as the document is reproduced in its entirety, in-cluding this copyright notice and author contact information. For other uses, contactthe author.

    Why GNU?One of the obvious advantages of the GNU family of development tools is their price.By virtue of their distribution license, source code for GNU tools like the compiler,linker and debugger can be had at virtually no cost by simply downloading themfrom the Free Software Foundation 2 (FSF)s GNU website.What you will nd if you go this route, however, may disappoint you. The sourcecode distribution packages do not come precongured for any of GNUs supportedtargets, so you must congure and build them yourself. Furthermore, the base tooldistributions do not include a graphical Integrated Development Environment (IDE) tofacilitate point-and-click code generation or debugging. Such tools certainly do exist, but they are not part of the base GNU tool distributions. You will see the explanation

    for this later on, when we discuss the GNU debugger in detail.Once you have the GNU development tools congured and installed, you will dis-cover that you have at your disposal a powerful and exible foundation of productsthat can be used to deliver just about any kind of embedded solution, regardless of how arcane or extreme your needs might be. GNU tools are highly adaptable, andcan be tailored to provide solutions that few competitive commercial offerings can:debugging over CAN and other atypical communications links, quick replacement of internal libraries like oating point emulation, and ne-grained, precise control overlinker memory assignments are but a few of the possibilities.GNU tool exibility extends beyond the target domain, however. No other embed-ded toolchain in existence, proprietary or otherwise, supports as many combinations

    3

  • 8/9/2019 Gnu Arm7t Micro Cross

    4/28

    Getting Started with GNU:

    of host and target environments as GNU. Whether your development workstation isa Linux PC, a Solaris Sparcstation, or even a Win32 machine, GNU probably runs onit and runs exactly the same as on any other host. Heterogenous development en-

    vironments are especially attractive for GNU, because build management and auto-mated debugging scripts will move consistently and transparently between systemswith minimal or no modications whatsoever.Perhaps the most surprising thing of all, however, is how well GNU tools are sup-ported. An active embedded GNU user base keeps in almost contact via a collectionof mailing lists, frequently-asked-questions documents and websites, and in manycases the authors of the tools themselves offer valuable assistance by providing ex-planations helpful examples. To illustrate: I have consistently found that if I ask anintelligent question, I can expect to get an intelligent answer back in less than a daygenerally less than four hours. Thats an impressive statistic for any help desk, butan especially impressive one for a help desk that costs nothing to use.

    Why not GNU?Alongside their long list of strengths, the GNU tools have a short list of weaknesses.The rst two are that GNU tools arent well documented for widespread embed-ded use, and they arent "shrinkwrapped" to the point that new users can get startedquickly. In response, a few vendors offer CDs containing precongured GNU setupsthat work with popular development hardware. There is also a growing list of con-sultants specializing in embedded GNU, who can provide reasonably-priced helpduring all phases of a development effort. The resources section at the end of thisarticle includes a few links to this and other kinds of information.Another drawback of the GNU world is that a switch to it from a commercial, closeddevelopment environment may incur some serious headaches or, even worse, be anall-or-nothing proposition. Many proprietary embedded development tool and li- brary vendors do not publish specications for their le formats, so you usually cant

    directly import outputs from their tools into a GNU system. In other words, if a ven-dor of a closed-source library of code wont provide the library in a format that theGNU linker understands, then youre sunk before youve even left the harbor. Dittofor the communications protocol used by your hardware emulator.One much-hyped limitation of the GNU tools that isnt really a limitation at all is thenotion that you cannot use GNU tools to produce proprietary, closed-source prod-ucts. This is simply untrue, and in fact most embedded GNU users are using the toolsin this way, and doing so legally. The real limitation, if you want to call it that, isthat you cannot incorporate libraries of code licensed under the GNU General PublicLicense 3 (GPL) into applications that remain proprietary to the end user. For more onthis, you should read the text of the GPL and related licenses as they appear on theFree Software Foundations website. And while you are doing that, you should alsoread the licenses for the tools you are using today you may nd that youre alreadyagreeing to some terms that are just as surprising.Keep in mind that the Free Software Foundation is not the only place to look forlibraries of downloadable, GNU-compatible materials. The Internet is literally burst-ing with source code, released under licenses varying from "must use in Open Sourceapplications only", to "come and get it." Graphics libraries, boot loaders, diagnostictools, and even entire operating systems like Linux 4 and eCos 5 can be easily foundafter just a few minutes with your favorite search engine.

    4

  • 8/9/2019 Gnu Arm7t Micro Cross

    5/28

    Getting Started with GNU:

    If GNU tools are free, then why buy a CD?It is important to understand that the Free Software Foundation doesnt exist to pro-vide development tools at zero cost. Their mission is really to provide developmenttools and other code in a way that preserves the freedoms of the end users of thatcode the zero cost part is just one side effect of that. For example, their belief is thatif you have purchased an operating system for your workstation, then you shouldget the source code for it as well, so that you may do with it as you wish.A useful analogy is that of an automobile: when you buy a car, the automaker doesnttell you where you may and may not drive with it. The FSF provides licenses like theGPL to enable software developers to pursue a similar philosophy if they so choose.The FSF website has a more detailed presentation of how their licenses and toolsrelate to their mission.When you buy a CD containing FSF products, you demonstrate support of the FSFsgoals. Your dollars go directly to the FSF if you purchase directly through them, orin some cases indirectly through the CD vendor in the form of a "portions of theproceeds" donation. But even if the CD vendor does not contribute nancially to theFSF, the FSF still gets something out of the transaction: another satised GNU user,another supporter of their philosophy. Any way you slice it, the FSF wins as doyou.Purchasing a CD also gets you some support time with the vendor, which can comein handy if you have troubles with installation, or when you set out to adapt the toolsto run on your own hardware. Usually this adaptation process is straightforward, buthaving someone around who knows the tools in detail is always an advantage.Finally, buying a CD usuallymakes business sense. One vendor listed in the resourcessection at the end of this article offers a product containing precongured GNU toolsfor most of the targets that GNU supports. Despite its bulk, the price is very reason-able: about the same as a couple of hours of an engineers salary. Unless you have alot of prior GNU experience, creating a basic GNU installation by hand in a businesssetting will be more expensive than their CD.

    So why arent we buying a CD?Well, an article that says "just go buy a CD" would be really short, and probably notvery interesting or compelling.There is educational value in setting the tools up by hand, particularly if you can doit in a hobbyists environment where the business-case arguments arent applicable.The GNU development tool installation process is very similar to the process used toinstall other GNU and related tools, so going through the procedure manually a fewtimes is a training exercise that pays dividends down the road, when you start usingand adapting GNU tools for more specialized use.The main reason were doing things by hand, however, is that there is no better wayto get a thorough look at what the GNU tools are all about than by digging aroundand getting a little dirt under our ngernails, so to speak. So lets get started already!

    Building GNU ToolsIn the next few sections, were going to set up an compiler, runtime library, assem- bler, linker and debugger that can be used to produce and run applications for theARM Evaluator-7T development board. Well subsequently prove that by buildingand testing a simple, "hello, world!"-style application on the ARM microprocessor.

    5

  • 8/9/2019 Gnu Arm7t Micro Cross

    6/28

    Getting Started with GNU:

    The rst step in the process is to get the materials we need. The instructions willassume that you are working in one of two host environments: either a Linux orother unix-like workstation, or on a Win32 PC (preferably Windows NT) running a

    unix emulation environment called Cygwin6

    . If you install Cygwin to try out thisarticle, then Use the default installation setup.Some commercial GNU tool distributions include a copy of Cygwin. If you haverecently purchased a Windows-compatible GNU setup, check to make sure it doesntalready include Cygwin before downloading another copy.If you are trying to run Cygwin on Windows 95 or 98, expect to have some difcultycompleting the procedure were about to start. Cygwin occasionally has difcultyproviding a solid emulation in these environments, and when things start to lookstrange you will have to simply reboot and try again. Once you get the tools built,however, they run ne on these machines. Cygwin is very stable on Windows NTand Windows 2000.You will need an ARM Evaluator-7T board 7 if you want to try out the "hello, world!"program on real hardware. The Evaluator-7T is available from ARM, Inc.s website 8.If you dont want to invest in hardware just yet, then stick with me anyway: the GNUdebugger comes with an excellent ARM instruction set simulator that can emulatemuch of what the Evaluator-7T can do, and in some cases even more, without anyexternal hardware at all.Table 1 lists the les you will need, and shows where to download them from. 9 Therewill probably be later versions of each of these packages by the time you get aroundto downloading them, but stick with the listed versions until youve completed theprocess at least once; updates occasionally change the installation procedure.Note also that if you use Microsofts Internet Explorer to download these, then youmust right-click on the download link and select "Save Link As". Internet Explorer of-ten has trouble with les in the GNU tar le format, and will corrupt them if you letit. If you are using Cygwin and cannot nd the les after you download them, then

    move the downloaded les to a directory likec:\cygwin\home\ < adminstrator >

    (replace administrator with your username, if necessary). This is Cygwins "home di-rectory", which should be right where you can nd them when you type ls from theCygwin shells command prompt.As you might imagine, the Free Software Foundations website stays busy. If youhave trouble getting a connection, use one of the mirror sites listed at:http://www.gnu.org/order/ftp.html 10.

    Table 1. What you need.

    binutils 11 ftp://ftp.gnu.org/gnu/binutils/binutils-2.11.2.tar.gz 11

    gcc11 ftp://ftp.gnu.org/gnu/gcc/gcc-2.95.3.tar.gz 11

    newlib 11 ftp://sources.redhat.com/pub/newlib/newlib-1.9.0.tar.gz 11

    gdb 11 ftp://sources.redhat.com/pub/gdb/releases/gdb-5.0.tar.gz 11

    6

  • 8/9/2019 Gnu Arm7t Micro Cross

    7/28

    Getting Started with GNU:

    gdb patchle 11 http://www.billgatliff.com/articles/gnu/gdb-5.0-rdi.diff 11

    The binutils package includes the GNU assembler and linker, and the gcc packagecontains the GNU C/C++ compiler. Newlib is the C runtime library we will use tosupply functions like printf() , and the gdb package contains the GNU debugger.

    Setting up the build environmentSet up a directory structure in which to build and install the tools. Then, set up someenvironment variables that will save typing later and make sure that the argumentsused during the conguration process are consistent throughout a source of com-mon errors. Figure 1 describes the commands, just type them in as they appear there(omit the leading $, which represents the Cygwin or unix command prompt).

    $ cd$ mkdir build-binutils$ mkdir build-gcc$ mkdir build-newlib$ mkdir build-gdb$ mkdir install$ export TARGET=arm-elf$ export PREFIX=pwd/install$ export PATH=$PATH:$PREFIX/bin

    Figure 1. Setting up the build environment.

    In the PREFIX setting, the ticks surrounding the pwd command are backticks, oftenfound on the tilde (~) key of a standard US-101 keyboard layout. You can see the

    result of this command by typing the following:$ set | grep PREFIX

    The value of TARGET is an argument that species the ARM microprocessor versionof the tools, using the ELF debugging format. PREFIX species the topmost direc-tory under which the GNU tools will be installed. The last command adds the GNUinstallation directory to the search path, so that we can run the tools after theyreinstalled.Next, decompress the source code for the tools themselves.

    $ tar xzvf binutils-2.11.2.tar.gz$ tar xzvf gcc-2.95.3.tar.gz$ tar xzvf newlib-1.9.0.tar.gz

    $ tar xzvf gdb-5.0.tar.gz

    Building the binutils packageThe commands to congure and install the assembler and linker are shown Figure 2.These commands do the following:

    Run the congure script included with the binutils package, to set up the sourcecode to build for the ARM target.

    7

  • 8/9/2019 Gnu Arm7t Micro Cross

    8/28

    Getting Started with GNU:

    Invoke the make program to actually compile the and install binutils. The outputfrom make is captured and stored in the le make.log .

    When this process nishes, you will see a number of programs in ${PREFIX}/bin .The assembler is arm-elf-as , and the linker is arm-elf-ld . arm-elf-objcopy is a utilitythat translates les to different formats (from ELF to S Record, for example), and arm-elf-objdump is a utility that can dissect the components of a le, to show you thingslike symbol addresses and a disassembly of the les object code. Well discuss theseutilities later.

    $ cd build-binutils$ ../binutils-2.11.2/configure -target=$TARGET -prefix=$PREFIX$ make all install 2>&1 | tee make.log$ cd ..

    Figure 2. Instructions to build binutils

    Building a bootstrap cross compilerNow that we have an assembler and linker, we can build and install the GNU C/C++compiler. The initial step in the procedure yields a bootstrap compiler that can only beused to build runtime libraries and header les; we will use this compiler to build thearm-elf version of the newlib C runtime library. Once thats complete, we will rebuildthe compiler completely, including internal libraries that need target-specic headerles from newlib in order to be compiled properly.The commands to make the boostrap compiler are shown in Figure 3. Do them now.

    $ cd build-gcc$ ../gcc-2.95.3/configure -target=$TARGET -prefix=$PREFIX \

    -with-newlib -without-headers -with-gnu-as \-with-gnu-ld -disable-shared -enable-languages=c

    $ make all-gcc install-gcc 2>&1 | tee make.log$ cd ..

    Figure 3. Instructions to build a bootstrap gcc.

    At this point we have a bootstrap compiler called arm-elf-gcc , located in ${PRE-FIX}/bin . Now we will build newlib, then return to nish building gcc.

    Building the newlib C runtime libraryThe procedure, shown in Figure 4, should seem pretty familiar by now.

    $ cd build-newlib$ ../newlib-1.9.0/configure -target=$TARGET -prefix=$PREFIX$ make all install 2>&1 | tee make.log$ cd ..

    Figure 4. Instructions to build newlib.

    8

  • 8/9/2019 Gnu Arm7t Micro Cross

    9/28

    Getting Started with GNU:

    Building a complete cross compilerNow that we have ARM header les and libraries from newlib, we can build a com-plete cross compiler setup for C/C++ development. The steps are shown in Figure5.

    $ cd build-gcc && rm -rf *$ ../gcc-2.95.3/configure -target=$TARGET -prefix=$PREFIX \

    -with-gnu-as -with-gnu-ld -enable-languages=c,c++$ make all install 2>&1 | tee make.log$ cd ..

    Figure 5. Instructions to build gcc.

    Building the debugger

    Are we there yet? Almost. The last step in the setup process is to build and install thedebugger. The procedure is shown in Figure 6 .

    $ patch -p0 < gdb-5.0-rdi. diff$ cd build-gdb$ ../gdb-5.0/configure -target=$TARGET -prefix=$PREFIX$ make all install 2>&1 | tee make.log$ cd ..

    Figure 6. Instructions to build gdb.

    This procedure is slightly different from what you have already seen, in that it in-cludes a patch step. This step corrects a minor source code problem that preventsgdb from building properly for the arm-elf target.

    Like most GNU programs, the GNU debugger is under constant development, andsometimes is released with minor issues that affect one or two of its many supportedtargets. When such problems are resolved, a patchleis usually released that contains just the differences between the updated version and the original version; this letsyou get the improvement by downloading just a few bytes of data, instead of down-loading the whole package again.The patchle is plaintext; you can easily read it with a text editor to see the changesit contains. In fact, thats exactly what the patch program does.

    If you run out of disk spaceAll told, you will need about 500MB of free disk space in order to build a completeGNU cross development environment. But you dont that much all at once: if space istight, you can delete the les left at the end of each step in the procedure before youmove on to the next step. For example, once you nish building the cross assemblerand linker, you can delete the contents of the build-binutils directory.The installed tool set requires less than 100MB of disk space.

    9

  • 8/9/2019 Gnu Arm7t Micro Cross

    10/28

    Getting Started with GNU:

    Compiling and Analyzing a ProgramNow that we have shiny new tools, lets try them out. Using the text editor of yourchoice, create a le hello.c that looks like Figure 7.

    #include < stdio.h >

    int a_global = 10;

    int foo ( int arg ){

    static int foo_counts = 0;

    foo_counts++;return foo_counts + arg;

    }

    int main ( void )

    { int nloops;char* charstr = "hello, world!\n";

    for( nloops = 0; nloops < 10; nloops++ ) {printf( "%s", charstr );foo( nloops );

    }

    return 0;}

    Figure 7. The hello.c program.

    Compiling a programYou compile hello.c into an ARM executable le called hello , like this:

    $ arm-elf-gcc -Wall -g -o hello hello.c

    or, like this:

    $ arm-elf-gcc -v -Wall -g -o hello hello.c

    The rst command quietly compiles, assembles and links the hello application; thesecond command is more verbose, which is useful when you are trying to nd outwhere gcc is looking for header les or linker command les. The -Wall optioncauses gcc to emit warnings for almost every kind of suspicous code construct itnds, and the -g option includes debugging information in the output le.If you are using the arm-elf tools under some versions of Cygwin, the compilersoutput le is called hello.exe , instead of just hello . The le itself is still an ARMexecutable, however, and you can conrm that using the le command. You can alsorename hello.exe , using the mv command.

    $ file hello.exehello.exe: ELF 32-bit LSB executable, Advanced RISC Machines ARM, ver-sion 1,statically linked, not stripped

    $ mv hello.exe hello

    10

  • 8/9/2019 Gnu Arm7t Micro Cross

    11/28

    Getting Started with GNU:

    Some other useful variations on how to invoke arm-elf-gcc are:

    $ arm-elf-gcc -S hello.c

    to compile to assembly language and then stop (the output le is hello.s ), and:

    $ arm-elf-gcc -O3 -o salutations -Wl,-Tlinker.cmd hello.c goodbye.c

    to combine the modules hello.c and goodbye.c into a single, highly optimized ap-plication, linked using instructions found in a linker command le called linker.cmd .The resulting executable is named salutations .You will want to avoid supplying optimization commands like -O3 during compila-tion of source code modules you intend to debug. The GNU compiler is an aggres-sive optimizer, and if you try to step through optimized code you will nd that somesource lines have moved, some have changed, and some have gone away entirely.Optimized code still produces the results you expect it to, but not necessarily in theway you expect it to; this makes it nearly impossible to debug using linear techniqueslike line-by-line stepping.

    Using objdumpThe utilities objdump and objcopy are two of many hidden gems of the GNU toolfamily. Objdump cracks apart the contents of object and executable les to displaytheir contents; objcopy translates object and executable les to different formats.To get an overall view of the structure of our new hello program, invoke objdumplike this:

    $ arm-elf-objdump -headers hello

    The output, shown in Figure 8, packs a lot of information into a small space. It starts by stating that hello is an ARM executable le, coded using the ELF le format inlittle-endian mode.

    hello: file format elf32-littlearm

    Sections:Idx Name Size VMA LMA File off Algn

    0 .text 00008880 00008000 00008000 00008000 2**2CONTENTS, ALLOC, LOAD, READONLY, CODE

    1 .rodata 00000230 00010880 00010880 00010880 2**2CONTENTS, ALLOC, LOAD, READONLY, DATA

    2 .data 0000085c 00010bb0 00010bb0 00010bb0 2**2CONTENTS, ALLOC, LOAD, DATA

    3 .ctors 00000008 0001140c 0001140c 0001140c 2**2CONTENTS, ALLOC, LOAD, DATA

    4 .dtors 00000008 00011414 00011414 00011414 2**2CONTENTS, ALLOC, LOAD, DATA

    5 .sbss 00000000 0001141c 0001141c 0001141c 2**0CONTENTS

    6 .bss 000000e0 0001141c 0001141c 0001141c 2**2ALLOC

    7 .debug_aranges 00000540 00000000 00000000 0001141c 2**0CONTENTS, READONLY, DEBUGGING

    8 .debug_pubnames 00000af5 00000000 00000000 0001195c 2**0CONTENTS, READONLY, DEBUGGING

    11

  • 8/9/2019 Gnu Arm7t Micro Cross

    12/28

    Getting Started with GNU:

    9 .debug_info 00020938 00000000 00000000 00012451 2**0CONTENTS, READONLY, DEBUGGING

    10 .debug_abbrev 00003098 00000000 00000000 00032d89 2**0CONTENTS, READONLY, DEBUGGING

    11 .debug_line 0000a400 00000000 00000000 00035e21 2**0CONTENTS, READONLY, DEBUGGING

    12 .stack 00000000 00080000 00080000 00040221 2**0CONTENTS

    Figure 8. Output from objdump headers.

    Next, objdump lists the contents of all the memory sections in the le. The rst sec-tion is called .text, and contains the machine instructions that make up the program.The section is 882CH (hex, or 34860 decimal) bytes long, and starts at address 8000H.The sections Virtual Memory Address and Load Memory Address are the same,which means that the linker placed the contents of the section at the physical ad-dress in which the code was intended to run. This will almost always be the case for.text sections of programs that do not implement or use virtual memory.

    The next line states that the linker marked the .text section with the attributes CON-TENTS , ALLOC , LOAD, READONLY and CODE. Put simply, this means that if an operatingsystem tries to load this le into memory, it must allocate memory for the .text sectionand copy the contents of this part of the hello le into it.The .rodata section contains the programs read-only data. In addition to the "hello,world!" text, this section also includes some string and constant data dened by theruntime library. The .data section holds the programs initialized global variables,which includes a_global and, again, some declarations from the runtime library.The .ctors and .dtors sections are lists of function pointers to global C++ object con-structors and destructors. If hello had been a C++ application with global objects,these sections would be larger. As it is, they only include a single entry: a null pointerthat tells the runtime librarys startup code that there are no constructors or destruc-tors to run.

    The .sbss and .bss sections hold the programs uninitialized global data. They lackthe LOAD attribute because there is nothing in the hello le to load: an runtimeprogram loader only needs to allocate initialized memory at the sections address forthe program to use.With the exception of the .stack section, the remaining sections contain debugginginformation. They do not affect the size of the executable image, although they dosignicantly affect the size of the hello le itself.The .stack section could be used to reserve memory for the programs runtime stack.In hello , however, this section is empty apparently the program loader or appli-cations startup code allocates its stack in some other way than by having the linkerreserve memory for it.To disassemble the executable contents of the hello le (the .text section), use obj-

    dumps -disassemble command. The output is long, naturally, so the example pipesthe output through the less utility:

    $ arm-elf-objdump -disassemble hello | less

    hello: file format elf32-littlearm

    Disassembly of section .text:

    00008000 :8000: e3a00016 mov r0, #22 ; 0x16

    12

  • 8/9/2019 Gnu Arm7t Micro Cross

    13/28

    Getting Started with GNU:

    8004: e28f10c4 add r1, pc, #196 ; 0xc48008: ef123456 swi 0x00123456800c: e59f00bc ldr r0, [pc, #bc] ; 80d0 8010: e590d008 ldr sp, [r0, #8]8014: e590a00c ldr sl, [r0, #12]

    ...

    Figure 9. Output from objdump disassemble.

    The output, shown in Figure 9, says that there is a function called _mainCRTSStartuplocated at address 8000H, and its rst instruction is mov r0,#22 , whose raw binaryrepresentation is the byte string E3A00016H . Trying to be helpful, objdump translatesthe decimal value of the opcodes operand to hex and puts it in a comment after theinstruction.A few instructions later, objdump detects another opcode that may be difcult todecipher in its raw form. The operand for the ldr instruction at address 800CH endsup being the value 80D0H , which is the same thing as an offset of D0H bytes fromthe start of _mainCRTStartup . Whether or not this additional information is helpfuldepends on what the programmer intended this instruction to do.To see source code intermixed with assembly code, use the -source option:

    $ arm-elf-objdump -source hello | less

    ...

    80d8: 000114fc streqd r1, [r1], -ip80dc: 00010bc0 andeq r0, r1, r0, asr #2380e0: 000000ff streqd r0, [r0], -pc

    000080e4 < foo > :80e4: e1a0c00d mov ip, sp80e8: e92dd800 stmdb sp!, {fp, ip, lr, pc}80ec: e24cb004 sub fp, ip, #4 ; 0x480f0: e24dd004 sub sp, sp, #4 ; 0x4

    int a_global = 10;

    int foo ( int arg ){

    80f4: e50b0010 str r0, [fp, -#16]static int foo_counts = 0;

    foo_counts++;80f8: e59f2028 ldr r2, [pc, #28] ; 8128 < foo+0x44 >80fc: e59f3024 ldr r3, [pc, #24] ; 8128 < foo+0x44 >8100: e59f2020 ldr r2, [pc, #20] ; 8128 < foo+0x44 >

    8104: e5921000 ldr r1, [r2]8108: e2812001 add r2, r1, #1 ; 0x1810c: e5832000 str r2, [r3]

    return foo_counts + arg;8110: e59f3010 ldr r3, [pc, #10] ; 8128 < foo+0x44 >8114: e5932000 ldr r2, [r3]8118: e51b1010 ldr r1, [fp, -#16]811c: e0823001 add r3, r2, r18120: e1a00003 mov r0, r38124: ea000000 b 812c < foo+0x48 >8128: 00010cc4 andeq r0, r1, r4, asr #25

    }812c: e91ba800 ldmdb fp, {fp, sp, pc}

    13

  • 8/9/2019 Gnu Arm7t Micro Cross

    14/28

    Getting Started with GNU:

    ...

    Figure 10. Output from objdump source.

    You will need to look a few hundred lines into the output, shown in Figure 10, beforeseeing any source code, because the beginning of the .text section contains instruc-tions from the runtime librarys startup code, which is written in assembly language.For more about objdump , use its -help option:

    $ arm-elf-objdump -help

    ...arm-elf-objcopy: supported targets: elf32-littlearm elf32-bigarm

    elf32-little elf32-big srec symbolsrec tekhex binary ihex

    Using objcopyTo translate our ELF hello le into a Motorola S Record le, use objcopy :

    $ arm-elf-objcopy -output-target srec hello hello.srec

    To see what other formats objcopy understands, use its -help option:

    $ arm-elf-objcopy -help

    Debugging a "Hello, World!" Program

    Enough stalling already, lets run some code.

    Using the ARM instruction set simulatorIf you dont have the Evaluator-7T, or you just dont want to set it up yet, you can usethe GNU debuggers ARM instruction set simulator to do all the following withoutany ARM hardware at all. The simulator is so complete, in fact, that even printf()works properly the output shows up on the GDB console.Typical of GNU tools, the debugging procedure is almost exactly the same whetheryou are using the instruction set simulator or real hardware. I will provide two setsof instructions in places when there is a difference.

    Using the ARM Evaluator-7T boardIf you intend to use real hardware, unwrap your new Evaluator-7T board and plugit in now. Connect the serial cable to the DEBUG port on the board, and connect theother end to a serial port on your PC. Note the serial port you connect it to, as thedebugger will use that port in a moment to control the board. Dont do anythingwith the CDs included with the Evaluator-7T packaging, theyre ARMs proprietary(albeit highly rated) development environment and arent needed here.

    14

  • 8/9/2019 Gnu Arm7t Micro Cross

    15/28

    Getting Started with GNU:

    Running gdbChange to the directory containing the hello executable, and use the following com-mand to launch the GNU debugger from the command prompt on your workstation.

    $ arm-elf-gdb hello

    If you are using the ARM instruction set simulator, then tell gdb to establish a con-nection to it:

    (gdb) target sim

    If you are using the Evaluator-7T board, tell gdb to establish a connection to it instead.The command used here will vary depending on what kind of host machine you areusing, and what serial port youve plugged into. For Cygwin hosts, the commandswill look something like this:

    (gdb) set remotebaud 57600(gdb) target rdi com1 (or com2, or ...)

    For a Linux or other unix-like host, the commands will look something like this:

    (gdb) set remotebaud 57600(gdb) target rdi /dev/ttyS0 (or /dev/ttyS1, or ...)

    If you are using a unix host and you get a permission error on the serial port, thenyou need to change either your userids group membership, or the permissions onthe serial port device node. Contact your system administrator if you have one, orsimply log in as the root user and use the chmod command:

    $ s u -< password >

    # chmod 777 /dev/ttyS0 /dev/ttyS1# exit

    You may see an "unimplemented message" error when gdb establishes the connectionto the target board. This is normal it just means that the boards rmware has a fewlow-level commands that gdb doesnt recognize, or that the target board lacks somecommands that gdb understands. During the connectio process, gdb will negotiatewith the target board until the two agree on the set of commands that are available.Once gdb has established a connection with the instruction set simulator or Evalua-tor board referred to uniformly as the debugging target tell it to download yourapplication to the target in preparation for debugging:

    (gdb) load

    Set a breakpoint at main() :

    (gdb) break mainBreakpoint 1 at 0x8140: file hello.c, line 14.

    Now launch the application. If you are running the instruction set simulator, use therun command. If you are using the Evaluator-7T, use the continue :

    (gdb) run

    or,

    15

  • 8/9/2019 Gnu Arm7t Micro Cross

    16/28

  • 8/9/2019 Gnu Arm7t Micro Cross

    17/28

    Getting Started with GNU:

    (gdb) nexthello, world!21 foo( nloops );(gdb) stepfoo (arg=35704) at hello.c:6

    The output of the next command is the result of the printf() function call, whichwe did not step into. The step command steps into the foo() function call, reportsthe arguments to foo() , and reports its location in the source code.Remember that if you just press ENTER , gdb will repeat your previous command.So if you want to step over several lines, you can just do this:

    (gdb) step(gdb) < ENTER >(gdb) < ENTER >

    To see a disassembly of a function, use the disassemble command:

    (gdb) disassemble main0x80e4 < main > : mov r12, sp0x80e8 < main+4 > : stmdb sp!, {r11, r12, lr, pc}0x80ec < main+8 > : sub r11, r12, #4 ; 0x40x80f0 < main+12 > : sub sp, sp, #8 ; 0x80x80f4 < main+16 > : bl 0x8214 < __gccmain >0x80f8 < main+20 > : ldr r3, [pc, #3c] ; 0x813c < main+88 >0x80fc < main+24 > : str r3, [r11, -#20]0x8100 < main+28 > : mov r3, #0 ; 0x00x8104 < main+32 > : str r3, [r11, -#16]0x8108 < main+36 > : ldr r3, [r11, -#16]0x810c < main+40 > : cmp r3, #9 ; 0x90x8110 < main+44 > : ble 0x8118 < main+52 >0x8114 < main+48 > : b 0x8134 < main+80 >0x8118 < main+52 > : ldr r0, [pc, #20] ; 0x8140 < main+92 >0x811c < main+56 > : ldr r1, [r11, -#20]

    To disassemble just the instruction the processor is about to execute, use the examinememory (x) command, with the instruction (/i) option:

    (gdb) x/i $pc0x8178 < main+72 > : ldr r3, [r11, -#16]

    To step one processor instruction, use the stepi command. In situations where mul-tiple processor instructions are associated with a single C statement, the programcounter value shown at the far left of the output changes even though the source linedisplayed stays the same.

    (gdb) stepi0x817c 19 for( nloops = 0; nloops < 10; nloops++ ) {

    (gdb) < ENTER >0x8180 19 for( nloops = 0; nloops < 10; nloops++ ) {

    When combined with the display command, you can use the stepi command towatch the processor step through assembly code:

    (gdb) display/i $pc2: x/i $pc 0x817c < main+76 > : add r2, r3, #1 ; 0x1(gdb) stepi0x8180 19 for( nloops = 0; nloops < 10; nloops++ ) {2: x/i $pc 0x8180 < main+80 > : str r2, [r11, -#16](gdb) < ENTER >

    17

  • 8/9/2019 Gnu Arm7t Micro Cross

    18/28

    Getting Started with GNU:

    2: x/i $pc 0x8184 < main+84 > : b 0x8154 < main+36 >

    To see the value of a particular register, use the print command. To prevent clasheswith similarly-named C objects, prex register names with $ .

    (gdb) print $r3

    To see information on all the registers, use the info command:

    (gdb) info registers

    To see the value that a call to foo() would return, simply invoke foo() from thedebugger. The called function is actually run by the debugging target, so any side-effects produced will remain:

    (gdb) print foo(10)$4 = 12(gdb) print foo(10)$4 = 13

    If you get lost in your application, and want to nd out where you are, use the back-trace command:

    (gdb) backtrace#0 foo (arg=35704) at hello.c:6#1 0x8178 in main() at hello.c:21

    To nd out about any commands you may have missed, use the help command:

    (gdb) help

    If you can only remember part of a command, or just the description of the command,use the apropos command to search for it:

    (gdb) apropos print

    Finally, when you are done, quit.

    (gdb) quit

    You will usually need to reset the Evaluator-7T board before starting another debug-ging session.

    Graphical DebuggingBy now you may have concluded that although gdb is functional, it isnt very pretty.And trying to debug code that interacts with complicated data structures using acommand-line interface can be tedious at best. Is this the best that GNU can do?Of course it isnt. Like many GNU tools, gdb is a command line application because itis used as a foundation for other applications, including several graphical debugginginterfaces. All the point-and-click you could ever want in a debugger is available,and from a variety of sources.Screenshots for two popular graphical debugging frontends to gdb are shown in Fig-ure 11 and Figure 12. The rst is from a program called Insight 11, an enhanced gdb

    18

  • 8/9/2019 Gnu Arm7t Micro Cross

    19/28

    Getting Started with GNU:

    that can run on both Win32 (Microsoft) and X Windows (everything else) worksta-tions. See the resources section for more information about Insight.

    Figure 11. The Insight debugger.

    The second screenshot is from an application called DDD 12, which uses a command-line gdb running in the background to do its work. This may sound clumsy, butit has tremendous benet: the same binary DDD application works for all versionsof the GNU debugger, regardless of the target chosen. In contrast, Insight must becongured and installed for each version of the debugger that you intend to use.

    19

  • 8/9/2019 Gnu Arm7t Micro Cross

    20/28

    Getting Started with GNU:

    Figure 12. The Data Display Debugger.

    DDD gets its graphics by way of X Windows, which means that it doesnt work on

    Win32 hosts without extra software. See the resources section for more informationabout DDD.The approaches to graphical debugging taken by Insight and DDD each have theiradvantages and disadvantages, and you will probably nd that you routinely move between all three options (Insight, DDD, and the plain-vanilla gdb) depending onwhat you are trying to accomplish at the time.

    What about an IDE?There are several freely-available packages for complete point-and-click develop-ment environments. Like the strategy used to enhance gdb with graphical capabil-ities, all of these options use the standard GNU compiler and other tools behind thescenes which is why it is important to know how to set the basic tools up rst.One option is called VIDE 13. This package contains features you would expect in anintegrated development environment, including a graphical editor, project les, and build management. A screenshot of VIDE is shown in Figure 13.

    20

  • 8/9/2019 Gnu Arm7t Micro Cross

    21/28

    Getting Started with GNU:

    Figure 13. Screenshot of VIDE

    Another option, albeit one that takes some work to set up for embedded use, isKDevelop 14. This environment is currently in widespread use on Linux hosts to writeand maintain applications for the KDE 15 window manager, and is an extremely pow-erful and full-featured IDE for producing native applications. Because it uses GNUtools behind the scenes, however, it can be retasked for embedded duty by tweakingsome conguration les.A screenshot of KDevelop in action appears in Figure 14.

    Figure 14. Screenshot of KDevelop

    What Next?There are several subjects you will want to learn more about after you have been in-

    21

  • 8/9/2019 Gnu Arm7t Micro Cross

    22/28

    Getting Started with GNU:

    troduced to GNU tools. Four of the important ones are the compilers inline assemblylanguage syntax, the linkers command lesyntax, how to enhance the C runtime li- brarys support for functions like malloc() and printf(), and how to construct a debugging

    agent that lets gdb talk to your own hardware. Well hit the high points here, you canrefer to the resources listed at the end of this article for more in-depth information.

    Inline assembly languageTo jam an assembly language instruction into the middle of a program ow, the GNUsyntax looks like this:

    fo r ( x = 0 ; x < 10 ; x++ ) {foo(); ___asm__("nop");

    }

    Inline assembly language has the risk of corrupting the program environment if reg-

    ister values are changed in unexpected ways. To avoid this danger, the GNU com-piler provides an extended inline assembly language syntax that includes operandconstraints that tell the compiler both how the instruction is using registers, and howthose registers can be mapped to objects in C:

    int one; __asm__( "mov %0, #1", "=r" (one));

    This instruction tells gcc to move the immediate value #1 into the register occupied by the integer variable one . If one isnt actually contained in a register (i.e. maybe itis actually found on the stack), then gcc will insert additional instructions before andafter the inlined opcode to get the value #1 into whatever representation the compileruses for one .Gccs online documentation tells more about how to use operand constraints in inline

    assembly language constructs.

    Linker command syntaxThe essence of a typical linker command le looks something like the text in Figure15. This excerpt starts loading the contents of the .text memory section at address8000H, and includes in that section all the contents of the .text, .text.*, .stub and othersections from the applications object les. Any empty spaces in the section created by alignment adjustments are lled with zeros.

    ...

    SECTIONS{

    . = 0x8000;.text :

    {*(.text)*(.text.*)*(.stub)*(.gnu.warning)*(.gnu.linkonce.t.*)*(.glue_7t) *(.glue_7)

    } =0

    22

  • 8/9/2019 Gnu Arm7t Micro Cross

    23/28

    Getting Started with GNU:

    PROVIDE (__etext = .);PROVIDE (_etext = .);

    ...

    Figure 15. An excerpt from a linker command le.

    The PROVIDE statement creates symbols that programs can use to nd out aboutthe applications memory layout at runtime. In the example, the symbols __etextand _etext are allocated at the end of the .text section.This excerpt illustrates only a fraction of the GNU linkers capabilities. See its exten-sive online help documentation for more information.

    Newlib stubsThe C runtime library we are using does not come equipped to talk to the Evaluator-7Ts hardware. To add this functionality, newlib provides hooks that your applicationcan connect to in order to catch the text coming out of a printf() call, detect matherrors, and implement lesystem-like behavior for functions like fopen() .There are seventeen such hooks in newlib, with names like _write_r and _sbrk_r .By supplying reentrant functions with these names in the application, you can morecompletely integrate newlibs capabilities into an embedded system.

    Debugging stubsGdb works ne when the hardware is as ready-to-play as the Evaluator-7T. But whatif you want to run a debugging session on custom hardware? To do this, you needa debugging agent in the target system that knows how to communicate with the de- bugger running on your workstation. In the GNU debugger world, this debugging

    agent is often called a debugging stub.A few example stubs are included with the GNU debuggers source code, in leswith names ending in "-stub". A more extensive and extensible list of stubs is housedin the gdbstubs 16 project. By modifying these stubs to suit your hardware, you enablegdb to talk to your application as it runs in your system.

    Commercial GNU OfferingsI mentioned earlier that a handful of companies offer precongured and ready-to-goversions of GNU tools for all kinds of embedded processors. The following mentionstwo of them. Note that I am not an employee of either of these companies, and I hold

    no nancial interests in them. Furthermore, the following is not a comprehensive list:I only describe products that I have current, favorable experience with.

    GNUProAt the high end, you cant get much better than Red Hat, Inc.s 17 GNUPro 18 distribu-tion. This product contains the latest-and-greatest versions of everything GNU hasto offer, thoroughly tested and maintained by the company that employs more GNUdevelopers (embedded and otherwise) than anyone else in the world. And becauseRed Hat has immediate access to such a large mindshare of the GNU community,

    23

  • 8/9/2019 Gnu Arm7t Micro Cross

    24/28

    Getting Started with GNU:

    they can and do offer a level of support that you simply cannot nd anywhereelse.

    Figure 16. Red Hat, Inc.s GNUPro GNU Distribution.

    But this level of attention comes at a cost. A GNUPro distribution starts at about $20kUSD, including unlimited support for up to ten users. For organizations contemplat-ing aggressive customizations of GNU tools, this is a reasonable and pragmatic sumof money; for someone who just wants an introduction to GNU or who only has basicneeds, however, the value package is a bit overkill.

    X-ToolsFortunately, there is an option on the low end. A recent, and somewhat unique offer-ing is from a company called Microcross, Inc. 19 whomarkets asingle CD containing GNU tools for almost every target that GNU supports. In addi-tion, they supply an informative and well-written manual, a graphical installer, PDFversions of all available GNU documentation, and a copy of the VIDE integrateddevelopment environment. To keep costs low, support and other services are soldseperately you dont pay for what you dont want.

    Figure 17. Microcross, Inc.s X-Tools GNU Distribution.

    24

  • 8/9/2019 Gnu Arm7t Micro Cross

    25/28

    Getting Started with GNU:

    The Microcross CD, called GNU X-Tools 20, makes sense for someone who wants anintroductory look at using GNU tools for embedded work and doesnt want to getmired in the details of setting up the tools themselves. X-Tools is also a good choice

    for someone who knows they will be working with multiple target architectures, orsomeone who doesnt need any service or support now but thinks that they mayat some point in the future. Finally, X-Tools is especially appropriate in situationswhere the development environment must be a Win32 host: X-Tools comes in bothWindows and Linux avors, which lets you avoid the pain of building GNU tools onsuboptimal hosts like Windows 98.

    Resources

    http://www.billgatliff.com 21

    Additional information on GNU programming for embedded systems.

    http://sources.redhat.com/ml/crossgcc 22

    The CrossGCC mailing list archives.

    http://www.arm.com 23

    Information on the ARM7T microprocessor, and the Evaluator 7T single-boardcomputer.

    http://sources.redhat.com/cygwin 24

    The Cygwin unix emulation package.

    http://sources.redhat.com/binutils 25

    The Binutils homepage.

    http://gcc.gnu.org 26

    The GNU Compiler Collection homepage.

    http://sources.redhat.com/gdb 27

    The GNU debugger homepage.

    http://sources.redhat.com/newlib 28

    The Newlib homepage.

    http://sources.redhat.com/insight 29

    The Insight homepage.

    http://www.gnu.org/software/ddd 30

    25

  • 8/9/2019 Gnu Arm7t Micro Cross

    26/28

    Getting Started with GNU:

    The DDD homepage.

    http://www.objectcentral.com/ 31

    The developers of VIDE.

    http://kdevelop.kde.org 32

    The KDevelop homepage.

    http://www.microcross.com 33

    The Microcross, Inc. website; they make X-Tools.

    http://www.microcross.com/Support/Docs/docs.html 34

    Microcrosss GNU documentation tree.

    http://www.redhat.com 35

    Red Hat, Inc.s website; they make GNUPro.

    http://www.gnu.org 36

    The Free Software Foundations GNU website.

    http://sourceforge.net/projects/gdbstubs 37

    Homepage for the gdbstubs project.

    About the AuthorBill Gatliff is an independent consultant with almost ten years of embedded develop-ment and training experience. He specializes GNU-based embedded development,and in using and adapting GNU tools to meet the needs of difcult developmentproblems. He welcomes the opportunity to participate in projects of all types.Bill is a Contributing Editor for Embedded Systems Programming Magazine 38, amember of the Advisory Panel for the Embedded Systems Conference 39, maintainerof the Crossgcc FAQ,creator of the gdbstubs 40 project, and a noted author and speaker.

    Bill welcomes feedback and suggestions. Contact information is on his website, athttp://www.billgatliff.com 41.

    Notes1. http://www.circuitcellar.com2. http://www.fsf.org3. http://www.gnu.org/licenses/licenses.html#GPL4. http://www.kernel.org

    26

  • 8/9/2019 Gnu Arm7t Micro Cross

    27/28

    Getting Started with GNU:

    5. http://sources.redhat.com/ecos6. http://sources.redhat.com/cygwin7. http://www.arm.com/devtools/evaluator_7t?OpenDocument8. http://www.arm.com9. We arent downloading RedHat products here, although what we are download-

    ing goes into several RedHat products. RedHat provides a valuable service to theGNU community by hosting the web and FTP sites for many GNU tools.

    10. http://www.gnu.org/order/ftp.html11. http://sources.redhat.com/insight12. http://www.gnu.org/software/ddd/ddd.html13. http://www.objectcentral.com/vide.htm14. http://kdevelop.kde.org15. http://www.kde.org16. http://www.sourceforge.net/projects/gdbstubs17. http://www.redhat.com18. http://www.redhat.com/products/tools/19. http://www.microcross.com20. http://www.microcross.com/Products/products.html21. http://www.billgatliff.com22. http://sources.redhat.com/ml/crossgcc23. http://www.arm.com24. http://sources.redhat.com/cygwin

    25. http://sources.redhat.com/binutils26. http://gcc.gnu.org27. http://sources.redhat.com/gdb28. http://sources.redhat.com/newlib29. http://sources.redhat.com/insight30. http://www.gnu.org/software/ddd31. http://www.objectcentral.com/32. http://kdevelop.kde.org33. http://www.microcross.com34. http://www.microcross.com/Support/Docs/docs.html35. http://www.redhat.com36. http://www.gnu.org37. http://sourceforge.net/projects/gdbstubs38. http://www.embedded.com/39. http://www.esconline.com/40. http://sourceforge.net/projects/gdbstubs41. http://www.billgatliff.com

    27

  • 8/9/2019 Gnu Arm7t Micro Cross

    28/28

    Getting Started with GNU: