undefined reference to sbrk

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
puntino
Posts: 1
Joined: Wed Jul 27, 2011 8:49 am

undefined reference to sbrk

Post by puntino »

I have tried to sort out the "unbdefined reference sbrk" message. First my system comfiguration, then my attempts. I have to build an executable (elf file) for powerpc with gnu-gcc 4.2.2. installed on windows XP. The tool chain works properly because I already managed to build several other projects without using function with dynamic allocation (e.g., malloc). The options that I use for compilaing: -gdwarf-2 -c -fno-exceptions -finline linking: -nostartfiles -v -Xlinker --output=$(OUTPUTFILE) -Xlinker -Map=map.txt -Xlinker --script=$(INDPATH) -lc -lm (INDPATH points to my linker script file).

To sort the problem, I have written my bsrk implementaion in a file, systemcall.c that is part of my project. Then, I modified also the script file to add the heap section.

I build the project with the former link option and I still get the same error.

I build with -nostartfiles -v -Xlinker --output=$(OUTPUTFILE) -Xlinker -Map=map.txt -Xlinker --script=$(INDPATH) -o systemcall.o -lc -lm , error again

I build with -nostartfiles -v -Xlinker --output=$(OUTPUTFILE) -Xlinker -Map=map.txt -Xlinker --script=$(INDPATH) -lc -lm -lyk -> NO ERROR, but when I run the program it gets stuck after I call the malloc function. I tried to find out on -lyk, but I haven't found satisfing info. I suspect that option provides a stub to the compiler with the _bsrk symbol, thus the compiler doesn't complain, but an actual implementation is missing.

How can I sort this problem? Another attemp that I was thinking of doing is to create from systemcall.o a static library and force the linker to link it wit -l option (or -L if I do not place the library in the "sys" folder). Any help is appreciated, thank you.
OSwhatever
Member
Member
Posts: 595
Joined: Mon Jul 05, 2010 4:15 pm

Re: undefined reference to sbrk

Post by OSwhatever »

If you make any library calls to libc, they might be calling malloc and that ends up in sbrk. Even if you're sure that malloc isn't called, GCC might decide include several system calls anyway.

One way to avoid this is by setting the sbrk symbol to zero in you linker script.

just add

sbrk = 0 (or _sbrk, can't remember)

Now your call will jump to zero if your code ever tries to call sbrk and you can easily trap that.

Also you can try to compile your code so that all functions reside in their own sections, then also tell the linker to remove unused sections. I guess the libc must be compiled with that option too if that is going to work.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: undefined reference to sbrk

Post by Love4Boobies »

The previous solution is a hack. Try replacing -nostartfiles with -ffreestanding.

EDIT: Weird, I can't locate some of the arguments you pass in GCC's documentation...
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply