Blame SOURCES/ltrace-0.7.91-cet.patch

fc6d88
diff -rup a/ltrace-elf.c b/ltrace-elf.c
fc6d88
--- a/ltrace-elf.c	2019-02-28 17:32:49.873659818 -0500
fc6d88
+++ b/ltrace-elf.c	2019-02-28 17:36:32.426779439 -0500
fc6d88
@@ -639,7 +639,21 @@ ltelf_read_elf(struct ltelf *lte, const
fc6d88
 			}
fc6d88
 		} else if (shdr.sh_type == SHT_PROGBITS
fc6d88
 			   || shdr.sh_type == SHT_NOBITS) {
fc6d88
-			if (strcmp(name, ".plt") == 0) {
fc6d88
+			if (strcmp(name, ".plt") == 0
fc6d88
+			    && lte->second_plt_seen == 0) {
fc6d88
+				lte->plt_addr = shdr.sh_addr;
fc6d88
+				lte->plt_size = shdr.sh_size;
fc6d88
+				lte->plt_data = elf_loaddata(scn, &shdr);
fc6d88
+				if (lte->plt_data == NULL)
fc6d88
+					fprintf(stderr,
fc6d88
+						"Can't load .plt data\n");
fc6d88
+				lte->plt_flags = shdr.sh_flags;
fc6d88
+			}
fc6d88
+			/* An Intel CET binary has two PLTs; the
fc6d88
+			   initial PLTGOT points to the second
fc6d88
+			   one.  */
fc6d88
+			else if (strcmp(name, ".plt.sec") == 0) {
fc6d88
+				lte->second_plt_seen = 1;
fc6d88
 				lte->plt_addr = shdr.sh_addr;
fc6d88
 				lte->plt_size = shdr.sh_size;
fc6d88
 				lte->plt_data = elf_loaddata(scn, &shdr);
fc6d88
diff -rup a/ltrace-elf.h b/ltrace-elf.h
fc6d88
--- a/ltrace-elf.h	2019-02-28 17:32:49.874660328 -0500
fc6d88
+++ b/ltrace-elf.h	2019-02-28 17:36:32.428779868 -0500
fc6d88
@@ -45,6 +45,7 @@ struct ltelf {
fc6d88
 	Elf_Data *dynsym;
fc6d88
 	size_t dynsym_count;
fc6d88
 	const char *dynstr;
fc6d88
+	int second_plt_seen;
fc6d88
 	GElf_Addr plt_addr;
fc6d88
 	GElf_Word plt_flags;
fc6d88
 	size_t plt_size;
fc6d88
diff -rup a/sysdeps/linux-gnu/x86/plt.c b/sysdeps/linux-gnu/x86/plt.c
fc6d88
--- a/sysdeps/linux-gnu/x86/plt.c	2019-02-28 17:32:49.991720041 -0500
fc6d88
+++ b/sysdeps/linux-gnu/x86/plt.c	2019-02-28 17:36:32.429780083 -0500
fc6d88
@@ -28,18 +28,18 @@
fc6d88
 #include "trace.h"
fc6d88
 
fc6d88
 static GElf_Addr
fc6d88
-x86_plt_offset(uint32_t i)
fc6d88
+x86_plt_offset(struct ltelf *lte, uint32_t i)
fc6d88
 {
fc6d88
 	/* Skip the first PLT entry, which contains a stub to call the
fc6d88
 	 * resolver.  */
fc6d88
-	return (i + 1) * 16;
fc6d88
+	return (i + (lte->second_plt_seen ? 0 : 1)) * 16;
fc6d88
 }
fc6d88
 
fc6d88
 GElf_Addr
fc6d88
 arch_plt_sym_val(struct ltelf *lte, size_t ndx, GElf_Rela *rela)
fc6d88
 {
fc6d88
 	uint32_t i = *VECT_ELEMENT(&lte->arch.plt_map, uint32_t, ndx);
fc6d88
-	return x86_plt_offset(i) + lte->plt_addr;
fc6d88
+	return x86_plt_offset(lte, i) + lte->plt_addr;
fc6d88
 }
fc6d88
 
fc6d88
 void *
fc6d88
@@ -116,6 +116,13 @@ arch_elf_init(struct ltelf *lte, struct
fc6d88
 	 *	 400426:   68 00 00 00 00          pushq  $0x0
fc6d88
 	 *	 40042b:   e9 e0 ff ff ff          jmpq   400410 <_init+0x18>
fc6d88
 	 *
fc6d88
+	 * For CET binaries it is the following:
fc6d88
+	 *
fc6d88
+	 *	13d0:       f3 0f 1e fa             endbr64 
fc6d88
+	 *	13d4:       68 27 00 00 00          pushq  $0x27  <-- index
fc6d88
+	 *	13d9:       f2 e9 71 fd ff ff       bnd jmpq 1150 <.plt>
fc6d88
+	 *	13df:       90                      nop
fc6d88
+	 *
fc6d88
          * On i386, the argument to push is an offset of relocation to
fc6d88
 	 * use.  The first PLT slot has an offset of 0x0, the second
fc6d88
 	 * 0x8, etc.  On x86_64, it's directly the index that we are
fc6d88
@@ -128,11 +135,33 @@ arch_elf_init(struct ltelf *lte, struct
fc6d88
 	unsigned int i, sz = vect_size(&lte->plt_relocs);
fc6d88
 	for (i = 0; i < sz; ++i) {
fc6d88
 
fc6d88
-		GElf_Addr offset = x86_plt_offset(i);
fc6d88
+		GElf_Addr offset = x86_plt_offset(lte, i);
fc6d88
+		uint32_t reloc_arg;
fc6d88
 
fc6d88
 		uint8_t byte;
fc6d88
-		if (elf_read_next_u8(lte->plt_data, &offset, &byte) < 0
fc6d88
-		    || byte != 0xff
fc6d88
+		if (elf_read_next_u8(lte->plt_data, &offset, &byte) < 0)
fc6d88
+		  continue;
fc6d88
+
fc6d88
+
fc6d88
+		if (byte == 0xf3
fc6d88
+		    && elf_read_next_u8(lte->plt_data, &offset, &byte) >= 0
fc6d88
+		    && byte == 0x0f
fc6d88
+		    && elf_read_next_u8(lte->plt_data, &offset, &byte) >= 0
fc6d88
+		    && byte == 0x1e
fc6d88
+		    && elf_read_next_u8(lte->plt_data, &offset, &byte) >= 0
fc6d88
+		    && byte == 0xfa
fc6d88
+		    && elf_read_next_u8(lte->plt_data, &offset, &byte) >= 0
fc6d88
+		    && byte == 0x68
fc6d88
+		    && elf_read_next_u32(lte->plt_data,
fc6d88
+					 &offset, &reloc_arg) >= 0)
fc6d88
+		  {
fc6d88
+		    /* CET */
fc6d88
+		    fprintf(stderr, "%d: reloc_arg is %lx\n", i, (long)reloc_arg);
fc6d88
+		    *VECT_ELEMENT(&lte->arch.plt_map, unsigned int, reloc_arg) = i;
fc6d88
+		    continue;
fc6d88
+		  }
fc6d88
+
fc6d88
+		if (byte != 0xff
fc6d88
 		    || elf_read_next_u8(lte->plt_data, &offset, &byte) < 0
fc6d88
 		    || (byte != 0xa3 && byte != 0x25))
fc6d88
 			continue;
fc6d88
@@ -140,7 +169,6 @@ arch_elf_init(struct ltelf *lte, struct
fc6d88
 		/* Skip immediate argument in the instruction.  */
fc6d88
 		offset += 4;
fc6d88
 
fc6d88
-		uint32_t reloc_arg;
fc6d88
 		if (elf_read_next_u8(lte->plt_data, &offset, &byte) < 0
fc6d88
 		    || byte != 0x68
fc6d88
 		    || elf_read_next_u32(lte->plt_data,