Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
elf: make os specific choices dynamic
Baptiste Daroussin committed 1 year ago
commit fa0f9ff3e52b7e001f0bbcf642f6f3c8d49aa70b
parent 71d8559
5 files changed +58 -37
modified libpkg/pkg_abi.c
@@ -151,17 +151,16 @@ pkg_get_myarch_with_legacy(struct os_info *oi)
        *p = tolower(*p);
    }

-
// TODO: When dealing with DragonFly, not only on DragonFly
-
#ifdef __DragonFly__
-
	size_t dsz;
-

-
	dsz = strlen(oi->abi);
-
	if (strncasecmp(oi->abi, "DragonFly", 9) == 0) {
-
		for (int i = 0; i < dsz; i++)
-
			oi->abi[i] = tolower(oi->abi[i]);
-
		return (0);
+
	if (oi->ostype == OS_DRAGONFLY) {
+
		size_t dsz;
+

+
		dsz = strlen(oi->abi);
+
		if (strncasecmp(oi->abi, "DragonFly", 9) == 0) {
+
			for (int i = 0; i < dsz; i++)
+
				oi->abi[i] = tolower(oi->abi[i]);
+
			return (0);
+
		}
	}
-
#endif

	/* Translate architecture string back to regular OS one */
	char *arch_tweak = strchr(oi->abi, ':');
modified libpkg/pkg_config.c
@@ -886,9 +886,8 @@ load_repo_file(int dfd, const char *repodir, const char *repofile,

	myarch_legacy = pkg_object_string(pkg_config_get("ALTABI"));
	ucl_parser_register_variable (p, "ALTABI", myarch_legacy);
-
#ifdef __FreeBSD__
-
	ucl_parser_register_variable(p, "OSVERSION", oi->str_osversion);
-
#endif
+
	if (oi->ostype == OS_FREEBSD)
+
		ucl_parser_register_variable(p, "OSVERSION", oi->str_osversion);
	if (oi->name != NULL) {
		ucl_parser_register_variable(p, "OSNAME", oi->name);
	}
@@ -1115,9 +1114,8 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)
		strlcpy(oi.abi, envabi, sizeof(oi.abi));
		pkg_arch_to_legacy(oi.abi, oi.altabi, sizeof(oi.abi));
	}
-
#ifdef __FreeBSD__
-
	ctx.osversion = oi.osversion;
-
#endif
+
	if (oi.ostype == OS_FREEBSD)
+
		ctx.osversion = oi.osversion;
	if (parsed != false) {
		pkg_emit_error("pkg_init() must only be called once");
		err = EPKG_FATAL;
@@ -1227,9 +1225,8 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)
	p = ucl_parser_new(0);
	ucl_parser_register_variable (p, "ABI", oi.abi);
	ucl_parser_register_variable (p, "ALTABI", oi.altabi);
-
#ifdef __FreeBSD__
-
	ucl_parser_register_variable(p, "OSVERSION", oi.str_osversion);
-
#endif
+
	if (oi.ostype == OS_FREEBSD)
+
		ucl_parser_register_variable(p, "OSVERSION", oi.str_osversion);
	if (oi.name != NULL) {
		ucl_parser_register_variable(p, "OSNAME", oi.name);
	}
@@ -1417,9 +1414,9 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)

	dbg(1, "pkg initialized");

-
#ifdef __FreeBSD__
-
	ctx.osversion = pkg_object_int(pkg_config_get("OSVERSION"));
-
#endif
+
	if (oi.ostype == OS_FREEBSD)
+
		ctx.osversion = pkg_object_int(pkg_config_get("OSVERSION"));
+

	/* Start the event pipe */
	evpipe = pkg_object_string(pkg_config_get("EVENT_PIPE"));
	if (evpipe != NULL)
modified libpkg/pkg_elf.c
@@ -626,6 +626,7 @@ elf_note_analyse(Elf_Data *data, GElf_Ehdr *elfhdr, struct os_info *oi)
	char *src;
	uint32_t gnu_abi_tag[4];
	char *note_os[6] = {"Linux", "GNU", "Solaris", "FreeBSD", "NetBSD", "Syllable"};
+
	int note_ost[6] = {OS_LINUX, OS_GNU, OS_SOLARIS, OS_FREEBSD, OS_NETBSD, OS_SYLLABLE};
	char *(*pnote_os)[6] = &note_os;
	char invalid_osname[] = "Unknown";
	uint32_t version = 0;
