Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Memory fault -- brain fried


devel / comp.arch.embedded / Re: Override libc functions

SubjectAuthor
* Override libc functionspozz
+- Re: Override libc functionsRichard Damon
+- Re: Override libc functionsTheo
+* Re: Override libc functionsDavid Brown
|`* Re: Override libc functionspozz
| `* Re: Override libc functionsPhil Hobbs
|  `- Re: Override libc functionsDavid Brown
+- Re: Override libc functionsDavid Brown
`- Re: Override libc functionsJohn-Smith

1
Override libc functions

<tglcq7$2n69q$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1032&group=comp.arch.embedded#1032

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Override libc functions
Date: Sat, 24 Sep 2022 00:43:18 +0200
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <tglcq7$2n69q$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 23 Sep 2022 22:43:19 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="a9f4ca7bc2c98ae79c2a300634a5bd80";
logging-data="2857274"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19CMAmJsqcHnvG5sS0JzgjZ803k+uKBLlQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.3.0
Cancel-Lock: sha1:SIiJ0procQ/r5TvVOkJC4RDN0Zc=
 by: pozz - Fri, 23 Sep 2022 22:43 UTC

Many times I'd like to replace libc functions in embedded systems,
because of some tests or because I need a different implementation.

For example, sprintf implementation of newlib uses malloc and I can't
use it many times.

It sometimes happens that I can use malloc, with some restrictions. Just
for test or optimization, I replace libc malloc with my own implementation.

In these cases I use a very simple trick: I simply define a new
sprintf()/malloc() function in one of my source code. During linking
(GNU ld), the linker prefers my implementation and ignore the
implementation in the libc. I don't know why this happens and if it is a
standard way during linking, but it works for me.

Recently I found a different behaviour, with an error from the linker:
multiple definitions of sprintf.

After some time, I understood it is caused by a piece of code that uses
another libc function (ctime) that uses sprintf too, I suppose.
Maybe in this case, ctime needs sprintf from libc so the linker tries to
add two sprintf and gives the error.

First question: what exactly happens?

Second question: is there a better method to override a standard libc
function?
I know I can use preprocessor magic, but in this case I should write
MYSPRINTF(...) that is much worse than sprintf(...).

Re: Override libc functions

<RXqXK.98529$ocy7.37015@fx38.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1033&group=comp.arch.embedded#1033

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx38.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0)
Gecko/20100101 Thunderbird/91.13.1
Subject: Re: Override libc functions
Content-Language: en-US
Newsgroups: comp.arch.embedded
References: <tglcq7$2n69q$1@dont-email.me>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <tglcq7$2n69q$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 37
Message-ID: <RXqXK.98529$ocy7.37015@fx38.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Fri, 23 Sep 2022 19:04:15 -0400
X-Received-Bytes: 2548
 by: Richard Damon - Fri, 23 Sep 2022 23:04 UTC

On 9/23/22 6:43 PM, pozz wrote:
> Many times I'd like to replace libc functions in embedded systems,
> because of some tests or because I need a different implementation.
>
> For example, sprintf implementation of newlib uses malloc and I can't
> use it many times.
>
> It sometimes happens that I can use malloc, with some restrictions. Just
> for test or optimization, I replace libc malloc with my own implementation.
>
> In these cases I use a very simple trick: I simply define a new
> sprintf()/malloc() function in one of my source code. During linking
> (GNU ld), the linker prefers my implementation and ignore the
> implementation in the libc. I don't know why this happens and if it is a
> standard way during linking, but it works for me.
>
> Recently I found a different behaviour, with an error from the linker:
> multiple definitions of sprintf.
>
> After some time, I understood it is caused by a piece of code that uses
> another libc function (ctime) that uses sprintf too, I suppose.
> Maybe in this case, ctime needs sprintf from libc so the linker tries to
> add two sprintf and gives the error.
>
> First question: what exactly happens?
>
> Second question: is there a better method to override a standard libc
> function?
> I know I can use preprocessor magic, but in this case I should write
> MYSPRINTF(...) that is much worse than sprintf(...).
>
>
My experiance is that a common cause is that if multiple functions are
defined in a file, it is normally all or nothing for including that
module, so you need to find out what all is defined in that file and
make a definition for ALL the functions that might be used by pieces of
the library that you use.

Re: Override libc functions

