RFC: generating a header using the linker (CRC calculation)

Ulf Samuelsson binutils@emagii.com
Wed Feb 15 20:07:25 GMT 2023


CRC calculation in the linker

> Actually -- there might be a way to do this: A linker plugin.  The 
> linker already
> has the architecture to support plugins, and you could create a new 
> one which would
> scan the text section, compute a CRC and then insert the value into a 
> header in the
> linked image...

There is a CRC library available from a guy named Lammert Bies

This is released under an MIT license.

/*
  * Library: libcrc
  * File:    src/crc64.c
  * Author:  Lammert Bies
  *
  * This file is licensed under the MIT License as stated below
  *
  * Copyright (c) 2016 Lammert Bies
  *
  * Permission is hereby granted, free of charge, to any person 
obtaining a copy
  * of this software and associated documentation files (the 
"Software"), to deal
  * in the Software without restriction, including without limitation 
the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or 
sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be 
included in all
  * copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 
SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
DEALINGS IN THE
  * SOFTWARE.
  *
  * Description
  * -----------
  * The source file src/crc64.c contains the routines which are needed to
  * calculate a 64 bit CRC value of a sequence of bytes.
  */


The library supports a variety of CRC algorithms, but only 64-bit CRC is 
needed here.

The two enclosed files are (with minor modifications) all that are needed.

When you encounter a CRC, you generate a table based on a polynom..

ECMA:    0x42F0E1EBA9EA3693ull
ISO:        0xD800000000000000ull
custom: <whatever>


| CRC64 ISO ' (' mustbe_exp ',' mustbe_exp ')'
           {
                      init_crc64_tab(0xD800000000000000ull)
                      ecc_start = $4;
                      ecc_end  = $6;
           }

| CRC64 ECMA ' (' mustbe_exp ',' mustbe_exp ')'
           {
                      init_crc64_tab(0x42F0E1EBA9EA3693ull);
                      ecc_start = $4;
                      ecc_end  = $6;
           }
| CRC64 POLY '[' mustbe_exp ']' '(' mustbe_exp ',' mustbe_exp ')'
           {
                      init_crc64_tab($4);
                      ecc_start = $7;
                      ecc_end  = $9;
           }

When it is time to calculate the CRC you either call crc_64 or crc_64_inv
on the area you specified in the CRC64 directive.

The question is if a plugin is the right way or just add it to the 
normal build.

The code is well known, and very unlikely to change.

> Cheers
>   Nick
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: crc64.c
Type: text/x-csrc
Size: 2025 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20230215/d43d0482/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: crc64.h
Type: text/x-chdr
Size: 373 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20230215/d43d0482/attachment-0001.bin>


More information about the Binutils mailing list