Chapter 4: Building Your First Application from Zero

This is an LLM ass-isted document, as are all in the ass/ subdirectory.
Of these, here in the dump/ subdirectory I keep the sloppy & doubtful ones.
I keep these as learning materials for myself, yet to be went thru.

Return to index

1. From Zero to a Running PRC

In this chapter, we write, compile, link, package, patch, and verify a complete Palm OS application written in modern C++20 using our GCC 14 toolchain.

2. Step 1: Write the C++ Source (hello.cpp)

Create a new directory for your first project:

mkdir -p ~/palm-projects/01-first-app && cd ~/palm-projects/01-first-app

Write hello.cpp:

// hello.cpp — Modern C++20 Palm OS Application
using UInt16 = unsigned short;
using UInt32 = unsigned long;
using Err = short;

constexpr UInt16 sysAppLaunchCmdNormalLaunch = 0;
constexpr Err errNone = 0;

// Minimal Palm OS API trap signatures
extern "C" {
    UInt16 FrmCustomAlert(UInt16 alertId, const char* s1, const char* s2, const char* s3);
}

// A modern C++ class demonstrating encapsulation and methods
template <typename T>
class Accumulator {
    T value_;
public:
    constexpr explicit Accumulator(T initial = 0) : value_(initial) {}
    void add(T x) { value_ += x; }
    T get() const { return value_; }
};

// Entry point: MUST be extern "C"
extern "C" {
UInt32 PilotMain(UInt16 cmd, void* cmdPBP, UInt16 launchFlags) {
    (void)cmdPBP;
    (void)launchFlags;

    // Ignore launch codes other than normal interactive launch
    if (cmd != sysAppLaunchCmdNormalLaunch) {
        return errNone;
    }

    // Modern C++ template class instantiated safely on the stack
    Accumulator<int> acc(10);
    acc.add(32); // value is now 42

    // Display a Palm OS alert dialog
    FrmCustomAlert(0, "C++20 on Palm OS", "Accumulator total: 42", "Enjoying GCC 14!");

    return errNone;
}
}

3. Step 2: The Universal Linker Script (palmres.ld)

Save the universal Palm OS linker script in your project directory:

/* palmres.ld */
OUTPUT_FORMAT("coff-m68k")
MEMORY { coderes : ORIGIN = 0x0, LENGTH = 32770 }
SECTIONS {
    .text : { *(.text) } > coderes
    /DISCARD/ : { *(.note.GN) *(.comment) *(.ident) }
}

4. Step 3: The 6-Step Compilation Pipeline

Now execute the complete toolchain pipeline:

4.1. 1. Compile C++ to m68k Assembly

/tmp/g++14-palmos -S -m68000 -fno-pic -nostdlib -ffreestanding \
  -std=c++20 -O2 -fno-ident -fno-exceptions -fno-rtti \
  -o hello.s hello.cpp
  • -S: Emits assembly text only instead of binary ELF object code.
  • -m68000: Targets the base 68000 architecture of the DragonBall CPU.
  • -fno-pic: Generates absolute PC-relative code instead of PIC tables.
  • -nostdlib -ffreestanding: Strips host OS libraries and standard includes.
  • -O2: Optimizes aggressively, inlining the Accumulator methods.

4.2. 2. Filter Modern Assembler Directives

Modern GCC emits ELF directives that the Palm OS binutils assembler (m68k-palmos-as) does not understand. Strip them:

grep -vE '\.ident|\.type|\.size|\.section' hello.s > hello.clean.s

4.3. 3. Assemble with Palm OS Binutils

m68k-palmos-as -m68000 -o hello.o hello.clean.s

This creates an authentic Palm OS COFF object file.

4.4. 4. Link with Palm OS Relocations

m68k-palmos-ld --embedded-relocs --no-check-sections -N \
  -T palmres.ld -o hello.elf hello.o
  • --embedded-relocs: Generates the relocation fixups required by Palm OS to relocate the code section when loaded into RAM.
  • -T palmres.ld: Uses our linker script to map .text into coderes and discard unnecessary notes.

4.5. 5. Package as a Palm OS PRC

/usr/bin/build-prc -t appl -c GP14 -n "Gpp14Modern" -o Gpp14Modern.prc hello.elf
  • -t appl: Sets database type to appl (application).
  • -c GP14: Assigns a 4-character Creator ID (GP14).
  • -n "Gpp14Modern": Sets the internal Palm OS application database name.

5. Step 4: The Critical PRC Flag Patch (Pitfall #30b)

If you install Gpp14Modern.prc into an emulator right now, the emulator will reject it with:

"failed to install database"

Why? When build-prc packages an executable from an ELF binary directly, it sets the database header attribute flags at offset 0x20 to 0x0001 (dmHdrAttrBackup only). Palm OS and modern emulators (like CloudpilotEmu) strictly require flag bit 3: dmHdrAttrLaunchableData (0x0008). Without it, the operating system treats the file as non-executable data!

We patch the two-byte word at offset 0x20 from 0x0001 to 0x0009 (dmHdrAttrBackup | dmHdrAttrLaunchableData):

python3 -c "
with open('Gpp14Modern.prc', 'r+b') as f:
    f.seek(0x20)
    flags = int.from_bytes(f.read(2), 'big')
    if not (flags & 0x0008):
        f.seek(0x20)
        f.write(bytes([0x00, 0x09]))
        print(f'Successfully patched header flags: 0x{flags:04x} -> 0x0009')
    else:
        print(f'Flags already correct (0x{flags:04x})')
"

6. Inspection & Size Verification

Check the size of your generated PRC:

ls -lh Gpp14Modern.prc
# Output:
# -rw-r--r-- 1 node node 201 B Oct  7 15:30 Gpp14Modern.prc

Notice the remarkable size: just 201 bytes! Because modern GCC 14 inlines the C++ template code and computes calculations at compile-time with zero runtime overhead, the resulting .text section is approximately 16 bytes of pure 68000 machine instructions. This is the smallest valid modern C++ executable ever created for Palm OS.

7. Automation: The Master Build Script (build.sh)

Save this reusable script to automate your build pipeline:

#!/usr/bin/env bash
set -euo pipefail

NAME="${1:-Gpp14Modern}"
SRC="${2:-hello.cpp}"
CRID="${3:-GP14}"

echo "==> [1/5] Compiling $SRC with GCC 14 C++20..."
/tmp/g++14-palmos -S -m68000 -fno-pic -nostdlib -ffreestanding \
  -std=c++20 -O2 -fno-ident -fno-exceptions -fno-rtti \
  -o "$NAME.s" "$SRC"

echo "==> [2/5] Filtering ELF directives..."
grep -vE '\.ident|\.type|\.size|\.section' "$NAME.s" > "$NAME.clean.s"

echo "==> [3/5] Assembling m68k COFF object..."
m68k-palmos-as -m68000 -o "$NAME.o" "$NAME.clean.s"

echo "==> [4/5] Linking with palmres.ld..."
m68k-palmos-ld --embedded-relocs --no-check-sections -N \
  -T palmres.ld -o "$NAME.elf" "$NAME.o"

echo "==> [5/5] Packaging PRC..."
/usr/bin/build-prc -t appl -c "$CRID" -n "$NAME" -o "$NAME.prc" "$NAME.elf"

# Header flag patch
python3 -c "
with open('$NAME.prc', 'r+b') as f:
    f.seek(0x20)
    flags = int.from_bytes(f.read(2), 'big')
    if not (flags & 0x0008):
        f.seek(0x20)
        f.write(bytes([0x00, 0x09]))
"

echo "==> SUCCESS: Created $NAME.prc ($(stat -c%s "$NAME.prc") bytes)"

8. Further Reading & Reference Resources