Using Sourcery G++ Lite on GNU/Linux Targets

In order to run and debug programs produced by Sourcery G++ on a GNU/Linux target, you must install runtime support files on the target. You may also need to set appropriate build options so that your executables can find the correct dynamic linker and libraries at runtime.

The runtime support files, referred to as the sysroot, are found in the powerpc-linux-gnu/libc directory of your Sourcery G++ Lite installation. The sysroot consists of the contents of the etc, lib, sbin, and usr directories. There may be other directories in powerpc-linux-gnu/libc that contain additional sysroots customized for particular combinations of command-line compiler flags, or multilibs. Refer to the section called “Library Configurations” for a list of the included multilibs in this version of Sourcery G++ Lite.

There are three choices for installing the sysroot on the target:

Setting the environment variable LD_LIBRARY_PATH on the target is not sufficient, since executables produced by Sourcery G++ depend on the Sourcery G++ dynamic linker included in the sysroot as well as the Sourcery G++ runtime libraries.

Installing the Sysroot

If you are modifying an existing system, rather than creating a new system from scratch, you should place the sysroot files in a new directory, rather than in the root directory of your target system.

If you choose to overwrite your existing C library, you may not be able to boot your system. You should back up your existing system before overwriting the C library and ensure that you can restore the backup even with your system offline.

When running Sourcery G++ on a GNU/Linux host, you have the alternative of installing the sysroot on the target at the same pathname where it is installed on the host system. One way to accomplish this is to NFS-mount the installation directory on both machines in the same location, rather than to copy files.

In many cases, you do not need to copy all of the files in the sysroot. For example, the usr/include subdirectory contains files that are only needed if you will actually be running the compiler on your target system. You do not need these files for non-native compilers. You also do not need any .o or .a files; these are used by the compiler when linking programs, but are not needed to run programs. You should definitely copy all .so files and the executable files in usr/bin and sbin.

You need to install the sysroot(s) corresponding to the compiler options you are using for your applications. The tables in the section called “Library Configurations” tell you which sysroot directories correspond to which compiler options. If you are unsure what sysroot is being referenced when you build your program, you can identify the sysroot by adding -v to your compiler command-line options, and looking at the --sysroot= pathname in the compiler output.

Using Linker Options to Specify the Sysroot Location

If you have installed the sysroot on the target in a location other than the file system root, you can use the -rpath and --dynamic-linker linker options to specify the sysroot location.

First find the correct sysroot directory and dynamic linker for your selected multilib. Refer to the section called “Library Configurations”. In the following steps, sysroot is the absolute path to the sysroot directory on the target corresponding to your selected multilib. For the default multilib, the dynamic linker path relative to the sysroot is lib/ld.so.1. This is used in the example below.

If you are using Sourcery G++ from the command line, follow these steps:

  1. When invoking powerpc-linux-gnu-gcc to link your executable, include the command-line options:

    -Wl,-rpath=sysroot/lib:sysroot/usr/lib \
    -Wl,--dynamic-linker=sysroot/lib/ld.so.1

    where sysroot is the absolute path to the sysroot directory on the target corresponding to your selected multilib.

  2. Copy the executable to the target and execute it normally.

Note that if you specify an incorrect path for --dynamic-linker, the common failure mode seen when running your application on the target is similar to

> ./hello
./hello: No such file or directory

or

> ./hello
./hello: bad ELF interpreter: No such file or directory

This can be quite confusing since it appears from the error message as if it is the ./hello executable that is missing rather than the dynamic linker it references.

Specifying the Sysroot Location at Runtime

You can invoke the Sourcery G++ dynamic linker on the target to run your application without having to compile it with specific linker options. To do this, follow these steps:

  1. Build your application on the host, without any additional linker options, and copy the executable to your target system.

  2. Find the correct sysroot directory and dynamic linker for your selected multilib. Refer to the section called “Library Configurations”. In the following steps, sysroot is the absolute path to the sysroot directory on the target corresponding to your selected multilib. For the default multilib, the dynamic linker is lib/ld.so.1. This is used in the example below.

  3. On the target system, invoke the dynamic linker with your executable as:

    > sysroot/lib/ld.so.1 \
      --library-path sysroot/lib:sysroot/usr/lib \
      /path/to/your-executable

    where sysroot is the absolute path to the sysroot directory on the target corresponding to your selected multilib.

    Invoking the linker in this manner requires that you provide either an absolute pathname to your executable, or a relative pathname prefixed with ./. Specifying only the name of a file in the current directory does not work.