<tXj*yy8Yy@news.chiark.greenend.org.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1034&group=comp.arch.embedded#1034

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!nntp.terraraq.uk!nntp-feed.chiark.greenend.org.uk!ewrotcd!.POSTED.chiark.greenend.org.uk!not-for-mail
From: theom+n...@chiark.greenend.org.uk (Theo)
Newsgroups: comp.arch.embedded
Subject: Re: Override libc functions
Date: 24 Sep 2022 15:46:59 +0100 (BST)
Organization: University of Cambridge, England
Message-ID: <tXj*yy8Yy@news.chiark.greenend.org.uk>
References: <tglcq7$2n69q$1@dont-email.me>
Injection-Info: chiark.greenend.org.uk; posting-host="chiark.greenend.org.uk:212.13.197.229";
logging-data="26547"; mail-complaints-to="abuse@chiark.greenend.org.uk"
User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (Linux/5.10.0-15-amd64 (x86_64))
Originator: theom@chiark.greenend.org.uk ([212.13.197.229])
 by: Theo - Sat, 24 Sep 2022 14:46 UTC

pozz <pozzugno@gmail.com> wrote:
> After some time, I understood it is caused by a piece of code that uses
> another libc function (ctime) that uses sprintf too, I suppose.
> Maybe in this case, ctime needs sprintf from libc so the linker tries to
> add two sprintf and gives the error.
>
> First question: what exactly happens?

It is possible to have a weak symbol, which provides a default linkage
unless the symbol is explicitly defined. It may also just be that the
linker is satisfied with the local definition and never pulls in the newlib
symbol (eg if the function and its caller were in the same file, I think the
compiler would resolve it without asking the linker - eg if the function was
declared static) and the happenstance is the linker doesn't notice a clash.
(sometimes the ordering of objects on the linker command line matters)

> Second question: is there a better method to override a standard libc
> function?
> I know I can use preprocessor magic, but in this case I should write
> MYSPRINTF(...) that is much worse than sprintf(...).

Do you need the linker to know about your switch, or is preprocessor magic
alone ok?

You could do:
#define sprintf MYSPRINTF
in a header file that's included in all your source code. As long as you
don't do that when compiling newlib that should be OK. If you want to call
newlib's sprintf(), you can either see if newlib has an internal function
you can call (eg __sprintf), or simply implement your MYSPRINTF function in
a file that does not see the #define and is exposed to the default sprintf()
function. Essentially at this point sprintf as a symbol only exists in
newlib, and the compiler sees you calling MYSPRINTF everywhere.

If you want to do more complicated things with the arguments, there are ways
to do varargs with macros if you need to.

Theo

Re: Override libc functions

<tgn8je$30qr9$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1035&group=comp.arch.embedded#1035

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Override libc functions
Date: Sat, 24 Sep 2022 17:43:42 +0200
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <tgn8je$30qr9$1@dont-email.me>
References: <tglcq7$2n69q$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 24 Sep 2022 15:43:42 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="2cfe73191ab385b121d01a7d2f31563f";
logging-data="3173225"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zHS10cQi+yuf826HcnEKGbkJnuTuAq1k="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:LQt2I41KACra7yVyUpokMtkcXMs=
Content-Language: en-GB
In-Reply-To: <tglcq7$2n69q$1@dont-email.me>
 by: David Brown - Sat, 24 Sep 2022 15:43 UTC

On 24/09/2022 00:43, pozz wrote:
> Many times I'd like to replace libc functions in embedded systems,
> because of some tests or because I need a different implementation.
>
> For example, sprintf implementation of newlib uses malloc and I can't
> use it many times.
>
> It sometimes happens that I can use malloc, with some restrictions. Just
> for test or optimization, I replace libc malloc with my own implementation.
>
> In these cases I use a very simple trick: I simply define a new
> sprintf()/malloc() function in one of my source code. During linking
> (GNU ld), the linker prefers my implementation and ignore the
> implementation in the libc. I don't know why this happens and if it is a
> standard way during linking, but it works for me.
>
> Recently I found a different behaviour, with an error from the linker:
> multiple definitions of sprintf.
>
> After some time, I understood it is caused by a piece of code that uses
> another libc function (ctime) that uses sprintf too, I suppose.
> Maybe in this case, ctime needs sprintf from libc so the linker tries to
> add two sprintf and gives the error.
>
> First question: what exactly happens?
>
> Second question: is there a better method to override a standard libc
> function?
> I know I can use preprocessor magic, but in this case I should write
> MYSPRINTF(...) that is much worse than sprintf(...).
>
>

