Skip to contentSkip to conversation

recordmcount: Correct st_shndx handling

PATCH Superseded Mark-PK Tsai 2 participants 3 messages Jun 15, 2021 – Jun 16, 2021 Arm MediaTek

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 recordmcount: Correct st_shndx handling

From: Peter Zijlstra <[email protected]>

One should only use st_shndx when >SHN_UNDEF and <SHN_LORESERVE. When SHN_XINDEX, then use .symtab_shndx. Otherwise use 0.

This handles the case: st_shndx >= SHN_LORESERVE && st_shndx != SHN_XINDEX.

---
 scripts/recordmcount.h | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
scripts/recordmcount.h +9 -4
index f9b19524da11..ef9c3425f86b 100644
@@ -194,13 +194,18 @@ static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab,
 	unsigned long offset;
 	int index;
 
-	if (sym->st_shndx != SHN_XINDEX)
+	if (w2(sym->st_shndx) > SHN_UNDEF &&
+	    w2(sym->st_shndx) < SHN_LORESERVE)
 		return w2(sym->st_shndx);
 
-	offset = (unsigned long)sym - (unsigned long)symtab;
-	index = offset / sizeof(*sym);
+	if (w2(sym->st_shndx) == SHN_XINDEX) {
+		offset = (unsigned long)sym - (unsigned long)symtab;
+		index = offset / sizeof(*sym);
 
-	return w(symtab_shndx[index]);
+		return w(symtab_shndx[index]);
+	}
+
+	return 0;
 }
 
 static unsigned int get_shnum(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
Signature · 1 line
2.18.0
#1 lore raw
Quoted 27 lines · Mark-PK Tsai

From: Peter Zijlstra <[email protected]>

One should only use st_shndx when >SHN_UNDEF and <SHN_LORESERVE. When SHN_XINDEX, then use .symtab_shndx. Otherwise use 0.

This handles the case: st_shndx >= SHN_LORESERVE && st_shndx != SHN_XINDEX.

---
 scripts/recordmcount.h | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
scripts/recordmcount.h +2 -0
index f9b19524da11..ef9c3425f86b 100644
@@ -194,13 +194,18 @@ static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab,
 	unsigned long offset;
 	int index;
 
+	if (w2(sym->st_shndx) > SHN_UNDEF &&
+	    w2(sym->st_shndx) < SHN_LORESERVE)
 		return w2(sym->st_shndx);

scripts/recordmcount.h static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab, · line 209

Quoted 10 lines
scripts/recordmcount.h +7 -0
 
+	if (w2(sym->st_shndx) == SHN_XINDEX) {
+		offset = (unsigned long)sym - (unsigned long)symtab;
+		index = offset / sizeof(*sym);
 
+		return w(symtab_shndx[index]);
+	}
+
+	return 0;
 }

Thanks. However that leads to atrocious codegen because w2 is an indirect function, something like the below seems much better.

1d00:       41 0f b7 7f 0e          movzwl 0xe(%r15),%edi
1d05:       c0 eb 04                shr    $0x4,%bl
1d08:       ff 15 7a 54 00 00       callq  *0x547a(%rip)        # 7188 <w2>
1d0e:       85 c0                   test   %eax,%eax
1d10:       74 16                   je     1d28 <main+0xba8>
1d12:       41 0f b7 7f 0e          movzwl 0xe(%r15),%edi
1d17:       ff 15 6b 54 00 00       callq  *0x546b(%rip)        # 7188 <w2>
1d1d:       3d ff fe 00 00          cmp    $0xfeff,%eax
1d22:       0f 86 00 03 00 00       jbe    2028 <main+0xea8>
1d28:       41 0f b7 7f 0e          movzwl 0xe(%r15),%edi
1d2d:       ff 15 55 54 00 00       callq  *0x5455(%rip)        # 7188 <w2>

vs

1d0c:       41 0f b7 7f 0e          movzwl 0xe(%r15),%edi
1d11:       ff 15 71 54 00 00       callq  *0x5471(%rip)        # 7188 <w2>

---

