Blame SOURCES/ltrace-0.7.91-cet.patch

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