When a linker is looking for symbols used, it will first look in the
list of object files it is given, then move on to searching the static
libraries. So an override of a library function should have priority.

However, when it pulls in a symbol, it will pull in the whole object
file that it is in. (Section garbage collection might let it later
remove unneeded sections, but that is /after/ this stage.) A static
library file is a collection of object files, so when the link requires
pulling in a symbol from the library, it gets all the symbols from the
object file containing it. Usually, libraries are build from lots of
small files with a single function or a few highly related functions in
order to minimise this issue. (It reduces scope for inter-procedural
optimisations in the library, however.)

In your case, it is not unlikely that the object library object file
that contains "sprintf" also contains "snprintf", "vsnprintf", and other
related functions. So although you have overriden "sprintf", perhaps
"ctime" uses "snprintf" and its object file also contains "sprintf" -
thus causing a conflict.

You can find out more by looking at your map file (even from a failed
link), especially with cross-references enabled in the map file.

Solutions then involve overriding the other library functions used by
"ctime", using a different library (perhaps "newlib-nano" does not have
the malloc issue in the first place), using something other than
"ctime", accepting the library's "sprintf", etc.

(As an aside - you should be wary about using "sprintf". "snprintf" is
normally a better choice.)

Re: Override libc functions

<tgn8oq$30rf7$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1036&group=comp.arch.embedded#1036

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Override libc functions
Date: Sat, 24 Sep 2022 17:46:33 +0200
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <tgn8oq$30rf7$1@dont-email.me>
References: <tglcq7$2n69q$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 24 Sep 2022 15:46:34 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="2cfe73191ab385b121d01a7d2f31563f";
logging-data="3173863"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19bbYkv81gju1gnWXXsv4CDaVPawrcjHKk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:LuQQCVdwcZhitf4+R6ho5UhQbhQ=
In-Reply-To: <tglcq7$2n69q$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 24 Sep 2022 15:46 UTC

On 24/09/2022 00:43, pozz wrote:
> Many times I'd like to replace libc functions in embedded systems,
> because of some tests or because I need a different implementation.
>
> For example, sprintf implementation of newlib uses malloc and I can't
> use it many times.
>
> It sometimes happens that I can use malloc, with some restrictions. Just
> for test or optimization, I replace libc malloc with my own implementation.
>
> In these cases I use a very simple trick: I simply define a new
> sprintf()/malloc() function in one of my source code. During linking
> (GNU ld), the linker prefers my implementation and ignore the
> implementation in the libc. I don't know why this happens and if it is a
> standard way during linking, but it works for me.
>
> Recently I found a different behaviour, with an error from the linker:
> multiple definitions of sprintf.
>
> After some time, I understood it is caused by a piece of code that uses
> another libc function (ctime) that uses sprintf too, I suppose.
> Maybe in this case, ctime needs sprintf from libc so the linker tries to
> add two sprintf and gives the error.
>
> First question: what exactly happens?
>
> Second question: is there a better method to override a standard libc
> function?
> I know I can use preprocessor magic, but in this case I should write
> MYSPRINTF(...) that is much worse than sprintf(...).
>
>

I forgot to mention in my last post - if you override any standard
library functions, make sure the function signature and the semantics
are identical to or stronger than the standard requirements. Compilers
sometimes rely on the standard functions doing what they are supposed to
do, regardless of whether you have overridden them or used the standard
include files. Thus you need to make sure your "sprintf" returns the
value you'd get from standard "sprintf", because the compiler might use
that value even if you don't explicitly collect it in your code using
the function.

Re: Override libc functions

<th3vnt$jrel$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1038&group=comp.arch.embedded#1038

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Override libc functions
Date: Thu, 29 Sep 2022 13:32:14 +0200
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <th3vnt$jrel$1@dont-email.me>
References: <tglcq7$2n69q$1@dont-email.me> <tgn8je$30qr9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 29 Sep 2022 11:32:13 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="a4a4948805eaac188caeacef82e00a42";
logging-data="650709"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19RZGX6RhiFlGtjVMJUN0DEDPWJQWtYEZU="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.3.0
Cancel-Lock: sha1:Tonby/TUEizUJE+GIOzThbBmOnk=
In-Reply-To: <tgn8je$30qr9$1@dont-email.me>
 by: pozz - Thu, 29 Sep 2022 11:32 UTC

