From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from emagii.se (www.emagii.com [185.133.207.17]) by sourceware.org (Postfix) with ESMTPS id 280A03858281 for ; Thu, 16 Feb 2023 20:40:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 280A03858281 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=emagii.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=emagii.com Received: from valinor.ownit.se (84-55-68-216.customers.ownit.se [84.55.68.216]) by emagii.se (Postfix) with ESMTPSA id 008CF120271; Thu, 16 Feb 2023 21:40:11 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emagii.com; s=default; t=1676580012; bh=GCgg2rMy9TVQE6Vquzk6ZJBRH2YJKEw0Ar6yWXsVUX8=; h=From:To:Subject; b=IrYvPOthKC+zcb7nPPJvYW5JS0yryrA01LTTj8i7QmR4OnHKhC4la5zCk/Fp9t9y6 fd+4q7geb46OhTI/lC0vBFOfxE15vQs35kR/8IAvQtt6KD6OtbJqCHIqwRXigHlIh/ HQimPmRZGtWLa5WFLQ4cq/+ovWQm/s+G7vMk7Xyc= Authentication-Results: emagii.beebytevps.io; spf=pass (sender IP is 84.55.68.216) smtp.mailfrom=binutils@emagii.com smtp.helo=valinor.ownit.se Received-SPF: pass (emagii.beebytevps.io: connection is authenticated) From: binutils@emagii.com To: binutils@sourceware.org Cc: nickc@redhat.com, Ulf Samuelsson Subject: [PATCH v0 5/6] ldlang.c: CRC64 Date: Thu, 16 Feb 2023 21:40:05 +0100 Message-Id: <20230216204006.1977-6-binutils@emagii.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230216204006.1977-1-binutils@emagii.com> References: <20230216204006.1977-1-binutils@emagii.com> X-PPP-Message-ID: <167658001228.3912074.4718280931711575654@localhost.localdomain> X-PPP-Vhost: emagii.com X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_NUMSUBJECT,SPF_HELO_FAIL,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Ulf Samuelsson Signed-off-by: Ulf Samuelsson --- ld/ldlang.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/ld/ldlang.c b/ld/ldlang.c index 2852a4222d3..e0bb079b34e 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -42,6 +42,8 @@ #include "demangle.h" #include "hashtab.h" #include "elf-bfd.h" +#include "checksum.h" + #if BFD_SUPPORTS_PLUGINS #include "plugin.h" #endif /* BFD_SUPPORTS_PLUGINS */ @@ -145,6 +147,11 @@ int lang_statement_iteration = 0; /* Count times through one_lang_size_sections_pass after mark phase. */ static int lang_sizing_iteration = 0; +/* CRC calculation on output section */ +bfd_vma crc64_poly = CRC_POLY_64; /* Default Polynome is CRC64 ECMA */ +bfd_vma *crc64_tab = NULL; +bool crc64_invert= false; + /* Return TRUE if the PATTERN argument is a wildcard pattern. Although backslashes are treated specially if a pattern contains wildcards, we do not consider the mere presence of a backslash to @@ -8524,6 +8531,131 @@ lang_add_attribute (enum statement_enum attribute) new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); } +/* + * bfd_vma *init_crc64_tab( bfd_vma poly ) ; + * + * 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 on request and is available as a table with constant values. + * init_crc64_tab is copyright (c) 2016 Lammert Bies + */ +static +bfd_vma *init_crc64_tab( bfd_vma poly ) { + + bfd_vma i; + bfd_vma j; + bfd_vma c; + bfd_vma crc; + bfd_vma *crc_tab; + + crc_tab = malloc(256 * sizeof(bfd_vma)); + if (crc_tab == NULL) + return NULL; + + 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_tab[i] = crc; + } + return crc_tab; + +} /* init_crc64_tab */ + +/* + * bfd_vma 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 + * crc_64 is copyright (c) 2016 Lammert Bies + */ + +bfd_vma crc_64(const unsigned char *input_str, size_t num_bytes) +{ + bfd_vma crc; + const unsigned char *ptr; + size_t a; + crc = CRC_START_64; + ptr = input_str; + if ( ptr != NULL ) { + for (a=0; a> 56) ^ (bfd_vma) *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 + * crc_64_inv is copyright (c) 2016 Lammert Bies + */ + +bfd_vma crc_64_inv( const unsigned char *input_str, size_t num_bytes ) { + + bfd_vma crc; + const unsigned char *ptr; + size_t a; + + crc = CRC_START_64_INV; + ptr = input_str; + + if ( ptr != NULL ) { + for (a=0; a> 56) ^ (bfd_vma) *ptr++) & 0x00000000000000FFull ]; + } + } + + return crc ^ 0xFFFFFFFFFFFFFFFFull; + +} /* crc_64_inv */ + +extern void lang_add_crc_syndrome(bool invert, bfd_vma poly) +{ + crc64_poly = poly; /* Set the polynom */ + crc64_invert = invert; + printf("Adding Syndrome: 0x%08lx\n", poly); + lang_add_data (QUAD, exp_intop (0)); /* Reserve room for the ECC value */ + if (crc64_tab == NULL) + { + crc64_tab = init_crc64_tab(crc64_poly); + } + else + { + einfo (_("%P:%pS: warning: CRC polynome declared twice (ignored)\n"), NULL); + } +} + +extern void lang_add_crc_table(void) +{ + if (crc64_tab == NULL) + { + crc64_tab = init_crc64_tab(crc64_poly); + if (crc64_tab == NULL) + { + einfo (_("%F%P: can not allocate memory for CRC table: %E\n")); + return; + } + } + for (bfd_vma i = 0 ; i < 256 ; i++) + { + lang_add_data (QUAD, exp_intop (crc64_tab[i])); + } +} + void lang_startup (const char *name) { -- 2.17.1