Skip to content

Commit

Permalink
Add hostarch.h, show arch and size in info
Browse files Browse the repository at this point in the history
  • Loading branch information
Moe-hacker committed Nov 23, 2024
1 parent 9454068 commit b1970b9
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 186 deletions.
204 changes: 18 additions & 186 deletions src/elf-magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,200 +37,23 @@ struct RURI_ELF_MAGIC *ruri_get_magic(const char *_Nonnull cross_arch)
* TODO: Support more architecture aliases.
*/
struct RURI_ELF_MAGIC *ret = (struct RURI_ELF_MAGIC *)malloc(sizeof(struct RURI_ELF_MAGIC));
// Avoid to simulate the same architecture as host.
#if defined(__aarch64__)
if (strcmp(cross_arch, "aarch64") == 0 || strcmp(cross_arch, "arm64") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__alpha__)
if (strcmp(cross_arch, "alpha") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__arm__)
if (strcmp(cross_arch, "arm") == 0 || strcmp(cross_arch, "arm32") == 0 || strcmp(cross_arch, "armhf") == 0 || strcmp(cross_arch, "armel") == 0 || strcmp(cross_arch, "armv7") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__armeb__)
if (strcmp(cross_arch, "armeb") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__cris__)
if (strcmp(cross_arch, "cris") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__hexagon__)
if (strcmp(cross_arch, "hexagon") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__hppa__)
if (strcmp(cross_arch, "hppa") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__i386__)
if (strcmp(cross_arch, "i386") == 0 || strcmp(cross_arch, "x86") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__loongarch64__)
if (strcmp(cross_arch, "loongarch64") == 0 || strcmp(cross_arch, "loong64") == 0 || strcmp(cross_arch, "loongarch") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__m68k__)
if (strcmp(cross_arch, "m68k") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__microblaze__)
if (strcmp(cross_arch, "microblaze") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__mips__)
if (strcmp(cross_arch, "mips") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__mips64__)
if (strcmp(cross_arch, "mips64") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__mips64el__)
if (strcmp(cross_arch, "mips64el") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__mipsel__)
if (strcmp(cross_arch, "mipsel") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__mipsn32__)
if (strcmp(cross_arch, "mipsn32") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__mipsn32el__)
if (strcmp(cross_arch, "mipsn32el") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__ppc__)
if (strcmp(cross_arch, "ppc") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__ppc64__)
if (strcmp(cross_arch, "ppc64") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__ppc64le__)
if (strcmp(cross_arch, "ppc64le") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__riscv32__)
if (strcmp(cross_arch, "riscv32") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__riscv64__)
if (strcmp(cross_arch, "riscv64") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__s390x__)
if (strcmp(cross_arch, "s390x") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__sh4__)
if (strcmp(cross_arch, "sh4") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__sh4eb__)
if (strcmp(cross_arch, "sh4eb") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__sparc__)
if (strcmp(cross_arch, "sparc") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__sparc32plus__)
if (strcmp(cross_arch, "sparc32plus") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__sparc64__)
if (strcmp(cross_arch, "sparc64") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__x86_64__)
if (strcmp(cross_arch, "x86_64") == 0 || strcmp(cross_arch, "amd64") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__xtensa__)
if (strcmp(cross_arch, "xtensa") == 0) {
free(ret);
return NULL;
}
#endif
#if defined(__xtensaeb__)
if (strcmp(cross_arch, "xtensaeb") == 0) {
free(ret);
return NULL;
// Avoid to simulate the same architecture as host.
if (strcmp(cross_arch, RURI_HOST_ARCH) == 0) {
ruri_error("Do not simulate the same architecture as host.");
}
#endif
if (strcmp(cross_arch, "aarch64") == 0 || strcmp(cross_arch, "arm64") == 0 || strcmp(cross_arch, "armv8") == 0) {
if (strcmp(RURI_HOST_ARCH, "aarch64") == 0) {
ruri_error("Do not simulate the same architecture as host.");
}
ret->magic = ruri_magicof(aarch64);
ret->mask = ruri_maskof(aarch64);
} else if (strcmp(cross_arch, "alpha") == 0) {
ret->magic = ruri_magicof(alpha);
ret->mask = ruri_maskof(alpha);
} else if (strcmp(cross_arch, "arm") == 0 || strcmp(cross_arch, "armhf") == 0 || strcmp(cross_arch, "arm32") == 0 || strcmp(cross_arch, "armel") == 0 || strcmp(cross_arch, "armv7") == 0) {
if (strcmp(RURI_HOST_ARCH, "arm") == 0) {
ruri_error("Do not simulate the same architecture as host.");
}
ret->magic = ruri_magicof(arm);
ret->mask = ruri_maskof(arm);
} else if (strcmp(cross_arch, "armeb") == 0) {
Expand All @@ -246,9 +69,15 @@ struct RURI_ELF_MAGIC *ruri_get_magic(const char *_Nonnull cross_arch)
ret->magic = ruri_magicof(hppa);
ret->mask = ruri_maskof(hppa);
} else if (strcmp(cross_arch, "i386") == 0 || strcmp(cross_arch, "x86") == 0) {
if (strcmp(RURI_HOST_ARCH, "i386") == 0) {
ruri_error("Do not simulate the same architecture as host.");
}
ret->magic = ruri_magicof(i386);
ret->mask = ruri_maskof(i386);
} else if (strcmp(cross_arch, "loongarch64") == 0 || strcmp(cross_arch, "loong64") == 0 || strcmp(cross_arch, "loongarch") == 0) {
if (strcmp(RURI_HOST_ARCH, "loongarch64") == 0) {
ruri_error("Do not simulate the same architecture as host.");
}
ret->magic = ruri_magicof(loongarch64);
ret->mask = ruri_maskof(loongarch64);
} else if (strcmp(cross_arch, "m68k") == 0) {
Expand Down Expand Up @@ -309,6 +138,9 @@ struct RURI_ELF_MAGIC *ruri_get_magic(const char *_Nonnull cross_arch)
ret->magic = ruri_magicof(sparc64);
ret->mask = ruri_maskof(sparc64);
} else if (strcmp(cross_arch, "x86_64") == 0 || strcmp(cross_arch, "amd64") == 0) {
if (strcmp(RURI_HOST_ARCH, "x86_64") == 0) {
ruri_error("Do not simulate the same architecture as host.");
}
ret->magic = ruri_magicof(x86_64);
ret->mask = ruri_maskof(x86_64);
} else if (strcmp(cross_arch, "xtensa") == 0) {
Expand Down
99 changes: 99 additions & 0 deletions src/include/hostarch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* This header contains no copyrightable information.
*/
#if defined(__aarch64__)
#define RURI_HOST_ARCH "aarch64"
#endif
#if defined(__alpha__)
#define RURI_HOST_ARCH "alpha"
#endif
#if defined(__arm__)
#define RURI_HOST_ARCH "arm"
#endif
#if defined(__armeb__)
#define RURI_HOST_ARCH "armeb"
#endif
#if defined(__cris__)
#define RURI_HOST_ARCH "cris"
#endif
#if defined(__hexagon__)
#define RURI_HOST_ARCH "hexagon"
#endif
#if defined(__hppa__)
#define RURI_HOST_ARCH "hppa"
#endif
#if defined(__i386__)
#define RURI_HOST_ARCH "i386"
#endif
#if defined(__loongarch64__)
#define RURI_HOST_ARCH "loongarch64"
#endif
#if defined(__m68k__)
#define RURI_HOST_ARCH "m68k"
#endif
#if defined(__microblaze__)
#define RURI_HOST_ARCH "microblaze"
#endif
#if defined(__mips__)
#define RURI_HOST_ARCH "mips"
#endif
#if defined(__mips64__)
#define RURI_HOST_ARCH "mips64"
#endif
#if defined(__mips64el__)
#define RURI_HOST_ARCH "mips64el"
#endif
#if defined(__mipsel__)
#define RURI_HOST_ARCH "mipsel"
#endif
#if defined(__mipsn32__)
#define RURI_HOST_ARCH "mipsn32"
#endif
#if defined(__mipsn32el__)
#define RURI_HOST_ARCH "mipsn32el"
#endif
#if defined(__ppc__)
#define RURI_HOST_ARCH "ppc"
#endif
#if defined(__ppc64__)
#define RURI_HOST_ARCH "ppc64"
#endif
#if defined(__ppc64le__)
#define RURI_HOST_ARCH "ppc64le"
#endif
#if defined(__riscv32__)
#define RURI_HOST_ARCH "riscv32"
#endif
#if defined(__riscv64__)
#define RURI_HOST_ARCH "riscv64"
#endif
#if defined(__s390x__)
#define RURI_HOST_ARCH "s390x"
#endif
#if defined(__sh4__)
#define RURI_HOST_ARCH "sh4"
#endif
#if defined(__sh4eb__)
#define RURI_HOST_ARCH "sh4eb"
#endif
#if defined(__sparc__)
#define RURI_HOST_ARCH "sparc"
#endif
#if defined(__sparc32plus__)
#define RURI_HOST_ARCH "sparc32plus"
#endif
#if defined(__sparc64__)
#define RURI_HOST_ARCH "sparc64"
#endif
#if defined(__x86_64__)
#define RURI_HOST_ARCH "x86_64"
#endif
#if defined(__xtensa__)
#define RURI_HOST_ARCH "xtensa"
#endif
#if defined(__xtensaeb__)
#define RURI_HOST_ARCH "xtensaeb"
#endif
#if !defined(RURI_HOST_ARCH)
#define RURI_HOST_ARCH "unknown"
#endif
1 change: 1 addition & 0 deletions src/include/ruri.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
#include "version.h"
#include "k2v.h"
#include "cprintf.h"
#include "hostarch.h"
// Info of a container to create.
struct __attribute__((aligned(128))) RURI_CONTAINER {
// Container directory.
Expand Down
5 changes: 5 additions & 0 deletions src/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ void ruri_show_version_info(void)
#if defined(RURI_COMMIT_ID)
cprintf("{base}%s%s%s", "ruri commit id ...: ", RURI_COMMIT_ID, "\n");
#endif
cprintf("{base}%s%s%s", "Architecture .....: ", RURI_HOST_ARCH, "\n");
struct stat st;
if (stat("/proc/self/exe", &st) == 0) {
cprintf("{base}%s%ldK%s", "Binary size ......: ", (st.st_size / 1024), "\n");
}
#if defined(LIBCAP_MAJOR) && defined(LIBCAP_MINOR)
cprintf("{base}%s%d%s%d%s", "libcap ...........: ", LIBCAP_MAJOR, ".", LIBCAP_MINOR, "\n");
#endif
Expand Down

0 comments on commit b1970b9

Please sign in to comment.