@@ -679,15 +680,26 @@ elf_note_analyse(Elf_Data *data, GElf_Ehdr *elfhdr, struct os_info *oi)
				src += 4;
			}
		}
-
		if (gnu_abi_tag[0] < 6)
+
		if (gnu_abi_tag[0] < 6) {
			oi->name = xstrdup((*pnote_os)[gnu_abi_tag[0]]);
-
		else
+
			oi->ostype = note_ost[gnu_abi_tag[0]];
+
		} else {
			oi->name = xstrdup(invalid_osname);
+
			oi->ostype = OS_UNKNOWN;
+
		}
	} else {
-
		if (note.n_namesz == 0)
+
		if (note.n_namesz == 0) {
			oi->name = xstrdup(invalid_osname);
-
		else
+
			oi->ostype = OS_UNKNOWN;
+
		} else {
			oi->name = xstrdup(src);
+
			if (STREQ(src, "FreeBSD"))
+
				oi->ostype = OS_FREEBSD;
+
			else if (STREQ(src, "DragonFly"))
+
				oi->ostype = OS_DRAGONFLY;
+
			else if (STREQ(src, "NetBSD"))
+
				oi->ostype = OS_NETBSD;
+
		}
		src += roundup2(note.n_namesz, 4);
		if (elfhdr->e_ident[EI_DATA] == ELFDATA2MSB)
			version = be32dec(src);
@@ -704,15 +716,15 @@ elf_note_analyse(Elf_Data *data, GElf_Ehdr *elfhdr, struct os_info *oi)
			oi->osversion = version;
			snprintf(oi->str_osversion, sizeof(oi->str_osversion), "%d", version);
		}
-
#ifdef __DragonFly__
-
		xasprintf(&oi->version, "%d.%d", version / 100000, (((version / 100 % 1000)+1)/2)*2);
-
#endif
-
#ifdef __NetBSD__
-
		xasprintf(&oi->version, "%d", (version + 1000000) / 100000000);
-
#endif
-
		xasprintf(&oi->version_major, "%d", version / 100000);
-
		xasprintf(&oi->version_minor, "%d", (version / 1000 % 100));
-
		xasprintf(&oi->version, "%d", version / 100000);
+
		if (oi->ostype == OS_DRAGONFLY) {
+
			xasprintf(&oi->version, "%d.%d", version / 100000, (((version / 100 % 1000)+1)/2)*2);
+
		} else if (oi->ostype == OS_NETBSD) {
+
			xasprintf(&oi->version, "%d", (version + 1000000) / 100000000);
+
		} else {
+
			xasprintf(&oi->version_major, "%d", version / 100000);
+
			xasprintf(&oi->version_minor, "%d", (version / 1000 % 100));
+
			xasprintf(&oi->version, "%d", version / 100000);
+
		}
	}

	return (true);
modified libpkg/private/pkg.h
@@ -106,6 +106,18 @@ typedef enum {
	IPV6,
} ip_version_t;

+
typedef enum {
+
	OS_UNKNOWN = 0,
+
	OS_DRAGONFLY,
+
	OS_FREEBSD,
+
	OS_GNU,
+
	OS_LINUX,
+
	OS_MACOS,
+
	OS_NETBSD,
+
	OS_SYLLABLE,
+
	OS_SOLARIS,
+
} os_type_t;
+

struct pkg_kvlist {
	kvlist_t *list;
};
@@ -614,6 +626,7 @@ typedef enum {

struct os_info {
	int osversion;
+
	int ostype;
	char *name;
	char *version;
	char *version_major;
modified tests/frontend/abi.sh
@@ -44,12 +44,12 @@ elfparse_body() {
		-o inline:"${_expected}" \
		pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/fbsd.bin config altabi

-
	_expected="DragonFly:5:amd64\n"
+
	_expected="dragonfly:5.10:x86:64\n"
	atf_check \
		-o inline:"${_expected}" \
		pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/dfly.bin config abi

-
	_expected="dragonfly:5:x86:64\n"
+
	_expected="dragonfly:5.10:x86:64\n"
	atf_check \
		-o inline:"${_expected}" \
		pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/dfly.bin config altabi