OpenCores
no use no use 1/1 no use no use
is
by yishin.li on Sep 2, 2010
yishin.li
Posts: 4
Joined: Jun 25, 2008
Last seen: Mar 24, 2011
Hello,

In gcc-4.2.2/gcc/config/or32/or32.h, there's "-u free" option for always linking free() and malloc() with newlib.

Is it necessary for newlib?

We can save 3.8KB program and 2.1KB data memory if the application does not need free() and malloc():

text data bss dec hex filename (without "-u free")
1016 20 0 1036 40c cbasic-nocache-O0-wo-free.or32
text data bss dec hex filename (with "-u free")
4866 2128 56 7050 1b8a cbasic-nocache-O0-free.or32

-Yishin
RE: is -u free option necessary for newlib?
by yishin.li on Sep 2, 2010
yishin.li
Posts: 4
Joined: Jun 25, 2008
Last seen: Mar 24, 2011
The title got trimmed. It should be
is "-u free" option necessary for newlib?
RE: is
by jeremybennett on Sep 2, 2010
jeremybennett
Posts: 815
Joined: May 29, 2008
Last seen: Jun 13, 2019

Hi yishin.li

malloc () and free () are used indirectly in most programs. Most of the library routines (for example for I/O) use them. The problem is that malloc () and free () are both declared as weak references in newlib. To get round this, the C compiler, when running with newlib, uses -u free to force linkage.

If you leave this out, then and library function that in turn requires malloc () and free (), it will not search for them in the library. That's how weak references work (you get the same for example in the ARM libraries). I don't understand why malloc () and free () are done this way in newlib 1.18.0.

If you really have a program that does not need malloc or free, the solution is to run the individual GCC sub-programs, omitting the "-u free". Run your compilation using "gcc -v", then repeat the compilation lines, omitting "-u free".

I'm open to better suggestions. I'd also like to understand why newlib 1.18.0 made this change to the malloc () and free () declarations.

HTH

Jeremy

--
Tel: +44 (1590) 610184
Cell: +44 (7970) 676050
SkypeID: jeremybennett
Email: jeremy.bennett@embecosm.com
Web: www.embecosm.com

RE: is -u free option necessary for newlib?
by yishin.li on Sep 2, 2010
yishin.li
Posts: 4
Joined: Jun 25, 2008
Last seen: Mar 24, 2011
Hi Jeremy,

Thanks for your reply. I googled and found 2 interesting discussions regarding weak references for malloc() with newlib.

http://permalink.gmane.org/gmane.comp.lib.newlib/4466
http://sourceware.org/ml/newlib/2009/msg00011.html
A while ago, Mark Mitchell added --disable-newlib-atexit-dynamic-alloc
to prevent atexit from pulling malloc. This was a bit inflexible
because once newlib was built with --disable-newlib-atexit-alloc, the
user couldn't enable malloc without rebuilding newlib.

This patch makes malloc a weak symbol in the support functions of
atexit. This way, if malloc is available for some other reason, we
use that. Otherwise, we don't to conserve space.


I had synthesized OR1200@50MHz on a Spartan3 FPGA with 16KB of program and data memory. I'm conservative with the space because 16KB is the hardware limit for the board on my hand.

BTW, I do need newlib for math calculation and memset(), memmove()... etc.

Regards,

Yishin
RE: is
by jeremybennett on Sep 3, 2010
jeremybennett
Posts: 815
Joined: May 29, 2008
Last seen: Jun 13, 2019

Hi Yishin,

I believe this patch was a mistake. It relies on a particular interpretation of how weak symbols are handled, which is an area with is explicitly target specific.

It works for targets which will search in libraries for weak symbols. However not all (including OpenRISC and ARM) do so, and thus the patch breaks code.

I am sympathetic to your needs. I suggest you file a bug, and then we can look at upgrading GCC with a -mno-malloc flag, to allow malloc to be omitted.

HTH

Jeremy

RE: is
by yishin.li on Sep 6, 2010
yishin.li
Posts: 4
Joined: Jun 25, 2008
Last seen: Mar 24, 2011
Hi Jeremy,

I've submitted another request to the bug tracer. Please close my previous bug tracer request ("not always linking free() for newlib").

Thanks,

-Yishin
no use no use 1/1 no use no use
© copyright 1999-2026 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.