libctf: add SHA-1 support for libctf

This very thin abstraction layer provides SHA-1ing facilities to all of
libctf, almost all inlined wrappers around the libiberty functionality
other than ctf_sha1_fini.

The deduplicator will use this to recursively hash types to prove their
identity.

libctf/
	* ctf-sha1.h: New, inline wrappers around sha1_init_ctx and
	sha1_process_bytes.
	* ctf-impl.h: Include it.
	(ctf_sha1_init): New.
	(ctf_sha1_add): Likewise.
	(ctf_sha1_fini): Likewise.
	* ctf-sha1.c: New, non-inline wrapper around sha1_finish_ctx
	producing strings.
	* Makefile.am: Add file.
	* Makefile.in: Regenerate.
This commit is contained in:
Nick Alcock 2020-06-05 21:10:37 +01:00
parent 6dd2819ffc
commit 1f2e8b5b87
6 changed files with 139 additions and 12 deletions

View File

@ -1,3 +1,16 @@
2020-07-22 Nick Alcock <nick.alcock@oracle.com>
* ctf-sha1.h: New, inline wrappers around sha1_init_ctx and
sha1_process_bytes.
* ctf-impl.h: Include it.
(ctf_sha1_init): New.
(ctf_sha1_add): Likewise.
(ctf_sha1_fini): Likewise.
* ctf-sha1.c: New, non-inline wrapper around sha1_finish_ctx
producing strings.
* Makefile.am: Add file.
* Makefile.in: Regenerate.
2020-07-22 Nick Alcock <nick.alcock@oracle.com>
* libctf.ver (ctf_link_set_variable_filter): Add.

View File

@ -44,7 +44,7 @@ libctf_nobfd_la_LDFLAGS = -version-info 0:0:0 @SHARED_LDFLAGS@ @VERSION_FLAGS@
libctf_nobfd_la_CPPFLAGS = $(AM_CPPFLAGS) -DNOBFD=1
libctf_nobfd_la_SOURCES = ctf-archive.c ctf-dump.c ctf-create.c ctf-decl.c ctf-error.c \
ctf-hash.c ctf-labels.c ctf-link.c ctf-lookup.c ctf-open.c \
ctf-string.c ctf-subr.c ctf-types.c ctf-util.c
ctf-sha1.c ctf-string.c ctf-subr.c ctf-types.c ctf-util.c
if NEED_CTF_QSORT_R
libctf_nobfd_la_SOURCES += ctf-qsort_r.c
endif

View File