Il 24/09/2022 17:43, David Brown ha scritto:
> On 24/09/2022 00:43, pozz wrote:
>> Many times I'd like to replace libc functions in embedded systems,
>> because of some tests or because I need a different implementation.
>>
>> For example, sprintf implementation of newlib uses malloc and I can't
>> use it many times.
>>
>> It sometimes happens that I can use malloc, with some restrictions.
>> Just for test or optimization, I replace libc malloc with my own
>> implementation.
>>
>> In these cases I use a very simple trick: I simply define a new
>> sprintf()/malloc() function in one of my source code. During linking
>> (GNU ld), the linker prefers my implementation and ignore the
>> implementation in the libc. I don't know why this happens and if it is
>> a standard way during linking, but it works for me.
>>
>> Recently I found a different behaviour, with an error from the linker:
>> multiple definitions of sprintf.
>>
>> After some time, I understood it is caused by a piece of code that
>> uses another libc function (ctime) that uses sprintf too, I suppose.
>> Maybe in this case, ctime needs sprintf from libc so the linker tries
>> to add two sprintf and gives the error.
>>
>> First question: what exactly happens?
>>
>> Second question: is there a better method to override a standard libc
>> function?
>> I know I can use preprocessor magic, but in this case I should write
>> MYSPRINTF(...) that is much worse than sprintf(...).
>>
>>
>
> When a linker is looking for symbols used, it will first look in the
> list of object files it is given, then move on to searching the static
> libraries.  So an override of a library function should have priority.
>
> However, when it pulls in a symbol, it will pull in the whole object
> file that it is in.  (Section garbage collection might let it later
> remove unneeded sections, but that is /after/ this stage.)  A static
> library file is a collection of object files, so when the link requires
> pulling in a symbol from the library, it gets all the symbols from the
> object file containing it.  Usually, libraries are build from lots of
> small files with a single function or a few highly related functions in
> order to minimise this issue.  (It reduces scope for inter-procedural
> optimisations in the library, however.)
>
> In your case, it is not unlikely that the object library object file
> that contains "sprintf" also contains "snprintf", "vsnprintf", and other
> related functions.  So although you have overriden "sprintf", perhaps
> "ctime" uses "snprintf" and its object file also contains "sprintf" -
> thus causing a conflict.

ctime()[1] uses actime()[2] that uses siprintf()[3].
I overrided siprintf() too and this solved the linking process.

However it's not completely clear to me. siprintf() is defined in a
single file (siprintf.c), so I thought it was contained in a single
object file (siprintf.o).
However it seems there's an object file (lib_a-sprintf.o) that contains
sprintf() and siprintf(). Maybe this multiple functions object file is
generated during newlib build process.

Re: Override libc functions

<bc3cd030-b5fc-8aab-c663-6f50722c24d0@electrooptical.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1039&group=comp.arch.embedded#1039

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: pcdhSpam...@electrooptical.net (Phil Hobbs)
Newsgroups: comp.arch.embedded
Subject: Re: Override libc functions
Date: Thu, 29 Sep 2022 12:03:02 -0400
Organization: A noiseless patient Spider
Lines: 86
Message-ID: <bc3cd030-b5fc-8aab-c663-6f50722c24d0@electrooptical.net>
References: <tglcq7$2n69q$1@dont-email.me> <tgn8je$30qr9$1@dont-email.me>
<th3vnt$jrel$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: reader01.eternal-september.org; posting-host="4e54c2e9ed1672eddf18f8bbdbc5b23e";
logging-data="744512"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/aKWHqqzm+9tbRKj2neUim"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.0
Cancel-Lock: sha1:mD3eV/jV5if2kb+P5iKtXqotKC0=
In-Reply-To: <th3vnt$jrel$1@dont-email.me>
 by: Phil Hobbs - Thu, 29 Sep 2022 16:03 UTC

