1 /* elf.c - Executable Linking Format manipulation functions.
2 *
3 * Copyright 2023 Rob Landley <[email protected]>
4 */
5
6 #include "toys.h"
7
elf_arch_name(int type)8 char *elf_arch_name(int type)
9 {
10 int i;
11
12 // Values from include/linux/elf-em.h (plus arch/*/include/asm/elf.h)
13 // Names are linux/arch/ directory (sometimes before 32/64 bit merges)
14 struct {int val; char *name;} types[] = {{0x9026, "alpha"}, {93, "arc"},
15 {195, "arcv2"}, {40, "arm"}, {183, "arm64"}, {0x18ad, "avr32"},
16 {247, "bpf"}, {106, "blackfin"}, {140, "c6x"}, {23, "cell"}, {76, "cris"},
17 {252, "csky"}, {0x5441, "frv"}, {46, "h8300"}, {164, "hexagon"},
18 {50, "ia64"}, {258, "loongarch"}, {88, "m32r"}, {0x9041, "m32r"},
19 {4, "m68k"}, {174, "metag"}, {189, "microblaze"},
20 {0xbaab, "microblaze-old"}, {8, "mips"}, {10, "mips-old"}, {89, "mn10300"},
21 {0xbeef, "mn10300-old"}, {113, "nios2"}, {92, "openrisc"},
22 {0x8472, "openrisc-old"}, {15, "parisc"}, {20, "ppc"}, {21, "ppc64"},
23 {243, "riscv"}, {22, "s390"}, {0xa390, "s390-old"}, {135, "score"},
24 {42, "sh"}, {2, "sparc"}, {18, "sparc8+"}, {43, "sparc9"}, {188, "tile"},
25 {191, "tilegx"}, {3, "386"}, {6, "486"}, {62, "x86-64"}, {94, "xtensa"},
26 {0xabc7, "xtensa-old"}
27 };
28
29 for (i = 0; i<ARRAY_LEN(types); i++)
30 if (type==types[i].val) return types[i].name;
31
32 sprintf(libbuf, "unknown arch %d", type);
33 return libbuf;
34 }
35
elf_print_flags(int arch,int flags)36 void elf_print_flags(int arch, int flags)
37 {
38 if (arch == 40) { // arm32
39 printf(", EABI%u", (flags >> 24) & 0xf);
40 if (flags & 0x200) printf(", soft float");
41 else if (flags & 0x400) printf(", hard float");
42 } else if (arch == 243) { // riscv
43 if (flags & 1) printf(", C");
44 if (flags & 8) printf(", E");
45 if (flags & 0x10) printf(", TSO");
46 printf(", %s float",
47 (char *[]){"soft", "single", "double", "quad"}[(flags&0x6)/2]);
48 }
49 }
50