public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Ulf Samuelsson <binutils@emagii.com>
To: Nick Clifton <nickc@redhat.com>, binutils@sourceware.org
Subject: Re: RFC: generating a header using the linker (CRC calculation)
Date: Wed, 15 Feb 2023 21:07:25 +0100	[thread overview]
Message-ID: <4e60f02f-322e-f268-cb31-a4e52a419fc6@emagii.com> (raw)
In-Reply-To: <7216fa28-4769-5bd2-a508-bf71184727fd@redhat.com>

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

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
>
>

[-- Attachment #2: crc64.c --]
[-- Type: text/x-csrc, Size: 2025 bytes --]

#include <stddef.h>
#include <stdint.h>
#include "crc64.h"
uint64_t	crc_tab64[256];
/*
 * void init_crc64_tab( void );
 *
 * For optimal speed, the CRC64 calculation uses a table with pre-calculated
 * bit patterns which are used in the XOR operations in the program. This table
 * is generated during compilation of the library and added to the library as a
 * table with constant values.
 */
void init_crc64_tab( uint64_t poly ) {

	uint64_t i;
	uint64_t j;
	uint64_t c;
	uint64_t crc;

	for (i=0; i<256; i++) {

		crc = 0;
		c   = i << 56;

		for (j=0; j<8; j++) {

			if ( ( crc ^ c ) & 0x8000000000000000ull ) crc = ( crc << 1 ) ^ poly;
			else                                       crc =   crc << 1;

			c = c << 1;
		}

		crc_tab64[i] = crc;
	}

}  /* init_crc64_tab */

/*
 * uint64_t crc_64_ecma( const unsigned char *input_str, size_t num_bytes );
 *
 * The function crc_64() calculates in one pass the 64 bit CRC value
 * for a byte string that is passed to the function together with a parameter
 * indicating the length.
 * This is used for CRC64-ECMA and CRC64-ISO
 */

uint64_t crc_64(const unsigned char *input_str, size_t num_bytes)
{
	uint64_t crc;
	const unsigned char *ptr;
	size_t a;
	crc = CRC_START_64;
	ptr = input_str;
	if ( ptr != NULL ) {
		for (a=0; a<num_bytes; a++) {
		crc = (crc << 8) ^ crc_tab64[ ((crc >> 56) ^ (uint64_t) *ptr++) & 0x00000000000000FFull ];
	}
	return crc;
}  /* crc_64 */


/*
 * The function crc_64_inv() calculates in one pass the CRC64 64 bit CRC
 * value for a byte string that is passed to the function together with a
 * parameter indicating the length.
 * This is used for CRC64-WE
 */

uint64_t crc_64_inv( const unsigned char *input_str, size_t num_bytes ) {

	uint64_t crc;
	const unsigned char *ptr;
	size_t a;

	crc = CRC_START_64_INV;
	ptr = input_str;

	if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {

		crc = (crc << 8) ^ crc_tab64[ ((crc >> 56) ^ (uint64_t) *ptr++) & 0x00000000000000FFull ];
	}

	return crc ^ 0xFFFFFFFFFFFFFFFFull;

}  /* crc_64_inv */


[-- Attachment #3: crc64.h --]
[-- Type: text/x-chdr, Size: 373 bytes --]

#include <stddef.h>
#include <stdint.h>

#define		CRC_POLY_64			0x42F0E1EBA9EA3693ull
#define		CRC_POLY_64_ISO		0xD800000000000000ull

#define		CRC_START_64		0x0000000000000000ull
#define		CRC_START_64_INV	0xFFFFFFFFFFFFFFFFull

uint64_t	crc_64    (const unsigned char *input_str, size_t num_bytes);
uint64_t	crc_64_inv(const unsigned char *input_str, size_t num_bytes);



  parent reply	other threads:[~2023-02-15 20:07 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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       ` Ulf Samuelsson [this message]
2023-02-15 21:01         ` RFC: generating a header using the linker (CRC calculation) 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4e60f02f-322e-f268-cb31-a4e52a419fc6@emagii.com \
    --to=binutils@emagii.com \
    --cc=binutils@sourceware.org \
    --cc=nickc@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).