pozz wrote:
> Il 24/09/2022 17:43, David Brown ha scritto:
>> On 24/09/2022 00:43, pozz wrote:
>>> Many times I'd like to replace libc functions in embedded systems,
>>> because of some tests or because I need a different implementation.
>>>
>>> For example, sprintf implementation of newlib uses malloc and I can't
>>> use it many times.
>>>
>>> It sometimes happens that I can use malloc, with some restrictions.
>>> Just for test or optimization, I replace libc malloc with my own
>>> implementation.
>>>
>>> In these cases I use a very simple trick: I simply define a new
>>> sprintf()/malloc() function in one of my source code. During linking
>>> (GNU ld), the linker prefers my implementation and ignore the
>>> implementation in the libc. I don't know why this happens and if it
>>> is a standard way during linking, but it works for me.
>>>
>>> Recently I found a different behaviour, with an error from the
>>> linker: multiple definitions of sprintf.
>>>
>>> After some time, I understood it is caused by a piece of code that
>>> uses another libc function (ctime) that uses sprintf too, I suppose.
>>> Maybe in this case, ctime needs sprintf from libc so the linker tries
>>> to add two sprintf and gives the error.
>>>
>>> First question: what exactly happens?
>>>
>>> Second question: is there a better method to override a standard libc
>>> function?
>>> I know I can use preprocessor magic, but in this case I should write
>>> MYSPRINTF(...) that is much worse than sprintf(...).
>>>
>>>
>>
>> When a linker is looking for symbols used, it will first look in the
>> list of object files it is given, then move on to searching the static
>> libraries.  So an override of a library function should have priority.
>>
>> However, when it pulls in a symbol, it will pull in the whole object
>> file that it is in.  (Section garbage collection might let it later
>> remove unneeded sections, but that is /after/ this stage.)  A static
>> library file is a collection of object files, so when the link
>> requires pulling in a symbol from the library, it gets all the symbols
>> from the object file containing it.  Usually, libraries are build from
>> lots of small files with a single function or a few highly related
>> functions in order to minimise this issue.  (It reduces scope for
>> inter-procedural optimisations in the library, however.)
>>
>> In your case, it is not unlikely that the object library object file
>> that contains "sprintf" also contains "snprintf", "vsnprintf", and
>> other related functions.  So although you have overriden "sprintf",
>> perhaps "ctime" uses "snprintf" and its object file also contains
>> "sprintf" - thus causing a conflict.
>
> ctime()[1] uses actime()[2] that uses siprintf()[3].
> I overrided siprintf() too and this solved the linking process.
>
> However it's not completely clear to me. siprintf() is defined in a
> single file (siprintf.c), so I thought it was contained in a single
> object file (siprintf.o).
> However it seems there's an object file (lib_a-sprintf.o) that contains
> sprintf() and siprintf(). Maybe this multiple functions object file is
> generated during newlib build process.

An old-time hack to improve optimization with dumb linkers was to have a
..c file #include other .c files so that the compiler got to see more of
the code at a time.

Haven't seen that one in awhile.

Cheers

Phil Hobbs

--
Dr Philip C D Hobbs
Principal Consultant
ElectroOptical Innovations LLC / Hobbs ElectroOptics
Optics, Electro-optics, Photonics, Analog Electronics
Briarcliff Manor NY 10510

http://electrooptical.net
http://hobbs-eo.com

Re: Override libc functions

<th4pfv$np08$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1040&group=comp.arch.embedded#1040

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Override libc functions
Date: Thu, 29 Sep 2022 20:51:43 +0200
Organization: A noiseless patient Spider
Lines: 88
Message-ID: <th4pfv$np08$1@dont-email.me>
References: <tglcq7$2n69q$1@dont-email.me> <tgn8je$30qr9$1@dont-email.me>
<th3vnt$jrel$1@dont-email.me>
<bc3cd030-b5fc-8aab-c663-6f50722c24d0@electrooptical.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 29 Sep 2022 18:51:43 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="b562d4af25c60a66b9345515b405dd5a";
logging-data="779272"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+KbVnMeIXep0Q1JFoFUosJJsPLykof6ac="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:shfv9qhvAqRyuNr9SAP9zuO7EDA=
Content-Language: en-GB
In-Reply-To: <bc3cd030-b5fc-8aab-c663-6f50722c24d0@electrooptical.net>
 by: David Brown - Thu, 29 Sep 2022 18:51 UTC