@ -165,17 +165,18 @@ am__DEPENDENCIES_1 =
libctf_nobfd_la_DEPENDENCIES = $(am__DEPENDENCIES_1)
am__libctf_nobfd_la_SOURCES_DIST = ctf-archive.c ctf-dump.c \
ctf-create.c ctf-decl.c ctf-error.c ctf-hash.c ctf-labels.c \
ctf-link.c ctf-lookup.c ctf-open.c ctf-string.c ctf-subr.c \
ctf-types.c ctf-util.c ctf-qsort_r.c
ctf-link.c ctf-lookup.c ctf-open.c ctf-sha1.c ctf-string.c \
ctf-subr.c ctf-types.c ctf-util.c ctf-qsort_r.c
@NEED_CTF_QSORT_R_TRUE@am__objects_1 = libctf_nobfd_la-ctf-qsort_r.lo
am_libctf_nobfd_la_OBJECTS = libctf_nobfd_la-ctf-archive.lo \
libctf_nobfd_la-ctf-dump.lo libctf_nobfd_la-ctf-create.lo \
libctf_nobfd_la-ctf-decl.lo libctf_nobfd_la-ctf-error.lo \
libctf_nobfd_la-ctf-hash.lo libctf_nobfd_la-ctf-labels.lo \
libctf_nobfd_la-ctf-link.lo libctf_nobfd_la-ctf-lookup.lo \
libctf_nobfd_la-ctf-open.lo libctf_nobfd_la-ctf-string.lo \
libctf_nobfd_la-ctf-subr.lo libctf_nobfd_la-ctf-types.lo \
libctf_nobfd_la-ctf-util.lo $(am__objects_1)
libctf_nobfd_la-ctf-open.lo libctf_nobfd_la-ctf-sha1.lo \
libctf_nobfd_la-ctf-string.lo libctf_nobfd_la-ctf-subr.lo \
libctf_nobfd_la-ctf-types.lo libctf_nobfd_la-ctf-util.lo \
$(am__objects_1)
libctf_nobfd_la_OBJECTS = $(am_libctf_nobfd_la_OBJECTS)
AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
@ -190,16 +191,17 @@ libctf_nobfd_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC \
am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1)
am__libctf_la_SOURCES_DIST = ctf-archive.c ctf-dump.c ctf-create.c \
ctf-decl.c ctf-error.c ctf-hash.c ctf-labels.c ctf-link.c \
ctf-lookup.c ctf-open.c ctf-string.c ctf-subr.c ctf-types.c \
ctf-util.c ctf-qsort_r.c ctf-open-bfd.c
ctf-lookup.c ctf-open.c ctf-sha1.c ctf-string.c ctf-subr.c \
ctf-types.c ctf-util.c ctf-qsort_r.c ctf-open-bfd.c
@NEED_CTF_QSORT_R_TRUE@am__objects_2 = libctf_la-ctf-qsort_r.lo
am__objects_3 = libctf_la-ctf-archive.lo libctf_la-ctf-dump.lo \
libctf_la-ctf-create.lo libctf_la-ctf-decl.lo \
libctf_la-ctf-error.lo libctf_la-ctf-hash.lo \
libctf_la-ctf-labels.lo libctf_la-ctf-link.lo \
libctf_la-ctf-lookup.lo libctf_la-ctf-open.lo \
libctf_la-ctf-string.lo libctf_la-ctf-subr.lo \
libctf_la-ctf-types.lo libctf_la-ctf-util.lo $(am__objects_2)
libctf_la-ctf-sha1.lo libctf_la-ctf-string.lo \
libctf_la-ctf-subr.lo libctf_la-ctf-types.lo \
libctf_la-ctf-util.lo $(am__objects_2)
am_libctf_la_OBJECTS = $(am__objects_3) libctf_la-ctf-open-bfd.lo
libctf_la_OBJECTS = $(am_libctf_la_OBJECTS)
libctf_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
@ -455,8 +457,8 @@ libctf_nobfd_la_LDFLAGS = -version-info 0:0:0 @SHARED_LDFLAGS@ @VERSION_FLAGS@
libctf_nobfd_la_CPPFLAGS = $(AM_CPPFLAGS) -DNOBFD=1
libctf_nobfd_la_SOURCES = ctf-archive.c ctf-dump.c ctf-create.c \
ctf-decl.c ctf-error.c ctf-hash.c ctf-labels.c ctf-link.c \
ctf-lookup.c ctf-open.c ctf-string.c ctf-subr.c ctf-types.c \
ctf-util.c $(am__append_1)
ctf-lookup.c ctf-open.c ctf-sha1.c ctf-string.c ctf-subr.c \
ctf-types.c ctf-util.c $(am__append_1)
libctf_la_LIBADD = @BFD_LIBADD@ $(libctf_nobfd_la_LIBADD)
libctf_la_CPPFLAGS = $(AM_CPPFLAGS) -DNOBFD=0
libctf_la_DEPENDENCIES = @BFD_DEPENDENCIES@
@ -587,6 +589,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_la-ctf-open-bfd.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_la-ctf-open.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_la-ctf-qsort_r.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_la-ctf-sha1.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_la-ctf-string.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_la-ctf-subr.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_la-ctf-types.Plo@am__quote@
@ -602,6 +605,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_nobfd_la-ctf-lookup.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_nobfd_la-ctf-open.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_nobfd_la-ctf-qsort_r.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_nobfd_la-ctf-sha1.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_nobfd_la-ctf-string.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_nobfd_la-ctf-subr.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libctf_nobfd_la-ctf-types.Plo@am__quote@
@ -698,6 +702,13 @@ libctf_nobfd_la-ctf-open.lo: ctf-open.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libctf_nobfd_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libctf_nobfd_la-ctf-open.lo `test -f 'ctf-open.c' || echo '$(srcdir)/'`ctf-open.c
libctf_nobfd_la-ctf-sha1.lo: ctf-sha1.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libctf_nobfd_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libctf_nobfd_la-ctf-sha1.lo -MD -MP -MF $(DEPDIR)/libctf_nobfd_la-ctf-sha1.Tpo -c -o libctf_nobfd_la-ctf-sha1.lo `test -f 'ctf-sha1.c' || echo '$(srcdir)/'`ctf-sha1.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libctf_nobfd_la-ctf-sha1.Tpo $(DEPDIR)/libctf_nobfd_la-ctf-sha1.Plo
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='ctf-sha1.c' object='libctf_nobfd_la-ctf-sha1.lo' libtool=yes @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libctf_nobfd_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libctf_nobfd_la-ctf-sha1.lo `test -f 'ctf-sha1.c' || echo '$(srcdir)/'`ctf-sha1.c
libctf_nobfd_la-ctf-string.lo: ctf-string.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libctf_nobfd_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libctf_nobfd_la-ctf-string.lo -MD -MP -MF $(DEPDIR)/libctf_nobfd_la-ctf-string.Tpo -c -o libctf_nobfd_la-ctf-string.lo `test -f 'ctf-string.c' || echo '$(srcdir)/'`ctf-string.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libctf_nobfd_la-ctf-string.Tpo $(DEPDIR)/libctf_nobfd_la-ctf-string.Plo
@ -803,6 +814,13 @@ libctf_la-ctf-open.lo: ctf-open.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libctf_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libctf_la-ctf-open.lo `test -f 'ctf-open.c' || echo '$(srcdir)/'`ctf-open.c
libctf_la-ctf-sha1.lo: ctf-sha1.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libctf_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libctf_la-ctf-sha1.lo -MD -MP -MF $(DEPDIR)/libctf_la-ctf-sha1.Tpo -c -o libctf_la-ctf-sha1.lo `test -f 'ctf-sha1.c' || echo '$(srcdir)/'`ctf-sha1.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libctf_la-ctf-sha1.Tpo $(DEPDIR)/libctf_la-ctf-sha1.Plo
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='ctf-sha1.c' object='libctf_la-ctf-sha1.lo' libtool=yes @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libctf_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libctf_la-ctf-sha1.lo `test -f 'ctf-sha1.c' || echo '$(srcdir)/'`ctf-sha1.c
libctf_la-ctf-string.lo: ctf-string.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libctf_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libctf_la-ctf-string.lo -MD -MP -MF $(DEPDIR)/libctf_la-ctf-string.Tpo -c -o libctf_la-ctf-string.lo `test -f 'ctf-string.c' || echo '$(srcdir)/'`ctf-string.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libctf_la-ctf-string.Tpo $(DEPDIR)/libctf_la-ctf-string.Plo

