summaryrefslogtreecommitdiffstats
path: root/libebl
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2018-11-12 23:34:24 +0100
committerMark Wielaard <mark@klomp.org>2018-11-13 13:46:40 +0100
commit72e30c2e0cb49a9a300667fdd5ff09082f717950 (patch)
treed8aa22a90873f98e00df21f33c0ae40c520f0de5 /libebl
parent7a3f6fe60b8519b5372f5a5521ccbac59411f33f (diff)
Handle GNU Build Attribute ELF Notes.
GNU Build Attribute ELF Notes are generated by the GCC annobin plugin and described at https://fedoraproject.org/wiki/Toolchain/Watermark Unfortunately the constants aren't yet described in the standard glibc elf.h so they have been added to the elfutils specific elf-knowledge.h. The notes abuse the name owner field to encode some data not in the description. This makes it a bit hard to parse. We have to match the note owner name prefix (to "GA") to be sure the type is valid. We also cannot rely on the owner name being a valid C string since the attribute name and value can contain zero (terminators). So pass around namesz to the ebl note parsing functions. eu-elflint will recognize and eu-readelf -n will now show the notes: Note section [27] '.gnu.build.attributes' of 56080 bytes at offset 0x114564: Owner Data size Type GA 16 GNU Build Attribute OPEN Address Range: 0x2f30f - 0x2f30f VERSION: "3p8" GA 0 GNU Build Attribute OPEN TOOL: "gcc 8.2.1 20180801" GA 0 GNU Build Attribute OPEN "GOW": 45 GA 0 GNU Build Attribute OPEN STACK_PROT: 0 GA 0 GNU Build Attribute OPEN "stack_clash": TRUE GA 0 GNU Build Attribute OPEN "cf_protection": 0 GA 0 GNU Build Attribute OPEN "GLIBCXX_ASSERTIONS": TRUE GA 0 GNU Build Attribute OPEN "FORTIFY": 0 GA 0 GNU Build Attribute OPEN PIC: 3 GA 0 GNU Build Attribute OPEN SHORT_ENUM: FALSE GA 0 GNU Build Attribute OPEN ABI: c001100000012 GA 0 GNU Build Attribute OPEN "stack_realign": FALSE A new test was added to run-readelf -n for the existing annobin file. Signed-off-by: Mark Wielaard <mark@klomp.org>
Diffstat (limited to 'libebl')
-rw-r--r--libebl/ChangeLog8
-rw-r--r--libebl/eblobjnote.c151
-rw-r--r--libebl/eblobjnotetypename.c25
-rw-r--r--libebl/libebl.h4
4 files changed, 184 insertions, 4 deletions
diff --git a/libebl/ChangeLog b/libebl/ChangeLog
index 076596f0..79a2ff4c 100644
--- a/libebl/ChangeLog
+++ b/libebl/ChangeLog
@@ -1,3 +1,11 @@
+2018-11-12 Mark Wielaard <mark@klomp.org>
+
+ * libebl.h (ebl_object_note): Add new argument namesz.
+ * eblobjnote.c (ebl_object_note): Likewise and handle GNU Build
+ Attribute notes.
+ * eblobjnotetypename.c (ebl_object_note_type_name): Handle GNU
+ Build Attribute notes.
+
2018-11-11 Mark Wielaard <mark@klomp.org>
* eblobjnote.c (ebl_object_note): Recognize NT_VERSION with zero
diff --git a/libebl/eblobjnote.c b/libebl/eblobjnote.c
index 8fda7d99..58ac86d7 100644
--- a/libebl/eblobjnote.c
+++ b/libebl/eblobjnote.c
@@ -37,11 +37,14 @@
#include <string.h>
#include <libeblP.h>
+#include "common.h"
#include "libelfP.h"
+#include "libdwP.h"
+#include "memory-access.h"
void
-ebl_object_note (Ebl *ebl, const char *name, uint32_t type,
+ebl_object_note (Ebl *ebl, uint32_t namesz, const char *name, uint32_t type,
uint32_t descsz, const char *desc)
{
if (! ebl->object_note (name, type, descsz, desc))
@@ -135,6 +138,152 @@ ebl_object_note (Ebl *ebl, const char *name, uint32_t type,
return;
}
+ if (strncmp (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX,
+ strlen (ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX)) == 0
+ && (type == NT_GNU_BUILD_ATTRIBUTE_OPEN
+ || type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
+ {
+ /* There might or might not be a pair of addresses in the desc. */
+ if (descsz > 0)
+ {
+ printf (" Address Range: ");
+
+ union
+ {
+ Elf64_Addr a64[2];
+ Elf32_Addr a32[2];
+ } addrs;
+
+ size_t addr_size = gelf_fsize (ebl->elf, ELF_T_ADDR,
+ 2, EV_CURRENT);
+ if (descsz != addr_size)
+ printf ("<unknown data>\n");
+ else
+ {
+ Elf_Data src =
+ {
+ .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
+ .d_buf = (void *) desc, .d_size = descsz
+ };
+
+ Elf_Data dst =
+ {
+ .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
+ .d_buf = &addrs, .d_size = descsz
+ };
+
+ if (gelf_xlatetom (ebl->elf, &dst, &src,
+ elf_getident (ebl->elf,
+ NULL)[EI_DATA]) == NULL)
+ printf ("%s\n", elf_errmsg (-1));
+ else
+ {
+ if (addr_size == 4)
+ printf ("%#" PRIx32 " - %#" PRIx32 "\n",
+ addrs.a32[0], addrs.a32[1]);
+ else
+ printf ("%#" PRIx64 " - %#" PRIx64 "\n",
+ addrs.a64[0], addrs.a64[1]);
+ }
+ }
+ }
+
+ /* Most data actually is inside the name.
+ https://fedoraproject.org/wiki/Toolchain/Watermark */
+
+ /* We need at least 2 chars of data to describe the
+ attribute and value encodings. */
+ const char *data = (name
+ + strlen (ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX));
+ if (namesz < 2)
+ {
+ printf ("<insufficient data>\n");
+ return;
+ }
+
+ printf (" ");
+
+ /* In most cases the value comes right after the encoding bytes. */
+ const char *value = &data[2];
+ switch (data[1])
+ {
+ case GNU_BUILD_ATTRIBUTE_VERSION:
+ printf ("VERSION: ");
+ break;
+ case GNU_BUILD_ATTRIBUTE_STACK_PROT:
+ printf ("STACK_PROT: ");
+ break;
+ case GNU_BUILD_ATTRIBUTE_RELRO:
+ printf ("RELRO: ");
+ break;
+ case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
+ printf ("STACK_SIZE: ");
+ break;
+ case GNU_BUILD_ATTRIBUTE_TOOL:
+ printf ("TOOL: ");
+ break;
+ case GNU_BUILD_ATTRIBUTE_ABI:
+ printf ("ABI: ");
+ break;
+ case GNU_BUILD_ATTRIBUTE_PIC:
+ printf ("PIC: ");
+ break;
+ case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
+ printf ("SHORT_ENUM: ");
+ break;
+ case 32 ... 126:
+ printf ("\"%s\": ", &data[1]);
+ value += strlen (&data[1]) + 1;
+ break;
+ default:
+ printf ("<unknown>: ");
+ break;
+ }
+
+ switch (data[0])
+ {
+ case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
+ {
+ /* Any numbers are always in (unsigned) little endian. */
+ static const Dwarf dbg
+ = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
+ size_t bytes = namesz - (value - name);
+ uint64_t val;
+ if (bytes == 1)
+ val = *(unsigned char *) value;
+ else if (bytes == 2)
+ val = read_2ubyte_unaligned (&dbg, value);
+ else if (bytes == 4)
+ val = read_4ubyte_unaligned (&dbg, value);
+ else if (bytes == 8)
+ val = read_8ubyte_unaligned (&dbg, value);
+ else
+ goto unknown;
+ printf ("%" PRIx64, val);
+ }
+ break;
+ case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
+ printf ("\"%s\"", value);
+ break;
+ case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
+ printf ("TRUE");
+ break;
+ case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
+ printf ("FALSE");
+ break;
+ default:
+ {
+ unknown:
+ printf ("<unknown>");
+ }
+ break;
+ }
+
+ printf ("\n");
+
+ return;
+ }
+
/* NT_VERSION doesn't have any info. All data is in the name. */
if (descsz == 0 && type == NT_VERSION)
return;
diff --git a/libebl/eblobjnotetypename.c b/libebl/eblobjnotetypename.c
index 8cdd7819..29a5391c 100644
--- a/libebl/eblobjnotetypename.c
+++ b/libebl/eblobjnotetypename.c
@@ -1,5 +1,5 @@
/* Return note type name.
- Copyright (C) 2002, 2007, 2009, 2011, 2016 Red Hat, Inc.
+ Copyright (C) 2002, 2007, 2009, 2011, 2016, 2018 Red Hat, Inc.
This file is part of elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 2002.
@@ -79,6 +79,29 @@ ebl_object_note_type_name (Ebl *ebl, const char *name, uint32_t type,
}
}
+ if (strncmp (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX,
+ strlen (ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX)) == 0)
+ {
+ /* GNU Build Attribute notes (ab)use the owner name to store
+ most of their data. Don't decode everything here. Just
+ the type.*/
+ char *t = buf;
+ const char *gba = "GNU Build Attribute";
+ int w = snprintf (t, len, "%s ", gba);
+ t += w;
+ len -= w;
+ if (type == NT_GNU_BUILD_ATTRIBUTE_OPEN)
+ w = snprintf (t, len, "OPEN");
+ else if (type == NT_GNU_BUILD_ATTRIBUTE_FUNC)
+ w = snprintf (t, len, "FUNC");
+ else
+ w = snprintf (t, len, "%x", type);
+ t += w;
+ len -= w;
+
+ return buf;
+ }
+
if (strcmp (name, "GNU") != 0)
{
/* NT_VERSION is special, all data is in the name. */
diff --git a/libebl/libebl.h b/libebl/libebl.h
index 58306547..ca9b9fec 100644
--- a/libebl/libebl.h
+++ b/libebl/libebl.h
@@ -179,8 +179,8 @@ extern const char *ebl_object_note_type_name (Ebl *ebl, const char *name,
char *buf, size_t len);
/* Print information about object note if available. */
-extern void ebl_object_note (Ebl *ebl, const char *name, uint32_t type,
- uint32_t descsz, const char *desc);
+extern void ebl_object_note (Ebl *ebl, uint32_t namesz, const char *name,
+ uint32_t type, uint32_t descsz, const char *desc);
/* Check whether an attribute in a .gnu_attributes section is recognized.
Fills in *TAG_NAME with the name for this tag.