From 4223c83c4bb6ea3aeee68f1810ca86a8fb785f86 Mon Sep 17 00:00:00 2001
From: midipix <writeonce@midipix.org>
Date: Tue, 6 Feb 2024 20:43:10 +0000
Subject: slbt_get_archive_meta(): bsd symrefs vector: fix ar_name_offset
 logic.

---
 src/arbits/slbt_archive_meta.c |  6 ++++--
 src/arbits/slbt_armap_bsd_32.c | 13 +++++++++++--
 src/arbits/slbt_armap_bsd_64.c | 17 +++++++++++++++--
 3 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/src/arbits/slbt_archive_meta.c b/src/arbits/slbt_archive_meta.c
index 1af0ab8..8c8f999 100644
--- a/src/arbits/slbt_archive_meta.c
+++ b/src/arbits/slbt_archive_meta.c
@@ -762,7 +762,8 @@ int slbt_get_archive_meta(
 		symrefs_32 = m->armaps.armap_symrefs_32;
 
 		for (idx=0; idx<m->armaps.armap_nsyms; idx++) {
-			symrefs_32[idx].ar_name_offset = m->symstrv[idx] - m->symstrv[0];
+			if (m->armaps.armap_common_32.ar_armap_attr & AR_ARMAP_ATTR_SYSV)
+				symrefs_32[idx].ar_name_offset = m->symstrv[idx] - m->symstrv[0];
 
 			if (!slbt_archive_member_from_offset(m,symrefs_32[idx].ar_member_offset))
 				return slbt_free_archive_meta_impl(
@@ -787,7 +788,8 @@ int slbt_get_archive_meta(
 		symrefs_64 = m->armaps.armap_symrefs_64;
 
 		for (idx=0; idx<m->armaps.armap_nsyms; idx++) {
-			symrefs_64[idx].ar_name_offset = m->symstrv[idx] - m->symstrv[0];
+			if (m->armaps.armap_common_64.ar_armap_attr & AR_ARMAP_ATTR_SYSV)
+				symrefs_64[idx].ar_name_offset = m->symstrv[idx] - m->symstrv[0];
 
 			if (!slbt_archive_member_from_offset(m,symrefs_64[idx].ar_member_offset))
 				return slbt_free_archive_meta_impl(
diff --git a/src/arbits/slbt_armap_bsd_32.c b/src/arbits/slbt_armap_bsd_32.c
index ed236b6..f41c500 100644
--- a/src/arbits/slbt_armap_bsd_32.c
+++ b/src/arbits/slbt_armap_bsd_32.c
@@ -118,13 +118,22 @@ int slbt_ar_parse_primary_armap_bsd_32(
 	mark    = armap->ar_first_name_offset;
 	symrefs = m->armaps.armap_symrefs_32;
 
-	for (idx=0,uch=*++mark; idx<nsyms; idx++,uch=*++mark) {
+	for (idx=0; idx<nsyms; idx++) {
+		uch  = *mark++;
+
+		uref = (attr == AR_ARMAP_ATTR_BE_32)
+			? (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3]
+			: (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0];
+
+		symrefs[idx].ar_name_offset = uref;
+
+		uch  = *mark++;
+
 		uref = (attr == AR_ARMAP_ATTR_BE_32)
 			? (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3]
 			: (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0];
 
 		symrefs[idx].ar_member_offset = uref;
-		mark++;
 	}
 
 	armap->ar_string_table = m->symstrv;
diff --git a/src/arbits/slbt_armap_bsd_64.c b/src/arbits/slbt_armap_bsd_64.c
index b7b7d6d..6dcc737 100644
--- a/src/arbits/slbt_armap_bsd_64.c
+++ b/src/arbits/slbt_armap_bsd_64.c
@@ -127,7 +127,21 @@ int slbt_ar_parse_primary_armap_bsd_64(
 	mark    = armap->ar_first_name_offset;
 	symrefs = m->armaps.armap_symrefs_64;
 
-	for (idx=0,uch=*++mark; idx<nsyms; idx++,uch=*++mark) {
+	for (idx=0; idx<nsyms; idx++) {
+		uch  = *mark++;
+
+		if (attr == AR_ARMAP_ATTR_BE_64) {
+			uref_hi = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
+			uref_lo = (uch[4] << 24) + (uch[5] << 16) + (uch[6] << 8) + uch[7];
+		} else {
+			uref_lo = (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0];
+			uref_hi = (uch[7] << 24) + (uch[6] << 16) + (uch[5] << 8) + uch[4];
+		}
+
+		symrefs[idx].ar_name_offset = (uref_hi << 32) + uref_lo;
+
+		uch  = *mark++;
+
 		if (attr == AR_ARMAP_ATTR_BE_64) {
 			uref_hi = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
 			uref_lo = (uch[4] << 24) + (uch[5] << 16) + (uch[6] << 8) + uch[7];
@@ -137,7 +151,6 @@ int slbt_ar_parse_primary_armap_bsd_64(
 		}
 
 		symrefs[idx].ar_member_offset = (uref_hi << 32) + uref_lo;
-		mark++;
 	}
 
 	armap->ar_string_table = m->symstrv;
-- 
cgit v1.2.3