scripts/recordmcount.h +13 -5
index f9b19524da11..b3e9d0563c03 100644
@@ -192,15 +192,23 @@ static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab,
 				 Elf32_Word const *symtab_shndx)
 {
 	unsigned long offset;
+	unsigned short shndx;
 	int index;
 
-	if (sym->st_shndx != SHN_XINDEX)
-		return w2(sym->st_shndx);
+	shndx = w2(sym->st_shndx);
 
-	offset = (unsigned long)sym - (unsigned long)symtab;
-	index = offset / sizeof(*sym);
+	if (shndx > SHN_UNDEF &&
+	    shndx < SHN_LORESERVE)
+		return shndx;
 
-	return w(symtab_shndx[index]);
+	if (shndx == SHN_XINDEX) {
+		offset = (unsigned long)sym - (unsigned long)symtab;
+		index = offset / sizeof(*sym);
+
+		return w(symtab_shndx[index]);
+	}
+
+	return 0;
 }
 
 static unsigned int get_shnum(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

#2 lore raw
Quoted 33 lines · Mark-PK Tsai
Quoted 32 lines

From: Peter Zijlstra <[email protected]>

One should only use st_shndx when >SHN_UNDEF and <SHN_LORESERVE. When SHN_XINDEX, then use .symtab_shndx. Otherwise use 0.

This handles the case: st_shndx >= SHN_LORESERVE && st_shndx != SHN_XINDEX.

---
 scripts/recordmcount.h | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
scripts/recordmcount.h +5 -0
index f9b19524da11..ef9c3425f86b 100644
@@ -194,13 +194,18 @@ static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab,
 	unsigned long offset;
 	int index;
 
+	if (w2(sym->st_shndx) > SHN_UNDEF &&
+	    w2(sym->st_shndx) < SHN_LORESERVE)
 		return w2(sym->st_shndx);
 
+	if (w2(sym->st_shndx) == SHN_XINDEX) {
+		offset = (unsigned long)sym - (unsigned long)symtab;
+		index = offset / sizeof(*sym);
 
Quoted 8 lines

scripts/recordmcount.h static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab, · line 212

Quoted 5 lines
scripts/recordmcount.h +4 -0
+		return w(symtab_shndx[index]);
+	}
+
+	return 0;
 }

Thanks. However that leads to atrocious codegen because w2 is an indirect function, something like the below seems much better.

Oh, I didn't notice that.
I'll update in v4.
Thanks!

Quoted 52 lines
1d00:       41 0f b7 7f 0e          movzwl 0xe(%r15),%edi
1d05:       c0 eb 04                shr    $0x4,%bl
1d08:       ff 15 7a 54 00 00       callq  *0x547a(%rip)        # 7188 <w2>
1d0e:       85 c0                   test   %eax,%eax
1d10:       74 16                   je     1d28 <main+0xba8>
1d12:       41 0f b7 7f 0e          movzwl 0xe(%r15),%edi
1d17:       ff 15 6b 54 00 00       callq  *0x546b(%rip)        # 7188 <w2>
1d1d:       3d ff fe 00 00          cmp    $0xfeff,%eax
1d22:       0f 86 00 03 00 00       jbe    2028 <main+0xea8>
1d28:       41 0f b7 7f 0e          movzwl 0xe(%r15),%edi
1d2d:       ff 15 55 54 00 00       callq  *0x5455(%rip)        # 7188 <w2>

vs

1d0c:       41 0f b7 7f 0e          movzwl 0xe(%r15),%edi
1d11:       ff 15 71 54 00 00       callq  *0x5471(%rip)        # 7188 <w2>

---

scripts/recordmcount.h +13 -5
index f9b19524da11..b3e9d0563c03 100644
@@ -192,15 +192,23 @@ static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab,
 				 Elf32_Word const *symtab_shndx)
 {
 	unsigned long offset;
+	unsigned short shndx;
 	int index;
 
-	if (sym->st_shndx != SHN_XINDEX)
-		return w2(sym->st_shndx);
+	shndx = w2(sym->st_shndx);
 
-	offset = (unsigned long)sym - (unsigned long)symtab;
-	index = offset / sizeof(*sym);
+	if (shndx > SHN_UNDEF &&
+	    shndx < SHN_LORESERVE)
+		return shndx;
 
-	return w(symtab_shndx[index]);
+	if (shndx == SHN_XINDEX) {
+		offset = (unsigned long)sym - (unsigned long)symtab;
+		index = offset / sizeof(*sym);
+
+		return w(symtab_shndx[index]);
+	}
+
+	return 0;
 }
 
 static unsigned int get_shnum(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)