How to debug and run ARM programs without development board? This article mainly explains how to build arm cross-compilation and running environment on Ubuntu.

Install the cross-compile tool chain

Install cross-compile toolchain ARM-linux-gnueabihf-gcc:

sudo apt-get install gcc-arm-linux-gnueabihf
Copy the code

Once installed, you can see the number of cross-compilation tools that have been added to the system:

helloworld@ubuntu:~$ arm-linux-gnueabihf-
arm-linux-gnueabihf-addr2line     arm-linux-gnueabihf-gcov-7
arm-linux-gnueabihf-ar            arm-linux-gnueabihf-gcov-dump
arm-linux-gnueabihf-as            arm-linux-gnueabihf-gcov-dump-7
arm-linux-gnueabihf-c++filt       arm-linux-gnueabihf-gcov-tool
arm-linux-gnueabihf-cpp           arm-linux-gnueabihf-gcov-tool-7
arm-linux-gnueabihf-cpp-7         arm-linux-gnueabihf-gprof
arm-linux-gnueabihf-dwp           arm-linux-gnueabihf-ld
arm-linux-gnueabihf-elfedit       arm-linux-gnueabihf-ld.bfd
arm-linux-gnueabihf-gcc           arm-linux-gnueabihf-ld.gold
arm-linux-gnueabihf-gcc-7         arm-linux-gnueabihf-nm
arm-linux-gnueabihf-gcc-ar        arm-linux-gnueabihf-objcopy
arm-linux-gnueabihf-gcc-ar-7      arm-linux-gnueabihf-objdump
arm-linux-gnueabihf-gcc-nm        arm-linux-gnueabihf-ranlib
arm-linux-gnueabihf-gcc-nm-7      arm-linux-gnueabihf-readelf
arm-linux-gnueabihf-gcc-ranlib    arm-linux-gnueabihf-size
arm-linux-gnueabihf-gcc-ranlib-7  arm-linux-gnueabihf-strings
arm-linux-gnueabihf-gcov          arm-linux-gnueabihf-strip
Copy the code

The following code is used to establish a soft link:

sudo ln -s /usr/arm-linux-gnueabihf/lib/libc.so.6 /lib/libc.so.6
sudo ln -s /usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3 /lib/ld-linux-armhf.so.3
Copy the code

Write the test code main.c:

#include <stdio.h>	
int main()
{
    printf("helloworld\n");
    return 0;
}
Copy the code

The following command is used to compile the main. C executable file a.out for the ARM platform. The following command is used to compile the main.

helloworld@ubuntu:~$ arm-linux-gnueabihf-gcc main.c helloworld@ubuntu:~$ file a.out a.out: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-, For the GNU/Linux 3.2.0, BuildID [sha1] = 7592 a0494955ca8bb953948ea4cfbefc90b2e2e9, not strippedCopy the code

2. Install ARM simulator QEMU

Install arm simulator QEMu:

sudo apt-get install qemu
Copy the code

Execute the arm platform executable file a.out. As you can see, the program prints the correct result helloWorld:

helloworld@ubuntu:~$ qemu-arm a.out 
helloworld
Copy the code

Note: QEMU can simulate many platforms, not just ARM.

3. Debug ARM programs through GDB

The principle of using GDB to debug ARM programs on Ubuntu is that the QEMU end acts as the GDB server to start the executable program, and the other end acts as the GDB client to connect to the GDB Server for local remote debugging.

  1. First install multi-platform GDB tools:

    sudo apt-get install gdb-multiarch
    Copy the code
  2. Recompile the sample code main.c, this time with the –static parameter. When this parameter is added, the resulting executable is statically linked. If you do not add this parameter, the GDB debugging single step function is not normal, the symbol table can not be found.

    arm-linux-gnueabihf-gcc --static -g main.c 
    Copy the code
  3. Start the executable program A.out with the following command. The -g option specifies the listening port of GDB, which is 1234. After this command runs, the current window is blocked.

    qemu-arm -g 1234 a.out
    Copy the code
  4. Open a command line window, start the GDB client, and enter the GDB interface:

    gdb-multiarch a.out
    Copy the code
  5. To connect to the server, enter the following content on the GDB interface:

    target remote localhost:1234
    Copy the code
  6. Next, you can debug the program using the related functions of GDB:

    (gdb) b main
    Breakpoint 1 at 0x102e8: file main.c, line 5.
    (gdb) c
    Continuing.
    
    Breakpoint 1, main () at main.c:5
    5	    printf("helloworld\n");
    Copy the code

Iv. Reference documents

  1. Compile, run and debug ARM program in Linux
  2. Qemu related documents
  3. CREATE DEBUG ENVIRONMENT FOR ARM ARCHITECTURE ON INTEL PROCESSOR

For more information, please visit blog.coderhuo. Tech.