Here's a CodeWarrior bug report which I just sent to Palm; I'm posting it to the
mailing list because it might be of interest to other CodeWarrior programmers.

-adam

----- Original Message -----
From: Adam Dingle
To: CodeWarrior Support
Sent: Monday, April 19, 1999 11:42 AM
Subject: CodeWarrior bug: function pointers are inconsistent


I'm using CodeWarrior for Palm OS release 5 on Windows NT 4.0; I've installed
patch 1.

I found a bug in the way that CodeWarrior constructs pointers to functions.  The
bug occurs in multisegment applications with the Smart code model, and only in
code which is not in the first code segment.  Under these circumstances, if a
function has been declared but not yet defined in the current source file,
CodeWarrior will construct a pointer to the function using PC-relative
addressing; the function pointer points directly to a function's executable
code, which is in storage memory under PalmOS.  If a function has already been
defined in the current source file, CodeWarrior will construct a pointer to the
function using A5-relative addressing; the function pointer points to a jump
instruction in the data segment, which is in PalmOS dynamic memory.

Because CodeWarrior constructs function pointers inconsistently, if I create a
pointer to a function foo() in code which appears after the function foo()
itself, and compare the pointer to the address of foo() in code which appears
before the function foo(), then the comparison will return false even though the
pointer really does point to the function foo().

To see the bug, create a new multisegment application in CodeWarrior.  Give the
application the source files "Starter.c" and "foo.c", whose text appears at the
end of this email message.  In the 68K Processor project settings, set the Code
Model to "Smart".  Go to the Segments view and create a new segment "Segment 2",
and move "foo.c" to Segment 2.  Now build the project and run the application in
the PalmOS Emulator with the CodeWarrior debugger.  Step into the body of the
test() function.  You will see that test() returns 0, even though it should
return 1.

Disassembly reveals what's going on.  The function hack() uses -12(a5) as the
address of the function fun(), because fun() appears before hack() in the source
code.  But the function test() computes the address of fun() using PC-relative
addressing, because fun() appears after test() in the source code.  And so the
comparison in test() returns false; -12(a5) is in dynamic memory but the
PC-relative address is in storage memory.

It would be nice if you could address this bug in CodeWarrior for Palm OS
release 6.

thanks -

-adam

=== begin Starter.c ===

#include <Pilot.h>

int hack();

DWord PilotMain( Word cmd, Ptr cmdPBP, Word launchFlags)
{
 if (cmd == sysAppLaunchCmdNormalLaunch)
  hack();
 return 0;
}

=== end Starter.c ===

=== begin foo.c ===

#include <stdio.h>

typedef void *(*test_fun)();

void *fun(void);

static int test(test_fun f)
{
if (f == fun)
 return 1;
else
 return 0;
}

void *fun()
{
return NULL;
}

void hack();
void hack()
{
test_fun f = fun;
int x = test(f);
}

=== end foo.c


Reply via email to