Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #32130

Re: Patch fixed strings in .hex file

From pozz <pozzugno@gmail.com>
Newsgroups comp.arch.embedded
Subject Re: Patch fixed strings in .hex file
Date 2024-01-16 16:57 +0100
Organization A noiseless patient Spider
Message-ID <uo691e$1cjl7$3@dont-email.me> (permalink)
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>

Show all headers | View raw


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.

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Patch fixed strings in .hex file pozz <pozzugno@gmail.com> - 2024-01-16 13:19 +0100
  Re: Patch fixed strings in .hex file David Brown <david.brown@hesbynett.no> - 2024-01-16 13:51 +0100
    Re: Patch fixed strings in .hex file pozz <pozzugno@gmail.com> - 2024-01-16 15:42 +0100
      Re: Patch fixed strings in .hex file David Brown <david.brown@hesbynett.no> - 2024-01-16 16:36 +0100
        Re: Patch fixed strings in .hex file pozz <pozzugno@gmail.com> - 2024-01-16 16:57 +0100
      Re: Patch fixed strings in .hex file dalai lamah <antonio12358@hotmail.com> - 2024-01-16 16:47 +0100
        Re: Patch fixed strings in .hex file David Brown <david.brown@hesbynett.no> - 2024-01-16 18:50 +0100
  Re: Patch fixed strings in .hex file Herbert Kleebauer <klee@unibwm.de> - 2024-01-16 15:07 +0100
    Re: Patch fixed strings in .hex file pozz <pozzugno@gmail.com> - 2024-01-16 15:42 +0100
  Re: Patch fixed strings in .hex file Grant Edwards <invalid@invalid.invalid> - 2024-01-16 15:24 +0000
    Re: Patch fixed strings in .hex file David Brown <david.brown@hesbynett.no> - 2024-01-16 16:38 +0100
      Re: Patch fixed strings in .hex file Grant Edwards <invalid@invalid.invalid> - 2024-01-16 18:32 +0000
    Re: Patch fixed strings in .hex file pozz <pozzugno@gmail.com> - 2024-01-16 17:01 +0100
  Re: Patch fixed strings in .hex file David Brown <david.brown@hesbynett.no> - 2024-01-16 16:39 +0100
    Re: Patch fixed strings in .hex file Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2024-01-17 20:19 +0200
  Re: Patch fixed strings in .hex file Stefan Reuther <stefan.news@arcor.de> - 2024-01-16 17:44 +0100
    Re: Patch fixed strings in .hex file Grant Edwards <invalid@invalid.invalid> - 2024-01-16 18:39 +0000
    Re: Patch fixed strings in .hex file Michael Schwingen <news-1513678000@discworld.dascon.de> - 2024-01-16 19:30 +0000
  Re: Patch fixed strings in .hex file Hans-Bernhard Bröker <HBBroeker@gmail.com> - 2024-01-16 19:35 +0100
    Re: Patch fixed strings in .hex file pozz <pozzugno@gmail.com> - 2024-01-17 08:45 +0100
      Re: Patch fixed strings in .hex file pozz <pozzugno@gmail.com> - 2024-01-17 09:07 +0100
      Re: Patch fixed strings in .hex file David Brown <david.brown@hesbynett.no> - 2024-01-17 11:27 +0100
        Re: Patch fixed strings in .hex file pozz <pozzugno@gmail.com> - 2024-01-17 12:54 +0100
          Re: Patch fixed strings in .hex file David Brown <david.brown@hesbynett.no> - 2024-01-17 13:54 +0100
          Re: Patch fixed strings in .hex file Stefan Reuther <stefan.news@arcor.de> - 2024-01-17 17:39 +0100
            Re: Patch fixed strings in .hex file David Brown <david.brown@hesbynett.no> - 2024-01-17 18:28 +0100
            Re: Patch fixed strings in .hex file Grant Edwards <invalid@invalid.invalid> - 2024-01-17 19:00 +0000
            Re: Patch fixed strings in .hex file pozz <pozzugno@gmail.com> - 2024-01-19 09:10 +0100
      Re: Patch fixed strings in .hex file Hans-Bernhard Bröker <HBBroeker@gmail.com> - 2024-01-18 18:49 +0100
  Re: Patch fixed strings in .hex file pozz <pozzugno@gmail.com> - 2024-01-17 09:57 +0100
  Re: Patch fixed strings in .hex file Don Y <blockedofcourse@foo.invalid> - 2024-01-17 10:03 -0700
  Re: Patch fixed strings in .hex file Paul Rubin <no.email@nospam.invalid> - 2024-01-17 21:42 -0800

csiph-web