public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* RFC: generating a header using the linker (ASCII, ASCIZ commands)
@ 2023-02-08 17:36 Ulf Samuelsson
  2023-02-13 10:09 ` Nick Clifton
  0 siblings, 1 reply; 15+ messages in thread
From: Ulf Samuelsson @ 2023-02-08 17:36 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 4287 bytes --]

Hi guys,

My very first mailing to the list. I joined due to a problem.

I was involved in an embedded project using the GNU linker.

One of the problems was that we wanted to generate a header,

and for various reasons, it was desirable to have the linker generate 
the header

==============

There are a few problems involved with that.

1: You want to compute a CRC over the text area. The text area should be 
aligned with a flash erase sector.

     We tried several approaches to align things, and gave up. We 
resorted to specify the size in advance.

     I could not see a way to align easily to the flash boundaries and 
add fill-

2: How generate the CRC? It would be good to have such an application in 
binutils.

    I ported the ielfutils from

     IAR has a windows command line program which is open source 
(ielfutils) so
     I ported that to Linux

3. You  want strings in the header. Not just BYTEs, SHORTs and LONGs etc.

     It is of course possible to emulate strings using the above 
functions but not so nice.

==============

I am thinking of adding two commands:

* ASCII     <size> , "string"

* ASCIZ   "string"

The ASCII command would allocate a fixed size area and would fill it 
from the string.

If the string is shorter, then the remainder would be cleared.
If the string is longer, then there would be an error message.

The ASCIZ command could allocate an area large enough to store the string.

===========================

I did a first prototype by patching a Yocto build.

The simplest idea I could think of was to just try to create a BYTE 
record for each byte in the field.
Obviously, creating a single field for the complete string would be better,
but I have never studied the code before, so bear with me.

The Yocto SDK on an U-Boot with a modified linker command file.

I added.

      ASCIZ "My first string"

to the linker command file.

The ASCIZ seems to work. checked U-Boot.bin with hexedit and my string 
was there.

I then tried

      ASCII  20, "My second string"

this ASCII did not work - get a syntax error.

The ASCIZ command is not flawless.

Depending on the size of the string, I can get warnings:

"warning: unsupported relocation type 1051152 at ffad8"

===========================

A summary of the changes are:

ldlex.l

+<WILD>"ASCII"                { RTOKEN(ASCII); }
+<WILD>"ASCIZ"                { RTOKEN(ASCIZ); }

ldgram.y

+%token <token> ALIGN_K BLOCK BIND QUAD SQUAD LONG SHORT BYTE ASCII ASCIZ

+    | ASCII INT ',' NAME
+        {
+          lang_add_string($2.integer, $4);
+        }
+    | ASCIZ NAME
+        {
+          lang_add_stringz($2);
+        }

ldlang.c

+void
+lang_add_string (bfd_vma size, char *s)
+{
+     lang_data_statement_type *new_stmt;
+     bfd_vma stringlen = strlen(s) + 1;    /* Add one for terminating 
'\0' */
+     bfd_vma fill_len = 0;
+     if (size == 0) {  /* Zero terminated string */
+        size = stringlen;
+     } else if (size > stringlen) {    /* Fix Size string */
+        fill_len = size - stringlen;
+     } else if (size > stringlen) {
+        /* We have an error */
+        einfo (_("%P:%pS: warning: string does not fit \"%s\"\n"), 
NULL, s);
+     }
+      /* Add byte expressions until end of string */
+     for (bfd_vma i = 0 ; i < size ; i++) {
+        new_stmt = new_stat (lang_data_statement, stat_ptr);
+        new_stmt->exp = exp_intop(s[i]);
+        new_stmt->type = BYTE;
+     }
+      /* Add byte expressions for filling to the end of the string */
+     for (bfd_vma i = 0 ; i < fill_len ; i++) {
+        new_stmt = new_stat (lang_data_statement, stat_ptr);
+        new_stmt->exp = exp_intop(0);
+        new_stmt->type = BYTE;
+     }

+}

...

+void
+lang_add_stringz (char *s)
+{
+    lang_add_string (0, s);
+}
+

On top of that, then info file is updated.

*So I wonder if this type of functionality is of interest?*

Also, what kind of glaring mistakes did I do for

a) the ASCII statment to result in a syntax error?

b) trigger the unsupported relocation.

Best Regards
Ulf Samuelsson

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-02-16  6:45 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-08 17:36 RFC: generating a header using the linker (ASCII, ASCIZ commands) Ulf Samuelsson
2023-02-13 10:09 ` Nick Clifton
2023-02-13 11:12   ` Ulf Samuelsson
2023-02-13 12:33     ` Jan Beulich
2023-02-13 15:54       ` Ulf Samuelsson
2023-02-13 14:11     ` Nick Clifton
2023-02-13 16:04       ` Ulf Samuelsson
2023-02-14 16:06         ` Nick Clifton
2023-02-14 18:12           ` Ulf Samuelsson
2023-02-15 20:07       ` RFC: generating a header using the linker (CRC calculation) Ulf Samuelsson
2023-02-15 21:01         ` Ulf Samuelsson
2023-02-15 21:29           ` Paul Koning
2023-02-15 22:08             ` Ulf Samuelsson
2023-02-15 22:11               ` Paul Koning
2023-02-16  6:45                 ` Ulf Samuelsson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).