Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Remember Darwin; building a better mousetrap merely results in smarter mice.


devel / comp.arch.embedded / Patch fixed strings in .hex file

SubjectAuthor
* Patch fixed strings in .hex filepozz
+* Re: Patch fixed strings in .hex fileDavid Brown
|`* Re: Patch fixed strings in .hex filepozz
| +* Re: Patch fixed strings in .hex fileDavid Brown
| |`- Re: Patch fixed strings in .hex filepozz
| `* Re: Patch fixed strings in .hex filedalai lamah
|  `- Re: Patch fixed strings in .hex fileDavid Brown
+* Re: Patch fixed strings in .hex fileHerbert Kleebauer
|`- Re: Patch fixed strings in .hex filepozz
+* Re: Patch fixed strings in .hex fileGrant Edwards
|+* Re: Patch fixed strings in .hex fileDavid Brown
||`- Re: Patch fixed strings in .hex fileGrant Edwards
|`- Re: Patch fixed strings in .hex filepozz
+* Re: Patch fixed strings in .hex fileDavid Brown
|`- Re: Patch fixed strings in .hex fileTauno Voipio
+* Re: Patch fixed strings in .hex fileStefan Reuther
|+- Re: Patch fixed strings in .hex fileGrant Edwards
|`- Re: Patch fixed strings in .hex fileMichael Schwingen
+* Re: Patch fixed strings in .hex fileHans-Bernhard Bröker
|`* Re: Patch fixed strings in .hex filepozz
| +- Re: Patch fixed strings in .hex filepozz
| +* Re: Patch fixed strings in .hex fileDavid Brown
| |`* Re: Patch fixed strings in .hex filepozz
| | +- Re: Patch fixed strings in .hex fileDavid Brown
| | `* Re: Patch fixed strings in .hex fileStefan Reuther
| |  +- Re: Patch fixed strings in .hex fileDavid Brown
| |  +- Re: Patch fixed strings in .hex fileGrant Edwards
| |  `- Re: Patch fixed strings in .hex filepozz
| `- Re: Patch fixed strings in .hex fileHans-Bernhard Bröker
+- Re: Patch fixed strings in .hex filepozz
+- Re: Patch fixed strings in .hex fileDon Y
`- Re: Patch fixed strings in .hex filePaul Rubin

Pages:12
Patch fixed strings in .hex file

<uo5s8d$1cjl8$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 13:19:27 +0100
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <uo5s8d$1cjl8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 16 Jan 2024 12:19:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b5206c1c7cc657f768081c5a742b55e0";
logging-data="1461928"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180DP8fq/2Q44Om59HPJTxMyZ3pqzgHz1o="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:c5IkK0tMjo30yin6iHuXSjxr0Bg=
Content-Language: it
 by: pozz - Tue, 16 Jan 2024 12:19 UTC

In one project I have many quasi-fixed strings that I'd like to keep in
non volatile memory (Flash) to avoid losing precious RAM space.

static const char s1[] = "/my/very/long/string/of/01020304";
static const char s2[] = "/another/string/01020304";
....

Substring "01020304" is a serial number that changes during production
with specific device. It has the same length in bytes (it's a simple hex
representation of a 32-bits integer).

Of course it's too difficult and slow to rebuild the firmware during
production passing to the compiler the real serial number. I think a
better solution is to patch the .hex file generated by the compiler.

I'm wondering how to detect the exact positions (addresses) of serial
numbers to fix.

The build system is gcc, so I could search for s1 in the elf file. Do
you know of a tool that returns the address of a symbol in the elf or
map file?

Could you suggest a better approach?

Re: Patch fixed strings in .hex file

<uo5u4r$1e6ae$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 13:51:39 +0100
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <uo5u4r$1e6ae$1@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 16 Jan 2024 12:51:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bab081cccfd299fcf30f3b94f14b8bfe";
logging-data="1513806"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QH7woiWoAVu4LW6rEJpCHnOnBNJFW3p4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:/dYb3XjUGGimV3bD+xR6HpqekdA=
In-Reply-To: <uo5s8d$1cjl8$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Tue, 16 Jan 2024 12:51 UTC

On 16/01/2024 13:19, pozz wrote:
> In one project I have many quasi-fixed strings that I'd like to keep in
> non volatile memory (Flash) to avoid losing precious RAM space.
>
> static const char s1[] = "/my/very/long/string/of/01020304";
> static const char s2[] = "/another/string/01020304";
> ...
>
> Substring "01020304" is a serial number that changes during production
> with specific device. It has the same length in bytes (it's a simple hex
> representation of a 32-bits integer).
>
> Of course it's too difficult and slow to rebuild the firmware during
> production passing to the compiler the real serial number. I think a
> better solution is to patch the .hex file generated by the compiler.
>
> I'm wondering how to detect the exact positions (addresses) of serial
> numbers to fix.
>
> The build system is gcc, so I could search for s1 in the elf file. Do
> you know of a tool that returns the address of a symbol in the elf or
> map file?
>
> Could you suggest a better approach?
>

In the source code, put the serial number in as "PQRXYZ" or some other
distinct string of characters. Generate bin files, not hex (or convert
with objcopy). Then do a simple search for the special string to find
its position and replace it with the serial number using a simple Python
script or your other favourite tool (awk, sed, perl, whatever).

Oh, and in the source code, don't forget to make the string "volatile".

Re: Patch fixed strings in .hex file

<uo62jp$1fj62$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: kle...@unibwm.de (Herbert Kleebauer)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 15:07:52 +0100
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <uo62jp$1fj62$1@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 16 Jan 2024 14:07:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d8145aa051b84dbc41d11b0d4836a980";
logging-data="1559746"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX188V1oK+4dKYBbsF3zRf/A+ANOAcGYhKj0="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:6dZKSUIa8uzDGfHkS/G2whW8Kpk=
Content-Language: de-DE, en-US
In-Reply-To: <uo5s8d$1cjl8$1@dont-email.me>
 by: Herbert Kleebauer - Tue, 16 Jan 2024 14:07 UTC

On 16.01.2024 13:19, pozz wrote:
> In one project I have many quasi-fixed strings that I'd like to keep in
> non volatile memory (Flash) to avoid losing precious RAM space.
>
> static const char s1[] = "/my/very/long/string/of/01020304";
> static const char s2[] = "/another/string/01020304";
> ...
>
> Substring "01020304" is a serial number that changes during production
> with specific device. It has the same length in bytes (it's a simple hex
> representation of a 32-bits integer).
>
> Of course it's too difficult and slow to rebuild the firmware during
> production passing to the compiler the real serial number. I think a
> better solution is to patch the .hex file generated by the compiler.
>
> I'm wondering how to detect the exact positions (addresses) of serial
> numbers to fix.

Generate two binaries with different substrings and then do
a binary file compare to find the position.

Re: Patch fixed strings in .hex file

<uo64kj$1cjl7$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 15:42:29 +0100
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <uo64kj$1cjl7$1@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <uo5u4r$1e6ae$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 16 Jan 2024 14:42:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b5206c1c7cc657f768081c5a742b55e0";
logging-data="1461927"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FerCOQU5Oy3K4kNaKiKZCAIQ6lV51hj4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:TiRHQDiQpfGqrZbFdF9CO+Q04sk=
Content-Language: it
In-Reply-To: <uo5u4r$1e6ae$1@dont-email.me>
 by: pozz - Tue, 16 Jan 2024 14:42 UTC

Il 16/01/2024 13:51, David Brown ha scritto:
> On 16/01/2024 13:19, pozz wrote:
>> In one project I have many quasi-fixed strings that I'd like to keep
>> in non volatile memory (Flash) to avoid losing precious RAM space.
>>
>> static const char s1[] = "/my/very/long/string/of/01020304";
>> static const char s2[] = "/another/string/01020304";
>> ...
>>
>> Substring "01020304" is a serial number that changes during production
>> with specific device. It has the same length in bytes (it's a simple
>> hex representation of a 32-bits integer).
>>
>> Of course it's too difficult and slow to rebuild the firmware during
>> production passing to the compiler the real serial number. I think a
>> better solution is to patch the .hex file generated by the compiler.
>>
>> I'm wondering how to detect the exact positions (addresses) of serial
>> numbers to fix.
>>
>> The build system is gcc, so I could search for s1 in the elf file. Do
>> you know of a tool that returns the address of a symbol in the elf or
>> map file?
>>
>> Could you suggest a better approach?
>>
>
> In the source code, put the serial number in as "PQRXYZ" or some other
> distinct string of characters.  Generate bin files, not hex (or convert
> with objcopy).  Then do a simple search for the special string to find
> its position and replace it with the serial number using a simple Python
> script or your other favourite tool (awk, sed, perl, whatever).

I thought about this approach, but is it so difficult to have the same
exact sequence of bytes somewhere else in the output?

> Oh, and in the source code, don't forget to make the string "volatile".

Why?

Re: Patch fixed strings in .hex file

<uo64lc$1cjl7$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 15:42:54 +0100
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <uo64lc$1cjl7$2@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <uo62jp$1fj62$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 16 Jan 2024 14:42:52 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b5206c1c7cc657f768081c5a742b55e0";
logging-data="1461927"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/QXZJzhj5NMEhXEHWy/ovS6VZXSEv0ZA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:5WaxPxZg1reb4/d8CRP4Z98CjiA=
Content-Language: it
In-Reply-To: <uo62jp$1fj62$1@dont-email.me>
 by: pozz - Tue, 16 Jan 2024 14:42 UTC

Il 16/01/2024 15:07, Herbert Kleebauer ha scritto:
> On 16.01.2024 13:19, pozz wrote:
>> In one project I have many quasi-fixed strings that I'd like to keep in
>> non volatile memory (Flash) to avoid losing precious RAM space.
>>
>> static const char s1[] = "/my/very/long/string/of/01020304";
>> static const char s2[] = "/another/string/01020304";
>> ...
>>
>> Substring "01020304" is a serial number that changes during production
>> with specific device. It has the same length in bytes (it's a simple hex
>> representation of a 32-bits integer).
>>
>> Of course it's too difficult and slow to rebuild the firmware during
>> production passing to the compiler the real serial number. I think a
>> better solution is to patch the .hex file generated by the compiler.
>>
>> I'm wondering how to detect the exact positions (addresses) of serial
>> numbers to fix.
>
> Generate two binaries with different substrings and then do
> a binary file compare to find the position.

Thank you for this?

Re: Patch fixed strings in .hex file

<uo672h$bsf$1@reader1.panix.com>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.samoylyk.net!news.quux.org!weretis.net!feeder6.news.weretis.net!panix!.POSTED.24-152-157-105.fttp.usinternet.com!not-for-mail
From: inva...@invalid.invalid (Grant Edwards)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 15:24:01 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <uo672h$bsf$1@reader1.panix.com>
References: <uo5s8d$1cjl8$1@dont-email.me>
Injection-Date: Tue, 16 Jan 2024 15:24:01 -0000 (UTC)
Injection-Info: reader1.panix.com; posting-host="24-152-157-105.fttp.usinternet.com:24.152.157.105";
logging-data="12175"; mail-complaints-to="abuse@panix.com"
User-Agent: slrn/1.0.3 (Linux)
 by: Grant Edwards - Tue, 16 Jan 2024 15:24 UTC

On 2024-01-16, pozz <pozzugno@gmail.com> wrote:

> I'm wondering how to detect the exact positions (addresses) of serial
> numbers to fix.

Assuming there's a symbol associated with the address, the link map
will tell you what the address is.

--
Grant

Re: Patch fixed strings in .hex file

<uo67pr$1ge1j$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 16:36:27 +0100
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <uo67pr$1ge1j$2@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <uo5u4r$1e6ae$1@dont-email.me>
<uo64kj$1cjl7$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 16 Jan 2024 15:36:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bab081cccfd299fcf30f3b94f14b8bfe";
logging-data="1587251"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/w0zKcXmmOdl7yp6iX3YJkJCAxLhyKamE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:1l2X/WHBpu6uV3ZF/b5oXhr0/U0=
Content-Language: en-GB
In-Reply-To: <uo64kj$1cjl7$1@dont-email.me>
 by: David Brown - Tue, 16 Jan 2024 15:36 UTC

On 16/01/2024 15:42, pozz wrote:
> Il 16/01/2024 13:51, David Brown ha scritto:
>> On 16/01/2024 13:19, pozz wrote:
>>> In one project I have many quasi-fixed strings that I'd like to keep
>>> in non volatile memory (Flash) to avoid losing precious RAM space.
>>>
>>> static const char s1[] = "/my/very/long/string/of/01020304";
>>> static const char s2[] = "/another/string/01020304";
>>> ...
>>>
>>> Substring "01020304" is a serial number that changes during
>>> production with specific device. It has the same length in bytes
>>> (it's a simple hex representation of a 32-bits integer).
>>>
>>> Of course it's too difficult and slow to rebuild the firmware during
>>> production passing to the compiler the real serial number. I think a
>>> better solution is to patch the .hex file generated by the compiler.
>>>
>>> I'm wondering how to detect the exact positions (addresses) of serial
>>> numbers to fix.
>>>
>>> The build system is gcc, so I could search for s1 in the elf file. Do
>>> you know of a tool that returns the address of a symbol in the elf or
>>> map file?
>>>
>>> Could you suggest a better approach?
>>>
>>
>> In the source code, put the serial number in as "PQRXYZ" or some other
>> distinct string of characters.  Generate bin files, not hex (or
>> convert with objcopy).  Then do a simple search for the special string
>> to find its position and replace it with the serial number using a
>> simple Python script or your other favourite tool (awk, sed, perl,
>> whatever).
>
> I thought about this approach, but is it so difficult to have the same
> exact sequence of bytes somewhere else in the output?

Try it and see.

>
>
>> Oh, and in the source code, don't forget to make the string "volatile".
>
> Why?
>

If you have :

static const char s1[] = "PQRXYZ";

and your code later does, say :

const int last_digit = s1[5] - '0';

the compiler will optimise it to :

const int last_digit = '*';

i.e., it will calculate 'Z' - '0' at compile time - and if I remember by
ASCII codes correctly, that matches '*'.

You will be messing with the string behind the compiler's back. Make it
volatile. "volatile const" might be unusual, but it is useful in
exactly this kind of circumstance.

Re: Patch fixed strings in .hex file

<uo67tp$1ggju$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.bbs.nz!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 16:38:33 +0100
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <uo67tp$1ggju$1@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <uo672h$bsf$1@reader1.panix.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 16 Jan 2024 15:38:33 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bab081cccfd299fcf30f3b94f14b8bfe";
logging-data="1589886"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2XHI9W6kCUZAgUldrNmNXyz+k2KxsIJU="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:t0RgFHkXOcA7lE1FYIPL10+W8LU=
Content-Language: en-GB
In-Reply-To: <uo672h$bsf$1@reader1.panix.com>
 by: David Brown - Tue, 16 Jan 2024 15:38 UTC

On 16/01/2024 16:24, Grant Edwards wrote:
> On 2024-01-16, pozz <pozzugno@gmail.com> wrote:
>
>> I'm wondering how to detect the exact positions (addresses) of serial
>> numbers to fix.
>
> Assuming there's a symbol associated with the address, the link map
> will tell you what the address is.
>

Making the symbol extern linkage (remove the "static") would help with that!

Re: Patch fixed strings in .hex file

<uo67vv$1ggju$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 16:39:43 +0100
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <uo67vv$1ggju$2@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 16 Jan 2024 15:39:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bab081cccfd299fcf30f3b94f14b8bfe";
logging-data="1589886"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DF6L8kQJ+aw4XZA+m6eVsdj2xaIQ6PTM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:xjM86mpt2/N85S/IT7UhixwLW7g=
In-Reply-To: <uo5s8d$1cjl8$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Tue, 16 Jan 2024 15:39 UTC

On 16/01/2024 13:19, pozz wrote:
> In one project I have many quasi-fixed strings that I'd like to keep in
> non volatile memory (Flash) to avoid losing precious RAM space.
>
> static const char s1[] = "/my/very/long/string/of/01020304";
> static const char s2[] = "/another/string/01020304";
> ...
>
> Substring "01020304" is a serial number that changes during production
> with specific device. It has the same length in bytes (it's a simple hex
> representation of a 32-bits integer).
>
> Of course it's too difficult and slow to rebuild the firmware during
> production passing to the compiler the real serial number. I think a
> better solution is to patch the .hex file generated by the compiler.
>
> I'm wondering how to detect the exact positions (addresses) of serial
> numbers to fix.
>
> The build system is gcc, so I could search for s1 in the elf file. Do
> you know of a tool that returns the address of a symbol in the elf or
> map file?
>
> Could you suggest a better approach?
>

Another - perhaps more reliable - method would be to put the string in
its own section with __attribute__((section('serial_number'))), and then
have a linker file entry to fix it at a specific known address.

Re: Patch fixed strings in .hex file

<1t86o9tvhrt2a.o9j4cdok7vth$.dlg@40tude.net>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: antonio1...@hotmail.com (dalai lamah)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 16:47:51 +0100
Lines: 29
Message-ID: <1t86o9tvhrt2a.o9j4cdok7vth$.dlg@40tude.net>
References: <uo5s8d$1cjl8$1@dont-email.me> <uo5u4r$1e6ae$1@dont-email.me> <uo64kj$1cjl7$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: individual.net OYTU1jeqbDlwCYTY9Nmw2Axdj1Vom/x6Xv02Cs0JH1Y2uSTRx7
Cancel-Lock: sha1:kCWRW1WhzhBP1M8qh1nzBlXmtMU= sha256:cR1BCXMbOIB3rPg20r01EbBMqZW248UhEH9d4UaGSig=
User-Agent: 40tude_Dialog/2.0.15.1it
X-Face: #^0cuOKw--#Q:"y/,&C;}2$_tSLc]PEJ^{cm%=omYXmfR"cSLu`<R:LRUq0q8W)T~e20'Ri gN/ct+i_K+F~-u0lXixh"|ga@w^:D?@OdallB_O(Bg<4m39>9oxl2!tVF1/w
 by: dalai lamah - Tue, 16 Jan 2024 15:47 UTC

