In order to run and debug programs produced by Sourcery G++ on a uClinux 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 sh-uclinux/libc
directory of your
Sourcery G++ Lite installation. The sysroot consists of the contents of the
etc
, lib
, sbin
,
and usr
directories.
The sysroots provided in Windows host packages for Sourcery G++ are not directly usable on the uClinux target because of differences between the Windows and uClinux file systems. Some files that are hard links, or copies, in the sysroot as installed on the Windows file system should be symbolic links on the uClinux target. Additionally, some files in the sysroot that should be marked executable on the uClinux target are not marked executable on Windows. If you intend to use the sysroot provided with Sourcery G++ on a Windows host system as the basis for your uClinux target filesystem, you must correct these issues after copying the sysroot to the target.
You have these choices for installing the sysroot on the target:
You can install the files in the filesystem root on the target
(that is, installing the files directly in /etc/
,
/lib/
, and so on). All applications on the
target then automatically use the Sourcery G++ libraries.
This method is primarily useful when you are
building a uClinux root filesystem from scratch.
If your target board already has a uClinux filesystem installed,
overwriting the existing C library files is not recommended, as this
may break other applications on your system, or cause it to fail to boot.
You can install the sysroot in an alternate location and build your
application with the -rpath
and
--dynamic-linker
linker options to specify the sysroot
location.
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.
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.
The mechanism you use for copying the sysroot to your target board depends on its hardware and software configuration. You may be able to use FTP or SSH with a server already running on your target. If your target board does not have networking configured, you may be able to copy files using an SD card or USB memory stick, or via a file transfer utility over a serial line. The instructions that come with your board may include specific suggestions.
When running Sourcery G++ on a GNU/Linux host, as an alternative to copying files to the target system, you may be able to NFS-mount the Sourcery G++ Lite installation directory from your host system on the target system. It is especially convenient for debugging if you can make the sysroot pathname on the target system be identical to that on the GNU/Linux host system; refer to Section 3.4.3, “Setting the Sysroot in the Debugger” for further discussion of this issue.
Otherwise, you must copy files from the
sh-uclinux/libc
directory of
your Sourcery G++ Lite install to the target system.
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
.
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.
If you are using Sourcery G++ from the command line, follow these steps:
First find the correct sysroot directory, dynamic linker, and library
subdirectory for your
selected multilib. Refer to Section 3.2, “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-uClibc.so.0
, and the library subdirectory is
lib
.
This is used in the example below.
When invoking sh-uclinux-gcc
to link your executable,
include the command-line options:
-Wl,-rpath=sysroot
/lib:sysroot
/usr/lib \ -Wl,--dynamic-linker=sysroot
/lib/ld-uClibc.so.0
where sysroot
is the absolute path to the
sysroot directory on the target corresponding to your selected multilib.
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
> ./factorial ./factorial: No such file or directory
or
> ./factorial ./factorial: 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 ./factorial
executable that is
missing rather than the dynamic linker it references.