Re: RISC OS modules with stock gcc?



David Given <dg@xxxxxxxxxxx> wrote:
That's very interesting --- how does it do it, if you can remember? Does
it actually manage to use ADR to refer to addresses, or does it assemble
the addresses at runtime via several instructions? (gcc appears to only
be able to fix up 32-bit values, so you can't use symbols as immediate
values on individual machine code instructions --- so you can't refer to
them with ADR.)

See below for some examples.

I've done some playing with gcc, and I've *nearly* gotten it all working
using some abbreviated GOT setup code (28 instructions --- not as good
as I'd hoped), gcc actually turns out to take a few shortcuts that
violate one of my assumptions. Basically, I have global variables and
function pointers working, but have lost some const data and string
constants.

Can you point me at something that explains how the GOT works? I had a
Google but didn't really come up with anything coherent.


My three test programs, built with a slightly out of date version of
Norcroft. -zM means 'generate code suitable for building a RISC OS
relocatable module':

foo1.c:

int bar(int a, int *b);
int foo(int a) {
int b=0;
return bar(a,&b);
}


*cc -s foo1.c
; generated by Norcroft RISC OS ARM C vsn 5.54 [04 Apr 2003]

AREA |C$$code|, CODE, READONLY
|x$codeseg|

DCB &66,&6f,&6f,&00
DCD &ff000004

IMPORT __rt_stkovf_split_small
IMPORT bar
EXPORT foo
foo
MOV ip,sp
STMDB sp!,{a1,fp,ip,lr,pc}
SUB fp,ip,#4
CMP sp,sl
BLMI __rt_stkovf_split_small
MOV a2,#0
STR a2,[sp,#-4]!
MOV a2,sp
BL bar
LDMDB fp,{fp,sp,pc}

AREA |C$$data|,DATA

|x$dataseg|

END

*cc -zM -s foo1.c
; generated by Norcroft RISC OS ARM C vsn 5.54 [04 Apr 2003]

AREA |C$$code|, CODE, READONLY
|x$codeseg|

DCB &66,&6f,&6f,&00
DCD &ff000004

IMPORT __rt_stkovf_split_small
IMPORT bar
EXPORT foo
foo
MOV ip,sp
STMDB sp!,{a1,fp,ip,lr,pc}
SUB fp,ip,#4
CMP sp,sl
BLMI __rt_stkovf_split_small
MOV a2,#0
STR a2,[sp,#-4]!
MOV a2,sp
BL bar
LDMDB fp,{fp,sp,pc}

AREA |C$$data|,DATA

|x$dataseg|

END


int bar(int a, int *b);
int foo(int a) {
static int b=0;
return bar(a,&b);
}

*cc -s foo2.c
; generated by Norcroft RISC OS ARM C vsn 5.54 [04 Apr 2003]

AREA |C$$code|, CODE, READONLY
|x$codeseg|

IMPORT bar
EXPORT foo
foo
LDR a2,[pc, #L000008-.-8]
B bar
L000008
DCD |x$dataseg|

AREA |C$$data|,DATA

|x$dataseg|
DCD &00000000

END

*cc -zM -s foo2.c
; generated by Norcroft RISC OS ARM C vsn 5.54 [04 Apr 2003]

AREA |C$$code|, CODE, READONLY
|x$codeseg|

IMPORT bar
EXPORT foo
foo
LDR a3,[pc, #L000010-.-8]
LDR a2,[sl,#-0]
ADD a2,a2,a3
B bar
L000010
DCD |x$dataseg|

AREA |C$$data|,DATA

|x$dataseg|
DCD &00000000

END


foo3.c:

int b=0;
int bar(int a, int *b);
int foo(int a) {
return bar(a,&b);
}

*cc -s foo3.c
; generated by Norcroft RISC OS ARM C vsn 5.54 [04 Apr 2003]

AREA |C$$code|, CODE, READONLY
|x$codeseg|

IMPORT bar
EXPORT foo
foo
LDR a2,[pc, #L000008-.-8]
B bar
L000008
DCD b

AREA |C$$data|,DATA

|x$dataseg|

EXPORT b
b
DCD &00000000

END

*cc -zM -s foo3.c
; generated by Norcroft RISC OS ARM C vsn 5.54 [04 Apr 2003]

AREA |C$$code|, CODE, READONLY
|x$codeseg|

IMPORT bar
EXPORT foo
foo
LDR a3,[pc, #L000010-.-8]
LDR a2,[sl,#-0]
ADD a2,a2,a3
B bar
L000010
DCD b

AREA |C$$data|,DATA

|x$dataseg|

EXPORT b
b
DCD &00000000

END
.



Relevant Pages

  • Re: Learning C, Trying to Understand & (unary op)
    ... void swap(int x, int y) ... int tmp; ... The formal parameters x and y are different objects from foo and bar; ...
    (comp.lang.c)
  • accessor/mutator - design flaw
    ... consider the class BAR which has a member data in_use that FOO ... Similarily FOO has member data 'idx' that BAR ... int in_use; ...
    (comp.lang.cpp)
  • Re: Features new to Java 5.0 rated today
    ... (JDK 6 ... public void doSomethingWithAllElements(Collectionbar) { ... i.e. if you declare "int x", x can contain any integer value, and cannot contain null, but if you declare it as "int? ... E.g. "Foo x" is a reference to an instance of Foo, ...
    (comp.lang.java.advocacy)
  • Circular dependency - I think..
    ... The header file for FOO is composed of BAR, ... int GetFbk(); ... void ComputeTorquerCmd(); ...
    (comp.lang.cpp)
  • Re: [C] strcat() question (ongoing)
    ... > At the machine level, yes, basically (depending on your definition ... We can only "PUT" data in / or assign data to variables such as an int or ... foo is an object? ... > house and look on the microwave. ...
    (alt.comp.lang.learn.c-cpp)