Un bel giorno pozz digitò:

>> In the source code, put the serial number in as "PQRXYZ" or some other
>> distinct string of characters.  Generate bin files, not hex (or convert
>> with objcopy).  Then do a simple search for the special string to find
>> its position and replace it with the serial number using a simple Python
>> script or your other favourite tool (awk, sed, perl, whatever).
>
> I thought about this approach, but is it so difficult to have the same
> exact sequence of bytes somewhere else in the output?

Extremely unlikely, especially since you use text strings and therefore you
actually use 64 bits (eigth ASCII characters) to represent a 32 bit number.
Besides, you don't need to use an ASCII string as the placeholder, you can
use any 64 bit number.

If for example your binary file is 1 MB, there is one chance over 2.2
trillion to have the same number duplicated somewhere else.

>> Oh, and in the source code, don't forget to make the string "volatile".
>
> Why?

To avoid that the compiler will optimize the code and "obfuscate" your
string. I don't think it is very likely, but it is not impossible,
especially if you use a very aggressive optimization level.

--
Fletto i muscoli e sono nel vuoto.

Re: Patch fixed strings in .hex file

<uo691e$1cjl7$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 16:57:36 +0100
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <uo691e$1cjl7$3@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <uo5u4r$1e6ae$1@dont-email.me>
<uo64kj$1cjl7$1@dont-email.me> <uo67pr$1ge1j$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 16 Jan 2024 15:57:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b5206c1c7cc657f768081c5a742b55e0";
logging-data="1461927"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ThNY7dMaz4E2PhGWgMugCiiGrhDHcGbs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:WBu7v+VpUfXs/suS1YW8qGTiMb8=
In-Reply-To: <uo67pr$1ge1j$2@dont-email.me>
Content-Language: it
 by: pozz - Tue, 16 Jan 2024 15:57 UTC