View File

@ -25,6 +25,7 @@
#include <sys/param.h>
#include "ctf-decls.h"
#include <ctf-api.h>
#include "ctf-sha1.h"
#include <sys/types.h>
#include <stdlib.h>
#include <stdarg.h>
@ -499,6 +500,10 @@ extern int ctf_dynset_exists (ctf_dynset_t *, const void *key,
extern int ctf_dynset_next (ctf_dynset_t *, ctf_next_t **, void **key);
extern void *ctf_dynset_lookup_any (ctf_dynset_t *);
extern void ctf_sha1_init (ctf_sha1_t *);
extern void ctf_sha1_add (ctf_sha1_t *, const void *, size_t);
extern char *ctf_sha1_fini (ctf_sha1_t *, char *);
#define ctf_list_prev(elem) ((void *)(((ctf_list_t *)(elem))->l_prev))
#define ctf_list_next(elem) ((void *)(((ctf_list_t *)(elem))->l_next))

50
libctf/ctf-sha1.c Normal file
View File

@ -0,0 +1,50 @@
/* SHA-1 thunks.
Copyright (C) 2019 Free Software Foundation, Inc.
This file is part of libctf.
libctf is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not see
<http://www.gnu.org/licenses/>. */
#include <ctf-impl.h>
#include <ctf-sha1.h>
static const char hex[] = "0123456789abcdef";
char *
ctf_sha1_fini (ctf_sha1_t *sha1, char *buf)
{
size_t i;
/* Alignment suitable for a uint32_t. */
union
{
uint32_t align;
unsigned char digest[((CTF_SHA1_SIZE - 1) / 2) + 1];
} align;
sha1_finish_ctx (sha1, align.digest);
if (!buf)
return NULL;
buf[CTF_SHA1_SIZE - 1] = '\0';
for (i = 0; i < (CTF_SHA1_SIZE - 1) / 2; i++)
{
buf[2 * i] = hex[align.digest[i] >> 4];
buf[2 * i + 1] = hex[align.digest[i] & 0xf];
}
return buf;
}

41
libctf/ctf-sha1.h Normal file
View File

@ -0,0 +1,41 @@
/* SHA-1 thunks.
Copyright (C) 2019 Free Software Foundation, Inc.
This file is part of libctf.
libctf is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not see
<http://www.gnu.org/licenses/>. */
#ifndef _CTF_SHA1_H
#define _CTF_SHA1_H
#include "config.h"
#include "sha1.h"
#define CTF_SHA1_SIZE 41
typedef struct sha1_ctx ctf_sha1_t;
static inline void
ctf_sha1_init (ctf_sha1_t *sha1)
{
sha1_init_ctx (sha1);
}
static inline void
ctf_sha1_add (ctf_sha1_t *sha1, const void *buf, size_t len)
{
sha1_process_bytes (buf, len, sha1);
}
#endif