RISC OS Open
A fast and easily customised operating system for ARM devices
ROOL
Home | News | Downloads | Bugs | Bounties | Forum | Documents | Photos | Contact us
Account
Forums → Community Support →

CUtil Makefile

Subscribe to CUtil Makefile 6 posts, 3 voices

 
Jan 20, 2021 12:09pm
Avatar Julie Stamp (8365) 261 posts

I’m having a go at making a Utility in C, and I’ve got a hello world, with this Makefile

COMPONENT      = TestUtil
INCLUDE_OSLIB ?= -IOS:
CINCLUDES      = ${INCLUDE_OSLIB}
LIBS           = ${OSLIB}
OBJS            = main

include CUtil

LDFLAGS        = -util

c.main

#include <stdio.h>
#include "os.h"

int main(int argc, char **argv)
{
  printf("Hello, world\n");
  return 0;
}

It links a utility, but when runs says “Trap in trap handler…”. I might be able to do without stdlib, so I tried getting rid of ANSILib by putting

UTIL_LIBS = ${OSLIB}

after the include CUtil, but it then fails to link saying symbol __main is undefined (I tried putting that as a function but then it says no entry point defined).

So:

Has someone got a working utility, in C, using CUtil or otherwise that links and runs ok with or without stdlib functions?

 
Jan 20, 2021 1:34pm
Avatar Stuart Swales (1481) 354 posts

I wouldn’t think that the SharedCLibrary (or AnsiLib for that matter) expects that environment!

 
Jan 20, 2021 2:13pm
Avatar Jeffrey Lee (213) 5848 posts

I don’t think we have any ready-to-go stubs / “crt0” to allow a “C-only” utility to be written. But, it shouldn’t be too hard to write some minimal stubs that will work with code that doesn’t need the standard library.

A (non-exhaustive) search of the sources turns up two uses of the CUtil makefile:

  • SystemRes.LoadWimp (pure assembler)
  • zinflate (C + asm stub)

zinflate is an unusual case – it’s the zlib decompression code used for compressed ROM images. So instead of producing a runnable Utility, it produces a position-independent binary which begins with the decompression header described here. But it does give some clues as to what’s necessary for creating an assembler stub that can be used with arbitrary C:

  1. Make sure __RelocCode gets called. __RelocCode is a function emitted by the linker, so you also need to make sure the linker is emitting it – which I believe will require either LDFLAGS=-RMF or LDFLAGS=-util. More info here
  2. Set up the arguments for the call to main (or whatever you want your main alternative to be). Since CUtil compiles the code as if it were an Absolute with stack limit checking/stack extension disabled, I don’t think there’s any need to set up FP, SL, or a stack chunk header (_kernel_stack_chunk).
  3. Call main
  4. Return to the OS

I wouldn’t think that the SharedCLibrary (or AnsiLib for that matter) expects that environment!

Yeah, you’d have trouble trying to convince either of those to work in a Utility.

 
Jan 20, 2021 3:25pm
Avatar Julie Stamp (8365) 261 posts

Ok, here’s my working hello world.

s.entry

        IMPORT main
        IMPORT __RelocCode

        EXPORT __main

        AREA |Asm_Code|,CODE,READONLY
        ENTRY

__main
        STR   R14,[R13,#-4]!
        MOV   R11,#0
        BL    __RelocCode
        BL    main
        LDR   PC,[R13],#4

        END

c.main

#include "os.h"

int main(const char *name, const char *tail)
{
  xos_write0("Hello, world");
  xos_new_line();
  return 0;
}

Makefile

COMPONENT      = TestUtil
INCLUDE_OSLIB ?= -IOS:
CINCLUDES      = ${INCLUDE_OSLIB}
OBJS           = main entry

include CUtil

UTIL_LIBS = ${OSLIB}
LDFLAGS   = -util

I did zero FP for the benefit of the first stack frame. I didn’t try and pass through the R12 workspace yet, I’ll see how it goes as I write the program — I think the most sensible will be to just make everything local variables since it’s all coming out of that same 1K block anyway.

Thanks :-)

 
Jan 20, 2021 3:46pm
Avatar Jeffrey Lee (213) 5848 posts

Note that __RelocCode corrupts R0-R3, R11 & R12.

 
Jan 21, 2021 9:47pm
Avatar Julie Stamp (8365) 261 posts

I’ve had to abandon this idea as I need to start a new application which I’m not allowed to do from a utility, so I’m doing a runnable module instead. It’s nice to know that you can do it though.

Reply

To post replies, please first log in.

Forums → Community Support →

Search forums

Social

Follow us on and

ROOL Store

Buy RISC OS Open merchandise here, including SD cards for Raspberry Pi and more.

Donate! Why?

Help ROOL make things happen – please consider donating!

RISC OS IPR

RISC OS is an Open Source operating system owned by RISC OS Developments Ltd and licensed primarily under the Apache 2.0 license.

Description

Community-provided support for all users of RISC OS.

Voices

  • Julie Stamp (8365)
  • Stuart Swales (1481)
  • Jeffrey Lee (213)

Options

  • Forums
  • Login
Site design © RISC OS Open Limited 2018 except where indicated
The RISC OS Open Beast theme is based on Beast's default layout

Valid XHTML 1.0  |  Valid CSS

Powered by Beast © 2006 Josh Goebel and Rick Olson
This site runs on Rails

Hosted by Arachsys