Booting Using GDB
Introduction
GDB can be used to load the Linux kernel onto the target,
and debug it. To do this you need several things:
-
The SuperH IPL (Initial Program Loader), and a way to load it onto
your target hardware.
-
The Linux kernel configured with support for GDB.
-
A version of GDB which supports the SuperH.
IPL
The SuperH IPL (Initial Program Loader) performs several functions:
-
It initialises the basic hardware, in particular the SuperH's Bus State
Controller (BSC).
-
It provides a very simple command interpreter which in the future will
be used to control the boot process.
-
Provides some basic facilities which the Linux kernel (or any other
application) can call on (for example to print messages on the serial
port).
-
It also contains a version of the GDB `stub' which the GDB running on the
host communicates with.
This is why the IPL's full name is sh-ipl+g: SuperH Initial
Program Loader with GDB stub.
In many ways IPL is analogous to the BIOS of a PC.
When used with GDB, the stub acts as the `debug agent', allowing the
GDB which is running on the host computer to perform operations on the
target machine. Normally GDB communicates with the stub over a serial
port, and that is what we use on the SuperH:
How you load IPL onto your target depends very much on what facilities
you already have available. If your target already has a monitor ROM,
which provides program download facilities, this is probably the best
option, at least to start with. Alternatively you may want to use
IPL as the monitor ROM, in which case it will be the first thing the
SuperH executes where it boots. In this case you will need to generate ROM
images, and will need a way to get the image into non-volatile memory.
The build process is almost identical for both
options, it is just the final step of loading the image onto the target
which will be different.
Throughout this document I will use the term `image' to describe the
file which is generated by the tools on the host, and which will be loaded
onto the target.
As IPL and the Linux kernel will need to co-exist, the memory map you use
needs to be considered. As the kernel always leaves the first 4K of memory
free, this can be used for the IPL's data and stack. If code also needs to be
in RAM, probably the easiest solution is to use the end of available
RAM (64K should be more than ample).
Thus we have two memory maps to describe to different parts of the system,
and which is general should never overlap.
The memory map which IPL should use is described in the
machine/target.mem file, in the IPL source
directory (see below), while the kernel
can be instructed to leave the end of memory alone by simply reducing the
amount of memory specified using the mem command line option.
Building IPL
First you will need to download the IPL sources. They are currently
available from
ftp://ftp.m17n.org/pub/linux-sh,
in the file sh-ipl+g-date.tar.gz. Download
the latest version, and unpack it. There are then two possibilities:
-
Your target hardware is already supported by the IPL. To work out whether
this is the case, have a look in the machine directory,
and hopefully there will be a file, target.mem,
where target is the name of your target hardware.
In this case you just have a small amount of configuration to do:
-
Make a symbolic link to the makefile config file:
ln -s config/config.mk-target config.mk
-
Make a symbolic link to the header config file:
ln -s config/config.h-target config.h
-
Your target hardware is not yet supported. In this case things are a little
harder, and you will need to know rather more about you hardware.
Briefly, you will need to:
-
Generate the configuration files described in the previous point.
config/config.mk-target should define the
following macros:
-
CONFIG_LITTLE_ENDIAN=y
-
if your target is little endian. Do nothing if it is big endian.
-
CONFIG_CPU_SH3=y or CONFIG_CPU_SH4=y
-
as appropriate.
-
CONFIG_TARGET=y
-
for your target hardware.
Similarly config/config.h-target should contain:
-
#define CONFIG_LITTLE_ENDIAN 1
-
if your target is little endian. Do nothing if it is big endian.
-
#define CONFIG_SCI 1 or
#define CONFIG_SCIF 1
-
as appropriate.
-
#define CONFIG_TARGET 1
-
for your target hardware.
There are a number of other options which can be used to pass
information through to the Linux kernel, which you may want
to investigate, but they are not currently used.
-
You will need to add a machine specific initialisation file,
which should have the name
init-target.S. This needs to
export a single function, init_bsc which initialises
the BSC. Obviously this code cannot access any RAM which requires
the BSC to already be initialised! The documentation for your
target hardware should give you the values needed for this file.
-
The Makefile needs to be modified to pick up the
machine specific initialisation file.
It also needs to define a value for the macro
ADJUST_VMA. This value is needed on some systems
where the address stored in the image for where it should be loaded,
is different from the address the code will actually run from.
For example, the
SuperH always boots from 0xa0000000, but some ROM programming
tools will expect this to be address 0x0.
It may also be necessary to define any special rules needed
for your target hardware, for example to generate a special
image file.
-
You may also need to add code to sh-sci.c which sets
up BPS_SETTING_VALUE with the correct value for your
hardware.
-
Finally you will need to add a file defining your target's
memory map, by writing the file
machine/target.mem, and include
this in the linker configuration script sh-stub.lds.S.
Once you've performed the configuration, you're ready to build the image.
You will need to select the format which is appropriate:
- ELF
make sh-stub.elf
- Intel hex
make sh-stub.hex
- Binary
make sh-stub.bin
- Motorola S-records
make sh-stub.srec
Next load the image onto the target. How you do this will depend on the
target hardware. Here are a few of the known options:
If you now connect a terminal emulator to the target (configured for
115200 baud, 8 bits, no parity), boot the target if necessary, and hit any
key, you should see:
SH IPL+g version 0.5, Copyright (C) 2000 Free Software Foundation, Inc.
This software comes with ABSOLUTELY NO WARRANTY; for details type `w'.
This is free software, and you are welcome to redistribute it under
certain conditions; type `l' for details.
>
Type `?' for a list of supported commands.
Building the Linux Kernel
The Linux kernel is built in the normal way, except with a slightly
different configuration.
Run your chosen configuration tool, and under:
Kernel hacking
select:
Use LinuxSH standard BIOS
which will cause another three options to appear. Make sure they are set
to the following values:
(a0000000) GDB Stub VBR
[*] GDB Stub kernel debug
[ ] Early printk support
The `GDB Stub VBR' needs to be set to the base address of the IPL text section.
If you are using IPR as the ROM monitor, then this will be 0xa0000000, but
if IPL is not actually the first program you are booting, it will probably
be something else.
Note 'GDB Stub kernel debug' and 'Early printk support' are mutually exclusive.
It is also useful to change the kernel compilation options slightly,
to add -g to enable the generation of debug data, and
and remove -fomit-frame-pointer, so that backtracking works.
The easiest way to do this is modify
arch/sh/Makefile by applying the following patch:
RCS file: /cvsroot/linuxsh/kernel/arch/sh/Makefile,v
retrieving revision 1.9
diff -u -r1.9 Makefile
--- arch/sh/Makefile 2026/07/31 12:35:55 1.9
+++ arch/sh/Makefile 2026/09/11 16:22:10
@@ -12,6 +12,11 @@
# this architecture
#
+# Redefine CFLAGS when building for debugging.
+ifdef CONFIG_DEBUG_KERNEL_WITH_GDB_STUB
+CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes -O1 -g
+endif
+
#
# Select the object file format to substitute into the linker script.
#
Finally build the kernel as normal.
Building GDB
First you will need to download the GDB sources. These should be
downloaded from SourceForge, from the gdb module.
Next build GDB, for example:
mkdir gdb-sh-host
cd gdb-sh-linux
../gdb/configure --host=i686-pc-linux-gnu --target=sh-linux-gnu \
--prefix=/usr/local/sh
make
make install
cd ..
You are now ready to run. First start the IPL running, and connect to it
with a terminal emulator as before. At the prompt type 'g', to start the
GDB stub, which should result in the output:
$S05#b8
Next quit the terminal emulator, and start GDB:
sh-linux-gnu-gdb vmlinux
GDB will print some messages, and then prompt for commands. To ensure
everything is working, a good starting point would be:
(gdb) set endian little
(gdb) set remotebaud 115200
(gdb) target remote /dev/ttyS1
Which should result in something similar to:
0xadff18c6 in ?? ()
where the address will be inside IPL. Next load the kernel:
(gdb) load
GDB will print a line of output as each section of the ELF file is loaded.
This can take several minutes.
Once the kernel has been loaded you can set any breakpoints required,
as normal. Finally start the kernel running:
cont
assuming all is well, the kernel will print all its debugging messages
using the GDB stub. As soon as the kernel tries to initialise the
SuperH serial ports, GDB will be detached from the kernel. This is a
little misleading, as it appears as if the kernel has exited:
SuperH SCI(F) driver initialized
ttySC0 at 0xffe00000 is a SCI
ttySC1 at 0xffe80000 is a SCIF
Program exited normally.
(gdb)
The system does this because it would
not be possible to use the serial port for both GDB and a normal serial
console. At this point it is possible to connect using the terminal
emulator again, and press a key to resume the kernel.
Once you have done this a few times, and the novelty has worn off, it
is probably a good idea to save these commands into a file. If you create
a .gdbinit file, this will be executed automatically by GDB
when it starts. This can be used to perform some initial configuration, load
the kernel image (and a root file system image if required), and set up the
kernel command line.
A number of same configuration files are provided, look for files with
names .gdbinit-machine in the
root of the Linux kernel directory. Simply copy one of these, and modify
it as required.
|