RFC: Add ELF note for description with JSON data

Message ID 2a19f48f-2230-70c2-a6eb-de224028e8ae@embedded-brains.de
State Corrupt patch
Headers
Series RFC: Add ELF note for description with JSON data |

Checks

Context Check Description
snail/binutils-gdb-check warning Git am fail log

Commit Message

Sebastian Huber March 29, 2023, 12:44 p.m. UTC
  Hello,

I would like to add some metadata to executable ELF files for the RTEMS 
real-time operating system. It seems that ELF notes are a good way to do 
this. Would it be possible to add a standard or GNU-specific note type 
for descriptions which contain data in JSON format? This would help to 
use standard tools such as readelf to get the descriptions of such 
notes. For example:



$ readelf -n --format=json file.elf
{"note-sections": [
   {"section": ".note.gnu.json", "notes": [
     {"name": "GNU",
      "type": 6,
      "type-info": "NT_GNU_JSON",
      "description": {"key": "value"}}]}

The note was produced by:

.pushsection .note.gnu.json , "", %note
.balign 4
.long 2f - 1f
.long 4f - 3f
.long 6
1: .asciz "GNU"
2: .balign 4
3: .asciz "{\"key\": \"value\"}"
4: .balign 4
.popsection

A basic support would require the allocation of a node type number and a 
new type-specific output.

Optionally, we could add JSON output to readelf. One option would be to 
do this using libxo:

https://juniper.github.io/libxo/libxo-manual.html

Would such a feature acceptable in general?
  

Comments

Nick Clifton March 29, 2023, 2:27 p.m. UTC | #1
Hi Sebastian,

> I would like to add some metadata to executable ELF files for the RTEMS real-time operating system.

Have you considered using the --package-data linker option ?

   '--package-metadata=JSON'
      Request the creation of a '.note.package' ELF note section.  The
      contents of the note are in JSON format, as per the package
      metadata specification.  For more information see:
      https://systemd.io/ELF_PACKAGE_METADATA/ If the JSON argument is
      missing/empty then this will disable the creation of the metadata
      note, if one had been enabled by an earlier occurrence of the
      -package-metdata option.  If the linker has been built with
      libjansson, then the JSON string will be validated.

Cheers
   Nick
  
Sebastian Huber March 29, 2023, 3:04 p.m. UTC | #2
Hello Nick,

On 29.03.23 16:27, Nick Clifton wrote:
> Hi Sebastian,
> 
>> I would like to add some metadata to executable ELF files for the 
>> RTEMS real-time operating system.
> 
> Have you considered using the --package-data linker option ?
> 
>    '--package-metadata=JSON'
>       Request the creation of a '.note.package' ELF note section.  The
>       contents of the note are in JSON format, as per the package
>       metadata specification.  For more information see:
> https://systemd.io/ELF_PACKAGE_METADATA/ If the JSON argument is
>       missing/empty then this will disable the creation of the metadata
>       note, if one had been enabled by an earlier occurrence of the
>       -package-metdata option.  If the linker has been built with
>       libjansson, then the JSON string will be validated.

thanks, this looks like the thing we need.

Independent of this, selecting the output format of readelf (and other 
tools) would be nice.
  
Nick Clifton March 29, 2023, 4:46 p.m. UTC | #3
Hi Sebastian,

> Independent of this, selecting the output format of readelf (and other tools) would be nice.

Yes, sorry.  I did not read your original email properly.


 > +#define NT_GNU_JSON            6       /* Contains arbitrary JSON data.  */

I would have no objection to adding this particular note type.
It would be best if if this was agreed upon by the ELF ABI folks though.


 > Optionally, we could add JSON output to readelf. One option would be to do this using libxo:

Personally I would prefer not to complicate readelf by adding another
dependency.  Are there tools that can read JSON output from a pipe ?

I was thinking that it might be reasonable to add an option to readelf
along the lines of --note-body-parser=<program> and have it pass the
contents of any note it is displaying to <program>.  This would allow
for any kind of formatting inside notes, and would not add any dependencies
to readelf.

Cheers
   Nick
  

Patch

diff --git a/include/elf/common.h b/include/elf/common.h
index 6f64f05890c..95d6068178f 100644
--- a/include/elf/common.h
+++ b/include/elf/common.h
@@ -824,6 +824,7 @@ 
  #define NT_GNU_BUILD_ID                3       /* Generated by ld 
--build-id.  */
  #define NT_GNU_GOLD_VERSION    4       /* Generated by gold.  */
  #define NT_GNU_PROPERTY_TYPE_0  5      /* Generated by gcc.  */
+#define NT_GNU_JSON            6       /* Contains arbitrary JSON data.  */

  #define NT_GNU_BUILD_ATTRIBUTE_OPEN    0x100
  #define NT_GNU_BUILD_ATTRIBUTE_FUNC    0x101