Il 16/01/2024 16:36, David Brown ha scritto:
> On 16/01/2024 15:42, pozz wrote:
>> Il 16/01/2024 13:51, David Brown ha scritto:
>>> On 16/01/2024 13:19, pozz wrote:
>>>> In one project I have many quasi-fixed strings that I'd like to keep
>>>> in non volatile memory (Flash) to avoid losing precious RAM space.
>>>>
>>>> static const char s1[] = "/my/very/long/string/of/01020304";
>>>> static const char s2[] = "/another/string/01020304";
>>>> ...
>>>>
>>>> Substring "01020304" is a serial number that changes during
>>>> production with specific device. It has the same length in bytes
>>>> (it's a simple hex representation of a 32-bits integer).
>>>>
>>>> Of course it's too difficult and slow to rebuild the firmware during
>>>> production passing to the compiler the real serial number. I think a
>>>> better solution is to patch the .hex file generated by the compiler.
>>>>
>>>> I'm wondering how to detect the exact positions (addresses) of
>>>> serial numbers to fix.
>>>>
>>>> The build system is gcc, so I could search for s1 in the elf file.
>>>> Do you know of a tool that returns the address of a symbol in the
>>>> elf or map file?
>>>>
>>>> Could you suggest a better approach?
>>>>
>>>
>>> In the source code, put the serial number in as "PQRXYZ" or some
>>> other distinct string of characters.  Generate bin files, not hex (or
>>> convert with objcopy).  Then do a simple search for the special
>>> string to find its position and replace it with the serial number
>>> using a simple Python script or your other favourite tool (awk, sed,
>>> perl, whatever).
>>
>> I thought about this approach, but is it so difficult to have the same
>> exact sequence of bytes somewhere else in the output?
>
> Try it and see.
>
>>
>>
>>> Oh, and in the source code, don't forget to make the string "volatile".
>>
>> Why?
>>
>
> If you have :
>
>     static const char s1[] = "PQRXYZ";
>
> and your code later does, say :
>
>     const int last_digit = s1[5] - '0';
>
> the compiler will optimise it to :
>
>     const int last_digit = '*';
>
> i.e., it will calculate 'Z' - '0' at compile time - and if I remember by
> ASCII codes correctly, that matches '*'.
>
> You will be messing with the string behind the compiler's back.  Make it
> volatile.  "volatile const" might be unusual, but it is useful in
> exactly this kind of circumstance.

Oh yes, I got the point now.

Re: Patch fixed strings in .hex file

<uo6989$1cjl8$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 17:01:15 +0100
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <uo6989$1cjl8$2@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <uo672h$bsf$1@reader1.panix.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 16 Jan 2024 16:01:13 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b5206c1c7cc657f768081c5a742b55e0";
logging-data="1461928"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ST+D7RCH5ziCjJYTAfXp0BDC6nU3w7dM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:tLeGtwNRrpvacjp8o26ZNcaRVSU=
Content-Language: it
In-Reply-To: <uo672h$bsf$1@reader1.panix.com>
 by: pozz - Tue, 16 Jan 2024 16:01 UTC

Il 16/01/2024 16:24, Grant Edwards ha scritto:
> On 2024-01-16, pozz <pozzugno@gmail.com> wrote:
>
>> I'm wondering how to detect the exact positions (addresses) of serial
>> numbers to fix.
>
> Assuming there's a symbol associated with the address, the link map
> will tell you what the address is.

The map file is simple to read by human, but I think it's better to use
some tool (readelf or objdump) that access elf file.

Even if I weren't able to create a command line for this task.

Re: Patch fixed strings in .hex file

<uo6f9m.2oc.1@stefan.msgid.phost.de>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.bbs.nz!news.mb-net.net!open-news-network.org!news.mind.de!bolzen.all.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: stefan.n...@arcor.de (Stefan Reuther)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 17:44:22 +0100
Lines: 19
Message-ID: <uo6f9m.2oc.1@stefan.msgid.phost.de>
References: <uo5s8d$1cjl8$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
X-Trace: individual.net 43mJVVKEotwTMbXaw4m8XQUs59PH/stnPIA/+J7b0g2LpIZgDJ
Cancel-Lock: sha1:K3+tQj2q1wR6K/gP/WfdfxTnXEU= sha256:buKZBoehB1cND7rgKOMTUw0nehwXOMJUeYvCnImnJRM=
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101
Thunderbird/68.12.1 Hamster/2.1.0.1538
In-Reply-To: <uo5s8d$1cjl8$1@dont-email.me>
 by: Stefan Reuther - Tue, 16 Jan 2024 16:44 UTC

Am 16.01.2024 um 13:19 schrieb pozz:
> The build system is gcc, so I could search for s1 in the elf file. Do
> you know of a tool that returns the address of a symbol in the elf or
> map file?

Last time I needed that, I hacked it up myself; at least back in 32-bit
times, ELF was not that hard (but I had to do that anyway to convert ELF
into something the controller could boot).

> Could you suggest a better approach?

Define your memory allocations explicitly. Instead of building a binary
and hacking the strings, place the strings at a fixed address and
regenerate the ELF or .hex file containing them from scratch. Whether
you then give the fixed addresses a name using linker magic, or just
cast pointers, is a matter of taste.

Stefan

Re: Patch fixed strings in .hex file

<uo6fl2$1i0eu$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 18:50:26 +0100
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <uo6fl2$1i0eu$2@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <uo5u4r$1e6ae$1@dont-email.me>
<uo64kj$1cjl7$1@dont-email.me> <1t86o9tvhrt2a.o9j4cdok7vth$.dlg@40tude.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 16 Jan 2024 17:50:26 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bab081cccfd299fcf30f3b94f14b8bfe";
logging-data="1638878"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6nd77n0eFubDyPck7oQkjK/SsPVyxIaw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:XAbULcnrmEgjcn7R2P4VIUZ4lNE=
Content-Language: en-GB
In-Reply-To: <1t86o9tvhrt2a.o9j4cdok7vth$.dlg@40tude.net>
 by: David Brown - Tue, 16 Jan 2024 17:50 UTC

On 16/01/2024 16:47, dalai lamah wrote:
> Un bel giorno pozz digitò:
>
>>> In the source code, put the serial number in as "PQRXYZ" or some other
>>> distinct string of characters.  Generate bin files, not hex (or convert
>>> with objcopy).  Then do a simple search for the special string to find
>>> its position and replace it with the serial number using a simple Python
>>> script or your other favourite tool (awk, sed, perl, whatever).
>>
>> I thought about this approach, but is it so difficult to have the same
>> exact sequence of bytes somewhere else in the output?
>
> Extremely unlikely, especially since you use text strings and therefore you
> actually use 64 bits (eigth ASCII characters) to represent a 32 bit number.
> Besides, you don't need to use an ASCII string as the placeholder, you can
> use any 64 bit number.
>
> If for example your binary file is 1 MB, there is one chance over 2.2
> trillion to have the same number duplicated somewhere else.
>
>>> Oh, and in the source code, don't forget to make the string "volatile".
>>
>> Why?
>
> To avoid that the compiler will optimize the code and "obfuscate" your
> string. I don't think it is very likely, but it is not impossible,
> especially if you use a very aggressive optimization level.
>

Actually, this sort of thing really does happen in practice. In one of
my current projects, I have some data that is filled in by
post-processing the binary file, and I had to use volatile accesses to
read the data or the compiler would optimise based on its knowledge of
the contents it saw at compile time. This is not just theoretical.

(To be fair, it is a bit more likely if - like in my case - the source
file uses null characters rather than a pseudo-random string of characters.)

Re: Patch fixed strings in .hex file

<uo6i47$211$1@reader1.panix.com>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!panix!.POSTED.24-152-157-105.fttp.usinternet.com!not-for-mail
From: inva...@invalid.invalid (Grant Edwards)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 18:32:39 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <uo6i47$211$1@reader1.panix.com>
References: <uo5s8d$1cjl8$1@dont-email.me> <uo672h$bsf$1@reader1.panix.com>
<uo67tp$1ggju$1@dont-email.me>
Injection-Date: Tue, 16 Jan 2024 18:32:39 -0000 (UTC)
Injection-Info: reader1.panix.com; posting-host="24-152-157-105.fttp.usinternet.com:24.152.157.105";
logging-data="2081"; mail-complaints-to="abuse@panix.com"
User-Agent: slrn/1.0.3 (Linux)
 by: Grant Edwards - Tue, 16 Jan 2024 18:32 UTC

On 2024-01-16, David Brown <david.brown@hesbynett.no> wrote:
> On 16/01/2024 16:24, Grant Edwards wrote:
>> On 2024-01-16, pozz <pozzugno@gmail.com> wrote:
>>
>>> I'm wondering how to detect the exact positions (addresses) of serial
>>> numbers to fix.
>>
>> Assuming there's a symbol associated with the address, the link map
>> will tell you what the address is.
>>
>
> Making the symbol extern linkage (remove the "static") would help with that!

IIRC, if you're using gcc/binutils, there are ways to get even static
symbols to show up in the link map (e.g. --fdata-sections), but making
the symbol global is smplest.

--
Grant

Re: Patch fixed strings in .hex file

<l0o0kcFat0kU1@mid.dfncis.de>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!news.dfncis.de!not-for-mail
From: HBBroe...@gmail.com (Hans-Bernhard Bröker)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 19:35:56 +0100
Lines: 21
Message-ID: <l0o0kcFat0kU1@mid.dfncis.de>
References: <uo5s8d$1cjl8$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.dfncis.de nyUr8BCEborLC1Jn1stq8w6q8XIG7H9423SpOxeQCMqHm82Yleol2ktHyq
Cancel-Lock: sha1:zromwsdaOAzE7L389IFduHSKoV4= sha256:kbTWOMzHgZWncHx66lekymIDBNvKTX8o/ebVOr698Gc=
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <uo5s8d$1cjl8$1@dont-email.me>
 by: Hans-Bernhard Bröke - Tue, 16 Jan 2024 18:35 UTC

Am 16.01.2024 um 13:19 schrieb pozz:

> I'm wondering how to detect the exact positions (addresses) of serial
> numbers to fix.

You do not.

Instead, you set up linker scripts, linker options and/or add
__attribute(()) to the variables' definitions to _place_ them at a
predetermined, fixed, known-useful location.

And do yourself one favour: have only _one_ instance of that number in
your code. Use concatenation or similar to output it where needed.

Then you can use tools like srecord GNU binutils to stamp your desired
number into that fixed location in the hex file. Professional-grade
chip flashing tools for production environments can usually do that by
themselves, so you don't even have to edit your "official" files.

Details will obviously vary by tool chain.

Re: Patch fixed strings in .hex file

<uo6ige$211$2@reader1.panix.com>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.furie.org.uk!newsfeed.endofthelinebbs.com!panix!.POSTED.24-152-157-105.fttp.usinternet.com!not-for-mail
From: inva...@invalid.invalid (Grant Edwards)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Tue, 16 Jan 2024 18:39:10 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <uo6ige$211$2@reader1.panix.com>
References: <uo5s8d$1cjl8$1@dont-email.me>
<uo6f9m.2oc.1@stefan.msgid.phost.de>
Injection-Date: Tue, 16 Jan 2024 18:39:10 -0000 (UTC)
Injection-Info: reader1.panix.com; posting-host="24-152-157-105.fttp.usinternet.com:24.152.157.105";
logging-data="2081"; mail-complaints-to="abuse@panix.com"
User-Agent: slrn/1.0.3 (Linux)
 by: Grant Edwards - Tue, 16 Jan 2024 18:39 UTC

On 2024-01-16, Stefan Reuther <stefan.news@arcor.de> wrote:
> Am 16.01.2024 um 13:19 schrieb pozz:
>> The build system is gcc, so I could search for s1 in the elf file. Do
>> you know of a tool that returns the address of a symbol in the elf or
>> map file?
>
> Last time I needed that, I hacked it up myself; at least back in 32-bit
> times, ELF was not that hard (but I had to do that anyway to convert ELF
> into something the controller could boot).

I think scanelf from pax-utils will do it.

https://github.com/gentoo/pax-utils

Re: Patch fixed strings in .hex file

<slrnuqdmbf.5gm.news-1513678000@a-tuin.ms.intern>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: news-151...@discworld.dascon.de (Michael Schwingen)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: 16 Jan 2024 19:30:55 GMT
Lines: 32
Message-ID: <slrnuqdmbf.5gm.news-1513678000@a-tuin.ms.intern>
References: <uo5s8d$1cjl8$1@dont-email.me>
<uo6f9m.2oc.1@stefan.msgid.phost.de>
X-Trace: individual.net h+z4dEXgqL91yvc7zXWTEwcCF1ZuCSaEx8/Zg4Ry8ItjIh4TPu
Cancel-Lock: sha1:kGNhdO4VVZEL7v3R5+/dqjvESLM= sha256:W6DgkZkZDeI4Kz2Elw+65eHdueYwDLG75jCeg1I1QPw=
User-Agent: slrn/1.0.3 (Linux)
 by: Michael Schwingen - Tue, 16 Jan 2024 19:30 UTC

On 2024-01-16, Stefan Reuther <stefan.news@arcor.de> wrote:
> Am 16.01.2024 um 13:19 schrieb pozz:
>> The build system is gcc, so I could search for s1 in the elf file. Do
>> you know of a tool that returns the address of a symbol in the elf or
>> map file?
>
> Last time I needed that, I hacked it up myself; at least back in 32-bit
> times, ELF was not that hard (but I had to do that anyway to convert ELF
> into something the controller could boot).

libelf should help.

The requirements sound similar to "we need to patch the checksum in the
vector table so that a LPC MCU will boot":

https://github.com/imi415/lpchecksum

It should be easy to modify that to patch serial numbers.

> Define your memory allocations explicitly. Instead of building a binary
> and hacking the strings, place the strings at a fixed address and
> regenerate the ELF or .hex file containing them from scratch. Whether
> you then give the fixed addresses a name using linker magic, or just
> cast pointers, is a matter of taste.

Yes. Placing the string in a special section via the linker script will
make it easier for the patch tool to locate the string.

cu
Michael
--
Some people have no respect of age unless it is bottled.

Re: Patch fixed strings in .hex file

<uo80im$1t6mh$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Wed, 17 Jan 2024 08:45:28 +0100
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <uo80im$1t6mh$1@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <l0o0kcFat0kU1@mid.dfncis.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 17 Jan 2024 07:45:26 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e0bcb5883b2fcb5248bc335979ad7082";
logging-data="2005713"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18pyCCAaGtAb2eCrLGplLzi/z+afV2ESk4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ad7F4kGwGIzrbp188LfWKyTBTnI=
In-Reply-To: <l0o0kcFat0kU1@mid.dfncis.de>
Content-Language: it
 by: pozz - Wed, 17 Jan 2024 07:45 UTC

Il 16/01/2024 19:35, Hans-Bernhard Bröker ha scritto:
> Am 16.01.2024 um 13:19 schrieb pozz:
>
>> I'm wondering how to detect the exact positions (addresses) of serial
>> numbers to fix.
>
> You do not.
>
> Instead, you set up linker scripts, linker options and/or add
> __attribute(()) to the variables' definitions to _place_ them at a
> predetermined, fixed, known-useful location.

Do you mean to choose by yourself the exact address of *each* string?
And where would you put them, at the beginning, in the middle or at the
end of the Flash? You need to calculate the address of the next string
from the address *and length* of the previous string. It seems to me a
tedious and error-prone job that could be done easily by the linker.

> And do yourself one favour: have only _one_ instance of that number in
> your code.  Use concatenation or similar to output it where needed.
>
> Then you can use tools like srecord GNU binutils to stamp your desired
> number into that fixed location in the hex file.  Professional-grade
> chip flashing tools for production environments can usually do that by
> themselves, so you don't even have to edit your "official" files.
>
> Details will obviously vary by tool chain.
>

Patching the .hex or .bin file replacing 8 bytes starting from a known
address is simple. I would write a Python script or would use one of
srecord[1] tools.

[1] https://srecord.sourceforge.net/

Re: Patch fixed strings in .hex file

<uo81sm$1tiin$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Wed, 17 Jan 2024 09:07:52 +0100
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <uo81sm$1tiin$1@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <l0o0kcFat0kU1@mid.dfncis.de>
<uo80im$1t6mh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 17 Jan 2024 08:07:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e0bcb5883b2fcb5248bc335979ad7082";
logging-data="2017879"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fiH1xTmDTY8R2T9LxBxZGhb41wqyNJ0k="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:45N6Ge1MLRL+oFMdc4yXr9ymtUc=
Content-Language: it
In-Reply-To: <uo80im$1t6mh$1@dont-email.me>
 by: pozz - Wed, 17 Jan 2024 08:07 UTC

Il 17/01/2024 08:45, pozz ha scritto:
> Il 16/01/2024 19:35, Hans-Bernhard Bröker ha scritto:
>> Am 16.01.2024 um 13:19 schrieb pozz:
>>
>>> I'm wondering how to detect the exact positions (addresses) of serial
>>> numbers to fix.
>>
>> You do not.
>>
>> Instead, you set up linker scripts, linker options and/or add
>> __attribute(()) to the variables' definitions to _place_ them at a
>> predetermined, fixed, known-useful location.
>
> Do you mean to choose by yourself the exact address of *each* string?
> And where would you put them, at the beginning, in the middle or at the
> end of the Flash? You need to calculate the address of the next string
> from the address *and length* of the previous string. It seems to me a
> tedious and error-prone job that could be done easily by the linker.
>
>
>> And do yourself one favour: have only _one_ instance of that number in
>> your code.  Use concatenation or similar to output it where needed.
>>
>> Then you can use tools like srecord GNU binutils to stamp your desired
>> number into that fixed location in the hex file.  Professional-grade
>> chip flashing tools for production environments can usually do that by
>> themselves, so you don't even have to edit your "official" files.
>>
>> Details will obviously vary by tool chain.
>>
>
> Patching the .hex or .bin file replacing 8 bytes starting from a known
> address is simple. I would write a Python script or would use one of
> srecord[1] tools.
>
> [1] https://srecord.sourceforge.net/
>

The command to patch 8 bytes in the address range 0x800-0x808 with the
string "01020304" would be:

srec_cat original.hex -I -E 0x800 0x808 -GEN 0x0800 0x0808 -REP_S
"01020304" -O patched.hex -I

-I is for Intel hex formato (input and output)
-E is to exclude the bytes to patch from the original hex
-GEN is to generate new bytes at a certaing range
-REP_S is the constant string to repeat in the range

In my case I don't really need to repeat the string in the range,
because the length of the string is exactly the length of the address range.

Re: Patch fixed strings in .hex file

<uo84qj$1tiio$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Wed, 17 Jan 2024 09:57:57 +0100
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <uo84qj$1tiio$1@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 17 Jan 2024 08:57:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e0bcb5883b2fcb5248bc335979ad7082";
logging-data="2017880"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zvyyFwrhDlbxSldqSd9+o9qaZ3oGjNRE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:SV1bw738WC/9QaDg6srK6RiicW4=
In-Reply-To: <uo5s8d$1cjl8$1@dont-email.me>
Content-Language: it
 by: pozz - Wed, 17 Jan 2024 08:57 UTC

Il 16/01/2024 13:19, pozz ha scritto:
> In one project I have many quasi-fixed strings that I'd like to keep in
> non volatile memory (Flash) to avoid losing precious RAM space.
>
> static const char s1[] = "/my/very/long/string/of/01020304";
> static const char s2[] = "/another/string/01020304";
> ...
>
> Substring "01020304" is a serial number that changes during production
> with specific device. It has the same length in bytes (it's a simple hex
> representation of a 32-bits integer).
>
> Of course it's too difficult and slow to rebuild the firmware during
> production passing to the compiler the real serial number. I think a
> better solution is to patch the .hex file generated by the compiler.
>
> I'm wondering how to detect the exact positions (addresses) of serial
> numbers to fix.
>
> The build system is gcc, so I could search for s1 in the elf file. Do
> you know of a tool that returns the address of a symbol in the elf or
> map file?
>
> Could you suggest a better approach?
>

With this command

readelf -s output.elf | grep string_to_patch

the output would be:

543: 00019c44 58 OBJECT LOCAL DEFAULT 1 lwt_message

In order to retrieve only the address:

readelf -s output.elf | grep string_to_patch | sed -e 's/^ *//' | sed
-e 's/ */ /g' | cut -d " " -f 2

The first sed removes all the spaces at the beginning, the second sed
squeezes multiple spaces to one and the cut command extract the second
field.

Re: Patch fixed strings in .hex file

<uo8a1l$1v1eq$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Wed, 17 Jan 2024 11:27:01 +0100
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <uo8a1l$1v1eq$1@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <l0o0kcFat0kU1@mid.dfncis.de>
<uo80im$1t6mh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 17 Jan 2024 10:27:01 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e5d003c4907c1c313df779ab33f575b8";
logging-data="2065882"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+m6F/RJJEW0XAu8zGR/cOmp8NNYcE6lBE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:E26ukGS1qso483S1S246o8s0plk=
In-Reply-To: <uo80im$1t6mh$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 17 Jan 2024 10:27 UTC

On 17/01/2024 08:45, pozz wrote:
> Il 16/01/2024 19:35, Hans-Bernhard Bröker ha scritto:
>> Am 16.01.2024 um 13:19 schrieb pozz:
>>
>>> I'm wondering how to detect the exact positions (addresses) of serial
>>> numbers to fix.
>>
>> You do not.
>>
>> Instead, you set up linker scripts, linker options and/or add
>> __attribute(()) to the variables' definitions to _place_ them at a
>> predetermined, fixed, known-useful location.
>
> Do you mean to choose by yourself the exact address of *each* string?
> And where would you put them, at the beginning, in the middle or at the
> end of the Flash? You need to calculate the address of the next string
> from the address *and length* of the previous string. It seems to me a
> tedious and error-prone job that could be done easily by the linker.
>

How many strings do you need here?

While it is possible to do all this using patching of odd places in your
file, using specific locations is often a better choice. Since you
haven't already said "Thanks for the advice - I tried it that way, it
worked, and I'm happy" in response to any post, I would say that now is
the time to take fixed location solutions seriously.

The way I always handle this is to define a struct type of fixed size,
containing all the information that might be added post-build. That can
be version information, serial numbers, length of the image (very useful
if you tag a CRC check on the end of the image), etc., - whatever you
want to add. Strings have fixed maximum sizes and space.

Make a dedicated section, and in the source code have a default instance
of the type in that section, with default values. (This is especially
handy when running from a debugger, as your elf file will not have
post-build values.) Empty strings should be all null characters.
Remember to declare it "volatile const". Your linker file specifies
that this section goes at a specific known fixed address (perhaps just
after interrupt vectors, or whatever is appropriate for your
microcontroller).

Now your post-build scripts have a simple fixed address to patch the
binaries.

>
>> And do yourself one favour: have only _one_ instance of that number in
>> your code.  Use concatenation or similar to output it where needed.
>>
>> Then you can use tools like srecord GNU binutils to stamp your desired
>> number into that fixed location in the hex file.  Professional-grade
>> chip flashing tools for production environments can usually do that by
>> themselves, so you don't even have to edit your "official" files.
>>
>> Details will obviously vary by tool chain.
>>
>
> Patching the .hex or .bin file replacing 8 bytes starting from a known
> address is simple. I would write a Python script or would use one of
> srecord[1] tools.
>
> [1] https://srecord.sourceforge.net/
>

Don't bother with hex or srec files. Use binary files - it makes things
easier.

Re: Patch fixed strings in .hex file

<uo8f5k$1veo9$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Wed, 17 Jan 2024 12:54:28 +0100
Organization: A noiseless patient Spider
Lines: 111
Message-ID: <uo8f5k$1veo9$1@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <l0o0kcFat0kU1@mid.dfncis.de>
<uo80im$1t6mh$1@dont-email.me> <uo8a1l$1v1eq$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 17 Jan 2024 11:54:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e0bcb5883b2fcb5248bc335979ad7082";
logging-data="2079497"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ncUfvdlWlOFkU3o1eSNp19/iz8vPmPvs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:mU9gQg+h6znqzHbNoFw04/k5Aak=
Content-Language: it
In-Reply-To: <uo8a1l$1v1eq$1@dont-email.me>
 by: pozz - Wed, 17 Jan 2024 11:54 UTC

Il 17/01/2024 11:27, David Brown ha scritto:
> On 17/01/2024 08:45, pozz wrote:
>> Il 16/01/2024 19:35, Hans-Bernhard Bröker ha scritto:
>>> Am 16.01.2024 um 13:19 schrieb pozz:
>>>
>>>> I'm wondering how to detect the exact positions (addresses) of
>>>> serial numbers to fix.
>>>
>>> You do not.
>>>
>>> Instead, you set up linker scripts, linker options and/or add
>>> __attribute(()) to the variables' definitions to _place_ them at a
>>> predetermined, fixed, known-useful location.
>>
>> Do you mean to choose by yourself the exact address of *each* string?
>> And where would you put them, at the beginning, in the middle or at
>> the end of the Flash? You need to calculate the address of the next
>> string from the address *and length* of the previous string. It seems
>> to me a tedious and error-prone job that could be done easily by the
>> linker.
>>
>
> How many strings do you need here?

They are 10 strings.

> While it is possible to do all this using patching of odd places in your
> file, using specific locations is often a better choice.  Since you
> haven't already said "Thanks for the advice - I tried it that way, it
> worked, and I'm happy" in response to any post, I would say that now is
> the time to take fixed location solutions seriously.

There are many suggested solutions and I think all of them can be used
with success. Just for sake of curiosity and studying, I'm exploring all
of them.

Sincerely I don't *like* solutions where you need to choose a fixed
location by yourself. Why you should make a job that can be done by the
linker?

> The way I always handle this is to define a struct type of fixed size,
> containing all the information that might be added post-build.  That can
> be version information, serial numbers, length of the image (very useful
> if you tag a CRC check on the end of the image), etc., - whatever you
> want to add.  Strings have fixed maximum sizes and space.
>
> Make a dedicated section, and in the source code have a default instance
> of the type in that section, with default values.  (This is especially
> handy when running from a debugger, as your elf file will not have
> post-build values.)  Empty strings should be all null characters.
> Remember to declare it "volatile const".  Your linker file specifies
> that this section goes at a specific known fixed address (perhaps just
> after interrupt vectors, or whatever is appropriate for your
> microcontroller).
>
> Now your post-build scripts have a simple fixed address to patch the
> binaries.

How the post-build script should know the exact address of a certain
field in the struct?

volatile const struct post_build_data {
uint32_t serial_number;
uint64_t mac_address;
uint32_t image_size;
char s1[32];
char s2[64];
char s3[13];
} post_build_data __attribute(...);

I know the fixed address of the symbol post_build_data (the only object
in my custom section), but now I have to calculate the offset of the
field s1 in the struct. This calculations is error prone.

In my opinion, it's much simpler to use a production script that
retrieves, without any error or manual calculation, the address of a
certain symbol directly from the elf.

From another post of mine:

readelf -s output.elf | grep string_to_patch | sed -e 's/^ *//' | sed -e
's/ */ /g' | cut -d " " -f 2

>>> And do yourself one favour: have only _one_ instance of that number
>>> in your code.  Use concatenation or similar to output it where needed.
>>>
>>> Then you can use tools like srecord GNU binutils to stamp your
>>> desired number into that fixed location in the hex file.
>>> Professional-grade chip flashing tools for production environments
>>> can usually do that by themselves, so you don't even have to edit
>>> your "official" files.
>>>
>>> Details will obviously vary by tool chain.
>>>
>>
>> Patching the .hex or .bin file replacing 8 bytes starting from a known
>> address is simple. I would write a Python script or would use one of
>> srecord[1] tools.
>>
>> [1] https://srecord.sourceforge.net/
>>
>
> Don't bother with hex or srec files.  Use binary files - it makes things
> easier.

Yes of course, patching an hex file or a binary file isn't the complex
task here.

Re: Patch fixed strings in .hex file

<uo8im6$20hfg$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Wed, 17 Jan 2024 13:54:29 +0100
Organization: A noiseless patient Spider
Lines: 124
Message-ID: <uo8im6$20hfg$1@dont-email.me>
References: <uo5s8d$1cjl8$1@dont-email.me> <l0o0kcFat0kU1@mid.dfncis.de>
<uo80im$1t6mh$1@dont-email.me> <uo8a1l$1v1eq$1@dont-email.me>
<uo8f5k$1veo9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 17 Jan 2024 12:54:30 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e5d003c4907c1c313df779ab33f575b8";
logging-data="2115056"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZU3j0sOQ1laJoWhni5rvuFpeDK7eEy/g="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:8L+QmPD+ZKoz5wcr3L4eOG/xqrA=
In-Reply-To: <uo8f5k$1veo9$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 17 Jan 2024 12:54 UTC

On 17/01/2024 12:54, pozz wrote:
> Il 17/01/2024 11:27, David Brown ha scritto:
>> On 17/01/2024 08:45, pozz wrote:
>>> Il 16/01/2024 19:35, Hans-Bernhard Bröker ha scritto:
>>>> Am 16.01.2024 um 13:19 schrieb pozz:
>>>>
>>>>> I'm wondering how to detect the exact positions (addresses) of
>>>>> serial numbers to fix.
>>>>
>>>> You do not.
>>>>
>>>> Instead, you set up linker scripts, linker options and/or add
>>>> __attribute(()) to the variables' definitions to _place_ them at a
>>>> predetermined, fixed, known-useful location.
>>>
>>> Do you mean to choose by yourself the exact address of *each* string?
>>> And where would you put them, at the beginning, in the middle or at
>>> the end of the Flash? You need to calculate the address of the next
>>> string from the address *and length* of the previous string. It seems
>>> to me a tedious and error-prone job that could be done easily by the
>>> linker.
>>>
>>
>> How many strings do you need here?
>
> They are 10 strings.
>

I thought you were storing serial numbers? But okay, if you need 10
strings you need 10 strings. The number is just a detail. (But if the
number were 200 strings for supporting different languages, you might do
things differently.)

>
>> While it is possible to do all this using patching of odd places in
>> your file, using specific locations is often a better choice.  Since
>> you haven't already said "Thanks for the advice - I tried it that way,
>> it worked, and I'm happy" in response to any post, I would say that
>> now is the time to take fixed location solutions seriously.
>
> There are many suggested solutions and I think all of them can be used
> with success. Just for sake of curiosity and studying, I'm exploring all
> of them.
>

Fair enough.

> Sincerely I don't *like* solutions where you need to choose a fixed
> location by yourself. Why you should make a job that can be done by the
> linker?
>

You do so because it makes live much easier. It is the same reason you
write your patching script in Python, rather than C.

>
>> The way I always handle this is to define a struct type of fixed size,
>> containing all the information that might be added post-build.  That
>> can be version information, serial numbers, length of the image (very
>> useful if you tag a CRC check on the end of the image), etc., -
>> whatever you want to add.  Strings have fixed maximum sizes and space.
> >
>> Make a dedicated section, and in the source code have a default
>> instance of the type in that section, with default values.  (This is
>> especially handy when running from a debugger, as your elf file will
>> not have post-build values.)  Empty strings should be all null
>> characters. Remember to declare it "volatile const".  Your linker file
>> specifies that this section goes at a specific known fixed address
>> (perhaps just after interrupt vectors, or whatever is appropriate for
>> your microcontroller).
>>
>> Now your post-build scripts have a simple fixed address to patch the
>> binaries.
>
> How the post-build script should know the exact address of a certain
> field in the struct?

You figure it out /once/ - using one of many possible methods. Counting
with static asserts to check, or looking at the binary after putting
canaries in the sample data.

If you think that you might change the struct often, you can use
separate variables and put them all in the same section, then look at
the map file. In practice you rarely need to do something like that.

>
> volatile const struct post_build_data {
>   uint32_t serial_number;
>   uint64_t mac_address;
>   uint32_t image_size;
>   char s1[32];
>   char s2[64];
>   char s3[13];
> } post_build_data __attribute(...);
>
> I know the fixed address of the symbol post_build_data (the only object
> in my custom section), but now I have to calculate the offset of the
> field s1 in the struct. This calculations is error prone.
>

Static assertions are your friend here.

> In my opinion, it's much simpler to use a production script that
> retrieves, without any error or manual calculation, the address of a
> certain symbol directly from the elf.
>

I doubt it is simpler. But of course it is possible, and what is
simpler for me is not necessarily the same as simpler for you.

> From another post of mine:
>
> readelf -s output.elf | grep string_to_patch | sed -e 's/^ *//' | sed -e
> 's/  */ /g' | cut -d " " -f 2
>

You are using a Python script to do the patching. Use pyelftools and do
this all in the one Python script. That way, future you who has to
maintain this system will not build a time machine to go back and
strangle the past you that thought this monster made sense. These kinds
of pipes can seem elegant, but they are write-only and a maintainer's
nightmare.

Re: Patch fixed strings in .hex file

<uo93cq.3ec.1@stefan.msgid.phost.de>

  copy mid

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

  copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: stefan.n...@arcor.de (Stefan Reuther)
Newsgroups: comp.arch.embedded
Subject: Re: Patch fixed strings in .hex file
Date: Wed, 17 Jan 2024 17:39:37 +0100
Lines: 50
Message-ID: <uo93cq.3ec.1@stefan.msgid.phost.de>
References: <uo5s8d$1cjl8$1@dont-email.me> <l0o0kcFat0kU1@mid.dfncis.de>
<uo80im$1t6mh$1@dont-email.me> <uo8a1l$1v1eq$1@dont-email.me>
<uo8f5k$1veo9$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net 7B3mFm6n3VD3JmL7v9BIQw5vdOuBDMdj7w3quR0nYuCWz+6DIc
Cancel-Lock: sha1:d0VljqX/CEJp3ia7HESNDzUDLEU= sha256:4zv1/RrdbceyeIY6EEWy7jNvAidAAoV4stwTjhUP+Kg=
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101
Thunderbird/68.12.1 Hamster/2.1.0.1538
In-Reply-To: <uo8f5k$1veo9$1@dont-email.me>
 by: Stefan Reuther - Wed, 17 Jan 2024 16:39 UTC

Am 17.01.2024 um 12:54 schrieb pozz:
> Il 17/01/2024 11:27, David Brown ha scritto:
>> While it is possible to do all this using patching of odd places in
>> your file, using specific locations is often a better choice.  Since
>> you haven't already said "Thanks for the advice - I tried it that way,
>> it worked, and I'm happy" in response to any post, I would say that
>> now is the time to take fixed location solutions seriously.
>
> There are many suggested solutions and I think all of them can be used
> with success. Just for sake of curiosity and studying, I'm exploring all
> of them.
>
> Sincerely I don't *like* solutions where you need to choose a fixed
> location by yourself. Why you should make a job that can be done by the
> linker?

It's not you vs. the linker. You co-operate. You need to tell the linker
about your chip anyway ("code is from 0x1000 to 0xc000, data is from
0xc000 to 0xd000"). So you can as well tell it "version stamp is from
0xcc00 to 0xd000, data only before 0xcc00".

If you have your identification information in a fixed place, you can,
for example, more easily analyze field returns. It's easy for your field
service has to change something, and it's easy to do software updates
that preserve the identification information. You don't need to figure
out which software build is running on the chip and what the address of
the structure happens to be in that one.

>> Now your post-build scripts have a simple fixed address to patch the
>> binaries.
>
> How the post-build script should know the exact address of a certain
> field in the struct?

By defining the struct in a compatible way. For example....

> volatile const struct post_build_data {
>   uint32_t serial_number;
>   uint64_t mac_address;

....this is a bad idea, because in most (but probably not all) chips,
uint64_t after uint32_t means there's 32 bits of padding, so if you need
serial-before-mac, you should at least make the padding explicit. There
also might be endian problems.

Using only char/uint8_t fields gives you a very high chance of identical
structure layout everywhere (`uint8_t mac_address[8]`).

Stefan

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor