Error sbrk undefined

Doug Evans dje@cygnus.com
Tue Jun 17 11:09:00 GMT 1997


   Date: Tue, 17 Jun 1997 21:13:28 +0900
   From: Jens Doege <doege@ljk.atsugi.asahi-kasei.co.jp>

   I use cygnus gcc2.7.2 with newlib-1.7.0.

   When linking my project 

   gcc68k -static -T ld.scr test.o upd72611.o dcpio.o bios.o endian.o
   g_scsi.o dsp_acc.o vec_tbl.o	-o fwtest.rom

   I get the following error message: 

   /usr/local/m68k-unknown-coff/lib/libg.a(sbrkr.o): In function `_sbrk_r':
   sbrkr.c:60: undefined reference to `sbrk'
   make: *** [fwtest] Error 1

   I figured out, thet it has something to do with the malloc() function I
   use. 
   Does anybody have an idea, what I'm doing wrong?

You need to write `sbrk'.

This is covered in the FAQ, though perhaps not as clearly as it
should be.  [Clearly one can't just stub out sbrk. :-)]

What is sbrk?  It is the low level system call upon which malloc is built.
If you want to use the malloc in newlib then you need to write sbrk.

Here's an example from newlib/libc/sys/sh/syscalls.c:

register char *stack_ptr asm ("r15");

caddr_t
_sbrk (int incr)
{
  extern char end;		/* Defined by the linker */
  static char *heap_end;
  char *prev_heap_end;

  if (heap_end == 0)
    {
      heap_end = &end;
    }
  prev_heap_end = heap_end;
  if (heap_end + incr > stack_ptr)
    {
      _write (1, "Heap and stack collision\n", 25);
      abort ();
    }
  heap_end += incr;
  return (caddr_t) prev_heap_end;
}

-----
4.5 Where are open, read, write, close, etc. ?

The following is a typical situation people run into when linking their
application.

/usr/local/m68k-coff/lib/libc.a(sbrkr.o): In function `_sbrk_r':
sbrkr.c:60: undefined reference to `sbrk'
/usr/local/m68k-coff/lib/libc.a(makebuf.o): In function `__smakebuf':
makebuf.c:93: undefined reference to `isatty'
/usr/local/m68k-coff/lib/libc.a(filer.o): In function `_open_r':
filer.c:63: undefined reference to `open'
/usr/local/m68k-coff/lib/libc.a(filer.o): In function `_close_r':
filer.c:100: undefined reference to `close'
/usr/local/m68k-coff/lib/libc.a(filer.o): In function `_lseek_r':
filer.c:142: undefined reference to `lseek'
/usr/local/m68k-coff/lib/libc.a(filer.o): In function `_read_r':
filer.c:184: undefined reference to `read'
/usr/local/m68k-coff/lib/libc.a(filer.o): In function `_write_r':
filer.c:226: undefined reference to `write'
/usr/local/m68k-coff/lib/libc.a(fstatr.o): In function `_fstat_r':
fstatr.c:61: undefined reference to `fstat'

Depending upon the target, system calls are not built into newlib's libc.a.
They are too dependent upon the particular target board in use.
Libgloss (which comes with newlib net releases) is intended to be the
repository of such routines and may either provide them in another library
that you must link against or in an object file.

For systems that don't have a need for such routines, just stub
them out. e.g.

int open (char *f, int flags, ...) { errno = ENOSYS; return -1; }

etc.



More information about the crossgcc mailing list