public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: David Malcolm <dmalcolm@redhat.com>
Subject: [PATCH 07/22] Add minimal version of Nick Clifton's annobin code
Date: Fri, 04 Aug 2017 21:36:00 -0000	[thread overview]
Message-ID: <1501884293-9047-8-git-send-email-dmalcolm@redhat.com> (raw)
In-Reply-To: <1501884293-9047-1-git-send-email-dmalcolm@redhat.com>

This patch provides a way to "watermark" binaries with
metadata.  It's used later in the patch kit to watermark
binaries with static analysis results and metadata.

See:
  https://fedoraproject.org/wiki/Toolchain/Watermark

Note: this is a version of Nick Clifton's "annobin" gcc plugin:
  https://nickc.fedorapeople.org/
heavily hacked up by me:
* removed everything (including plugin support) not needed by
  later patches in the kit
* rewritten as an API, rather than as a plugin
* removed annobin_inform (..., "ICE: ...") calls in favor of
  gcc_assert.
* line-wrapped
* added a annobin_ensure_init to initialize annobin_is_64bit.
* added #ifndef guard to annobin.h

It includes the commits:
* Remove size limit on string passed to annobin_output_string_note
* Version 2 of spec: Add a GA prefix to all names

gcc/ChangeLog:
	* Makefile.in (OBJS): Add annobin.o.
	* annobin.cc: New file.
	* annobin.h: New file.
---
 gcc/Makefile.in |   1 +
 gcc/annobin.cc  | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gcc/annobin.h   |  44 ++++++++++++++
 3 files changed, 230 insertions(+)
 create mode 100644 gcc/annobin.cc
 create mode 100644 gcc/annobin.h

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 9ceb3f3..319e3f3 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -1216,6 +1216,7 @@ OBJS = \
 	ggc-page.o \
 	alias.o \
 	alloc-pool.o \
+	annobin.o \
 	auto-inc-dec.o \
 	auto-profile.o \
 	bb-reorder.o \
diff --git a/gcc/annobin.cc b/gcc/annobin.cc
new file mode 100644
index 0000000..ad8e49a
--- /dev/null
+++ b/gcc/annobin.cc
@@ -0,0 +1,185 @@
+/* annobin - support for annotating binary files.
+   Copyright (c) 2017 Red Hat.
+   Created by Nick Clifton.
+   Heavily hacked up by David Malcolm.
+
+  This 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.
+
+  It 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.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "diagnostic-core.h"
+#include "annobin.h"
+#include "output.h"
+
+/* Internal variable, used by target specific parts of the annobin plugin as well
+   as this generic part.  True if the object file being generated is for a 64-bit
+   target.  */
+bool           annobin_is_64bit = false;
+
+static void
+annobin_ensure_init (void)
+{
+  static bool done_once = false;
+  if (done_once)
+    return;
+  done_once = true;
+
+  /* Compute the default data size.  */
+  switch (POINTER_SIZE)
+    {
+    case 16:
+    case 32:
+      annobin_is_64bit = false; break;
+    case 64:
+      annobin_is_64bit = true; break;
+    default:
+      sorry ("unknown target pointer size: %d", POINTER_SIZE);
+    }
+}
+
+void
+annobin_output_note (const void * name, unsigned namesz, bool name_is_string,
+		     const char * name_description,
+		     const void * desc, unsigned descsz, bool desc_is_string,
+		     unsigned type)
+{
+  annobin_ensure_init ();
+
+  unsigned i;
+
+  if (type == NT_GNU_BUILD_ATTRIBUTE_FUNC
+      || type == NT_GNU_BUILD_ATTRIBUTE_OPEN)
+    {
+      fprintf (asm_out_file, "\t.pushsection %s\n",
+	       GNU_BUILD_ATTRS_SECTION_NAME);
+    }
+
+  if (name == NULL)
+    {
+      gcc_assert (namesz == 0);
+      fprintf (asm_out_file, "\t.dc.l 0\t\t%s no name\n", ASM_COMMENT_START);
+    }
+  else if (name_is_string)
+    {
+      gcc_assert (strlen ((const char *) name) == namesz - 1);
+      fprintf (asm_out_file, "\t.dc.l %u \t%s namesz = strlen (%s)\n", namesz,
+               ASM_COMMENT_START, (const char *) name);
+    }
+  else
+    fprintf (asm_out_file, "\t.dc.l %u\t\t%s size of name\n", namesz,
+	     ASM_COMMENT_START);
+
+  if (desc == NULL)
+    {
+      gcc_assert (descsz == 0);
+      fprintf (asm_out_file, "\t.dc.l 0\t\t%s no description\n",
+	       ASM_COMMENT_START);
+    }
+  else if (desc_is_string)
+    {
+      gcc_assert (descsz == (annobin_is_64bit ? 8 : 4));
+      fprintf (asm_out_file, "\t.dc.l %u\t\t%s descsz = sizeof (address)\n",
+	       descsz, ASM_COMMENT_START);
+    }
+  else
+    fprintf (asm_out_file, "\t.dc.l %u\t\t%s size of description\n", descsz,
+	     ASM_COMMENT_START);
+
+  fprintf (asm_out_file, "\t.dc.l %#x\t%s type = %s\n", type, ASM_COMMENT_START,
+	   type == NT_GNU_BUILD_ATTRIBUTE_OPEN ? "OPEN" :
+	   type == NT_GNU_BUILD_ATTRIBUTE_FUNC ? "FUNC" :
+	   type == NT_GNU_PROPERTY_TYPE_0      ? "PROPERTY_TYPE_0"
+	   : "*UNKNOWN*");
+
+  if (name)
+    {
+      if (name_is_string)
+	{
+	  fprintf (asm_out_file, "\t.asciz \"%s\"", (const char *)name);
+	}
+      else
+	{
+	  fprintf (asm_out_file, "\t.dc.b");
+	  for (i = 0; i < namesz; i++)
+	    fprintf (asm_out_file, " %#x%c",
+		     ((const unsigned char *) name)[i],
+		     i < (namesz - 1) ? ',' : ' ');
+	}
+
+      fprintf (asm_out_file, "\t%s name (%s)\n",
+	       ASM_COMMENT_START, name_description);
+
+      if (namesz % 4)
+	{
+	  fprintf (asm_out_file, "\t.dc.b");
+	  while (namesz % 4)
+	    {
+	      namesz++;
+	      fprintf (asm_out_file, " 0%c", namesz % 4 ? ',' : ' ');
+	    }
+	  fprintf (asm_out_file, "\t%s Padding\n", ASM_COMMENT_START);
+	}
+    }
+
+  if (desc)
+    {
+      if (desc_is_string)
+	{
+	  /* The DESCRIPTION string is the name of a symbol.  We want to produce
+	     a reference to this symbol of the appropriate size for the target
+	     architecture.  */
+	  if (annobin_is_64bit)
+	    fprintf (asm_out_file, "\t.quad %s", (const char *)desc);
+	  else
+	    fprintf (asm_out_file, "\t.dc.l %s", (const char *)desc);
+	  fprintf (asm_out_file, "\t%s description (symbol name)\n",
+		   ASM_COMMENT_START);
+	}
+      else
+	{
+	  fprintf (asm_out_file, "\t.dc.b");
+
+	  for (i = 0; i < descsz; i++)
+	    {
+	      fprintf (asm_out_file, " %#x", ((const unsigned char *) desc)[i]);
+	      if (i == (descsz - 1))
+		fprintf (asm_out_file, "\t%s description\n", ASM_COMMENT_START);
+	      else if ((i % 8) == 7)
+		fprintf (asm_out_file, "\t%s description\n\t.dc.b",
+			 ASM_COMMENT_START);
+	      else
+		fprintf (asm_out_file, ",");
+	    }
+
+	  if (descsz % 4)
+	    {
+	      fprintf (asm_out_file, "\t.dc.b");
+	      while (descsz % 4)
+		{
+		  descsz++;
+		  fprintf (asm_out_file, " 0%c", descsz % 4 ? ',' : ' ');
+		}
+	      fprintf (asm_out_file, "\t%s Padding\n", ASM_COMMENT_START);
+	    }
+	}
+    }
+
+  if (type == NT_GNU_BUILD_ATTRIBUTE_FUNC
+      || type == NT_GNU_BUILD_ATTRIBUTE_OPEN)
+    {
+      fprintf (asm_out_file, "\t.popsection\n");
+      fflush (asm_out_file);
+    }
+
+  fprintf (asm_out_file, "\n");
+}
diff --git a/gcc/annobin.h b/gcc/annobin.h
new file mode 100644
index 0000000..76eb01c
--- /dev/null
+++ b/gcc/annobin.h
@@ -0,0 +1,44 @@
+/* annobin - support for annotating binary files.
+   Copyright (c) 2017 Red Hat.
+   Created by Nick Clifton.
+   Heavily hacked up by David Malcolm.
+
+  This 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.
+
+  It 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.  */
+
+#ifndef GCC_ANNOBIN_H
+#define GCC_ANNOBIN_H
+
+#define SHF_GNU_BUILD_NOTE      (1 << 20)	/* Section contains GNU BUILD ATTRIBUTE notes.  */
+#define NT_GNU_PROPERTY_TYPE_0  5		/* Generated by gcc.  */
+
+#define NT_GNU_BUILD_ATTRIBUTE_OPEN	0x100
+#define NT_GNU_BUILD_ATTRIBUTE_FUNC	0x101
+
+#define GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC	'*'
+#define GNU_BUILD_ATTRIBUTE_TYPE_STRING		'$'
+#define GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE	'+'
+#define GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE	'!'
+
+#define GNU_BUILD_ATTRIBUTE_VERSION	1
+#define GNU_BUILD_ATTRIBUTE_STACK_PROT	2
+#define GNU_BUILD_ATTRIBUTE_RELRO	3
+#define GNU_BUILD_ATTRIBUTE_STACK_SIZE	4
+#define GNU_BUILD_ATTRIBUTE_TOOL	5
+#define GNU_BUILD_ATTRIBUTE_ABI		6
+#define GNU_BUILD_ATTRIBUTE_PIC		7
+#define GNU_BUILD_ATTRIBUTE_SHORT_ENUM	8
+
+#define NOTE_GNU_PROPERTY_SECTION_NAME	".note.gnu.property"
+#define GNU_BUILD_ATTRS_SECTION_NAME	".gnu.build.attributes"
+
+extern void annobin_output_note (const void *, unsigned, bool, const char *, const void *, unsigned, bool, unsigned);
+
+#endif  /* GCC_ANNOBIN_H  */
-- 
1.8.5.3

  parent reply	other threads:[~2017-08-04 21:36 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-04 21:30 [PATCH 00/22] RFC: integrated 3rd-party static analysis support David Malcolm
2017-08-04 21:30 ` [PATCH 02/22] libcpp: add linemap_position_for_file_line_and_column David Malcolm
2017-09-01 17:50   ` Jeff Law
2017-08-04 21:30 ` [PATCH 01/22] Expose assert_loceq outside of input.c; add ASSERT_LOCEQ David Malcolm
2017-09-01 17:49   ` Jeff Law
2017-08-04 21:30 ` [PATCH 03/22] Add JSON implementation David Malcolm
2017-09-01 17:56   ` Jeff Law
2017-08-04 21:36 ` [PATCH 08/22] Add GNU_BUILD_ATTRIBUTE_STATIC_ANALYSIS to annobin.h David Malcolm
2017-08-04 21:36 ` [PATCH 10/22] Add checkers.h/cc David Malcolm
2017-08-04 21:36 ` [PATCH 19/22] Add checkers/ianal.py David Malcolm
2017-08-04 21:36 ` [PATCH 16/22] Add checkers/coverity.py David Malcolm
2017-08-04 21:36 ` [PATCH 22/22] Add contrib/get-static-analysis.py David Malcolm
2017-08-04 21:36 ` [PATCH 09/22] Add selftest::read_file (..., FILE *, ...) David Malcolm
2017-08-04 21:36 ` [PATCH 17/22] Add checkers/cppcheck.py David Malcolm
2017-08-04 21:36 ` David Malcolm [this message]
2017-09-01 18:17   ` [PATCH 07/22] Add minimal version of Nick Clifton's annobin code Jeff Law
2017-08-04 21:37 ` [PATCH 11/22] Add checkers/test-sources David Malcolm
2017-08-04 21:37 ` [PATCH 15/22] Add checkers/clang_analyzer.py David Malcolm
2017-08-04 21:37 ` [PATCH 04/22] Add firehose.h/cc David Malcolm
2017-08-04 21:38 ` [PATCH 13/22] Add checkers/checker.py David Malcolm
2017-08-04 21:38 ` [PATCH 06/22] Makefile.in: hack in -lpthread David Malcolm
2017-09-01 18:13   ` Jeff Law
2017-08-04 21:38 ` [PATCH 12/22] Add -Wrun-analyzers= to common.opt, toplev.c, and invoke.texi David Malcolm
2017-08-04 21:38 ` [PATCH 14/22] Add checkers/always_fails.py David Malcolm
2017-08-04 21:38 ` [PATCH 21/22] Add checkers/Makefile David Malcolm
2017-08-04 21:39 ` [PATCH 05/22] diagnostic.c/h: add support for external tools David Malcolm
2017-09-01 18:18   ` Jeff Law
2017-08-04 21:39 ` [PATCH 20/22] Add checkers/splint.py David Malcolm
2017-08-04 21:39 ` [PATCH 18/22] Add checkers/flawfinder.py David Malcolm
2017-08-05  1:00 ` [PATCH 00/22] RFC: integrated 3rd-party static analysis support Eric Gallager
2017-08-08  0:23   ` David Malcolm
2017-08-06 21:21 ` Martin Sebor
2017-08-08 17:57 ` Richard Sandiford
2017-09-01 17:46 ` Jeff Law
2017-09-02  2:46   ` Trevor Saunders

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=1501884293-9047-8-git-send-email-dmalcolm@redhat.com \
    --to=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /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).