Blame SOURCES/ltrace-0.7.91-cet.patch

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