On 29/09/2022 18:03, Phil Hobbs wrote:
> pozz wrote:
>> Il 24/09/2022 17:43, David Brown ha scritto:
>>> On 24/09/2022 00:43, pozz wrote:
>>>> Many times I'd like to replace libc functions in embedded systems,
>>>> because of some tests or because I need a different implementation.
>>>>
>>>> For example, sprintf implementation of newlib uses malloc and I
>>>> can't use it many times.
>>>>
>>>> It sometimes happens that I can use malloc, with some restrictions.
>>>> Just for test or optimization, I replace libc malloc with my own
>>>> implementation.
>>>>
>>>> In these cases I use a very simple trick: I simply define a new
>>>> sprintf()/malloc() function in one of my source code. During linking
>>>> (GNU ld), the linker prefers my implementation and ignore the
>>>> implementation in the libc. I don't know why this happens and if it
>>>> is a standard way during linking, but it works for me.
>>>>
>>>> Recently I found a different behaviour, with an error from the
>>>> linker: multiple definitions of sprintf.
>>>>
>>>> After some time, I understood it is caused by a piece of code that
>>>> uses another libc function (ctime) that uses sprintf too, I suppose.
>>>> Maybe in this case, ctime needs sprintf from libc so the linker
>>>> tries to add two sprintf and gives the error.
>>>>
>>>> First question: what exactly happens?
>>>>
>>>> Second question: is there a better method to override a standard
>>>> libc function?
>>>> I know I can use preprocessor magic, but in this case I should write
>>>> MYSPRINTF(...) that is much worse than sprintf(...).
>>>>
>>>>
>>>
>>> When a linker is looking for symbols used, it will first look in the
>>> list of object files it is given, then move on to searching the
>>> static libraries.  So an override of a library function should have
>>> priority.
>>>
>>> However, when it pulls in a symbol, it will pull in the whole object
>>> file that it is in.  (Section garbage collection might let it later
>>> remove unneeded sections, but that is /after/ this stage.)  A static
>>> library file is a collection of object files, so when the link
>>> requires pulling in a symbol from the library, it gets all the
>>> symbols from the object file containing it.  Usually, libraries are
>>> build from lots of small files with a single function or a few highly
>>> related functions in order to minimise this issue.  (It reduces scope
>>> for inter-procedural optimisations in the library, however.)
>>>
>>> In your case, it is not unlikely that the object library object file
>>> that contains "sprintf" also contains "snprintf", "vsnprintf", and
>>> other related functions.  So although you have overriden "sprintf",
>>> perhaps "ctime" uses "snprintf" and its object file also contains
>>> "sprintf" - thus causing a conflict.
>>
>> ctime()[1] uses actime()[2] that uses siprintf()[3].
>> I overrided siprintf() too and this solved the linking process.
>>
>> However it's not completely clear to me. siprintf() is defined in a
>> single file (siprintf.c), so I thought it was contained in a single
>> object file (siprintf.o).

The process is recursive - perhaps siprintf.o brings in other files,
ending in including something that also contains sprintf. The map file
cross-reference is your friend here.

>> However it seems there's an object file (lib_a-sprintf.o) that
>> contains sprintf() and siprintf(). Maybe this multiple functions
>> object file is generated during newlib build process.
>
> An old-time hack to improve optimization with dumb linkers was to have a
> .c file #include other .c files so that the compiler got to see more of
> the code at a time.
>
> Haven't seen that one in awhile.
>

Unless you write code specifically targeting such a setup, you have a
fair chance of breakage. And any decent toolchain has better ways of
handling this anyway - gcc has had "-fwhole-program" for a great many
years, then link-time optimisation has become the modern replacement.
clang/llvm has had LTO from the start, and many commercial embedded
compilers have something similar.

Re: Override libc functions

<tiem8f$2qlff$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1067&group=comp.arch.embedded#1067

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: noos...@noospam.com (John-Smith)
Newsgroups: comp.arch.embedded
Subject: Re: Override libc functions
Date: Sat, 15 Oct 2022 17:13:13 +0100
Organization: -
Lines: 18
Message-ID: <tiem8f$2qlff$1@dont-email.me>
References: <tglcq7$2n69q$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 15 Oct 2022 16:14:07 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="051e1cf04f8ec2a5d8b779059f82485f";
logging-data="2971119"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FuFUuAH6BF+ymdKDKnqWT"
Cancel-Lock: sha1:KyMeBIt2RbVY9XtCyUCKgFImO40=
X-No-Archive: yes
X-Newsreader: Forte Agent 3.3/32.846
 by: John-Smith - Sat, 15 Oct 2022 16:13 UTC

pozz <pozzugno@gmail.com> wrote

>Many times I'd like to replace libc functions in embedded systems,
>because of some tests or because I need a different implementation.
>
>For example, sprintf implementation of newlib uses malloc and I can't
>use it many times.

You might enjoy this
https://www.eevblog.com/forum/programming/best-thread-safe-printf-and-why-does-printf-need-the-heap-for-f-etc/msg4325479/#msg4325479

I spent a chunk of my life solving this on a 32F4 project / GCC / ST
CubeIDE.

The issue was that ST's Newlib libc.a had non weak symbols, and the
library had to be weakened (I did the whole lib) before any could be
replaced.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor