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