1b6cee71dSXin LI 2b6cee71dSXin LI#------------------------------------------------------------------------------ 3*ae316d1dSXin LI# $File: cafebabe,v 1.31 2024/08/30 16:53:22 christos Exp $ 4b6cee71dSXin LI# Cafe Babes unite! 5b6cee71dSXin LI# 6b6cee71dSXin LI# Since Java bytecode and Mach-O universal binaries have the same magic number, 7b6cee71dSXin LI# the test must be performed in the same "magic" sequence to get both right. 8b6cee71dSXin LI# The long at offset 4 in a Mach-O universal binary tells the number of 9b6cee71dSXin LI# architectures; the short at offset 4 in a Java bytecode file is the JVM minor 10b6cee71dSXin LI# version and the short at offset 6 is the JVM major version. Since there are only 11b6cee71dSXin LI# only 18 labeled Mach-O architectures at current, and the first released 12b6cee71dSXin LI# Java class format was version 43.0, we can safely choose any number 13b6cee71dSXin LI# between 18 and 39 to test the number of architectures against 14b6cee71dSXin LI# (and use as a hack). Let's not use 18, because the Mach-O people 15b6cee71dSXin LI# might add another one or two as time goes by... 16b6cee71dSXin LI# 17b6cee71dSXin LI 18b6cee71dSXin LI### MACH-O START ### 1943a5ec4eSXin LI# URL: https://en.wikipedia.org/wiki/Mach-O 20b6cee71dSXin LI 21b6cee71dSXin LI0 name mach-o \b [ 2243a5ec4eSXin LI# for debugging purpose CPU type as hexadecimal 2343a5ec4eSXin LI#>0 ubequad x CPU=%16.16llx 2443a5ec4eSXin LI# display CPU type as string like: i386 x86_64 ... armv7 armv7k ... 25b6cee71dSXin LI>0 use mach-o-cpu \b 2643a5ec4eSXin LI# for debugging purpose print offset to 1st mach_header like: 2743a5ec4eSXin LI# 1000h 4000h seldom 2d000h 88000h 5b000h 10e000 h 2843a5ec4eSXin LI#>8 ubelong x at %#x offset 2940427ccaSGordon Tetlow>(8.L) indirect x \b: 30b6cee71dSXin LI>0 belong x \b] 31b6cee71dSXin LI 3243a5ec4eSXin LI# Reference: https://opensource.apple.com/source/cctools/cctools-949.0.1/ 3343a5ec4eSXin LI# include/mach-o/fat.h 3443a5ec4eSXin LI# include/mach/machine.h 35b6cee71dSXin LI0 belong 0xcafebabe 36b6cee71dSXin LI>4 belong 1 Mach-O universal binary with 1 architecture: 375f0216bdSXin LI!:mime application/x-mach-binary 38b6cee71dSXin LI>>8 use mach-o \b 3943a5ec4eSXin LI# nfat_arch; number of CPU architectures; highest is 18 for CPU_TYPE_POWERPC in 2020 4043a5ec4eSXin LI>4 ubelong >1 4143a5ec4eSXin LI>>4 ubelong <20 Mach-O universal binary with %d architectures: 425f0216bdSXin LI!:mime application/x-mach-binary 43b6cee71dSXin LI>>>8 use mach-o \b 4443a5ec4eSXin LI>>>4 ubelong >1 4543a5ec4eSXin LI>>>>28 use mach-o \b 4643a5ec4eSXin LI>>>4 ubelong >2 4743a5ec4eSXin LI>>>>48 use mach-o \b 4843a5ec4eSXin LI>>>4 ubelong >3 4943a5ec4eSXin LI>>>>68 use mach-o \b 5043a5ec4eSXin LI>>>4 ubelong >4 5143a5ec4eSXin LI>>>>88 use mach-o \b 5243a5ec4eSXin LI>>>4 ubelong >5 5343a5ec4eSXin LI>>>>108 use mach-o \b 54b6cee71dSXin LI### MACH-O END ### 55*ae316d1dSXin LI### JAVA START ### 56*ae316d1dSXin LI# Reference: http://en.wikipedia.org/wiki/Java_class_file 57*ae316d1dSXin LI# Update: Joerg Jenderek 58*ae316d1dSXin LI>4 ubelong >30 compiled Java class data, 59*ae316d1dSXin LI!:mime application/x-java-applet 60*ae316d1dSXin LI#!:mime application/java-byte-code 61*ae316d1dSXin LI!:ext class 62*ae316d1dSXin LI>>6 ubeshort x version %d. 63*ae316d1dSXin LI>>4 ubeshort x \b%d 64*ae316d1dSXin LI# for debugging purpose version as hexadecimal to compare with Mach-O universal binary 65*ae316d1dSXin LI#>>4 ubelong x (%#8.8x) 66*ae316d1dSXin LI# Which is which? 67*ae316d1dSXin LI# https://docs.oracle.com/javase/specs/jvms/se6/html/ClassFile.doc.html 68*ae316d1dSXin LI#>>4 belong 0x002b (Java 0.?) 69*ae316d1dSXin LI#>>4 belong 0x032d (Java 1.0) 70*ae316d1dSXin LI#>>4 belong 0x032d (Java 1.1) 71*ae316d1dSXin LI>>4 belong 0x002e (Java 1.2) 72*ae316d1dSXin LI>>4 belong 0x002f (Java 1.3) 73*ae316d1dSXin LI>>4 belong 0x0030 (Java 1.4) 74*ae316d1dSXin LI>>4 belong 0x0031 (Java 1.5) 75*ae316d1dSXin LI>>4 belong 0x0032 (Java 1.6) 76*ae316d1dSXin LI>>4 belong 0x0033 (Java 1.7) 77*ae316d1dSXin LI>>4 belong 0x0034 (Java 1.8) 78*ae316d1dSXin LI>>4 belong 0x0035 (Java SE 9) 79*ae316d1dSXin LI>>4 belong 0x0036 (Java SE 10) 80*ae316d1dSXin LI>>4 belong 0x0037 (Java SE 11) 81*ae316d1dSXin LI>>4 belong 0x0038 (Java SE 12) 82*ae316d1dSXin LI>>4 belong 0x0039 (Java SE 13) 83*ae316d1dSXin LI>>4 belong 0x003A (Java SE 14) 84*ae316d1dSXin LI>>4 belong 0x003B (Java SE 15) 85*ae316d1dSXin LI>>4 belong 0x003C (Java SE 16) 86*ae316d1dSXin LI>>4 belong 0x003D (Java SE 17) 87*ae316d1dSXin LI>>4 belong 0x003E (Java SE 18) 88*ae316d1dSXin LI>>4 belong 0x003F (Java SE 19) 89*ae316d1dSXin LI>>4 belong 0x0040 (Java SE 20) 90*ae316d1dSXin LI>>4 belong 0x0041 (Java SE 21) 91*ae316d1dSXin LI>>4 belong 0x0042 (Java SE 22) 92*ae316d1dSXin LI>>4 belong 0x0043 (Java SE 23) 93*ae316d1dSXin LI>>4 belong 0x0044 (Java SE 24) 94*ae316d1dSXin LI>>4 belong 0x0045 (Java SE 25) 95*ae316d1dSXin LI>>4 belong 0x0046 (Java SE 26) 96*ae316d1dSXin LI# pool count unequal zero 97*ae316d1dSXin LI#>>8 beshort x \b, pool count %#x 98*ae316d1dSXin LI# pool table 99*ae316d1dSXin LI#>>10 ubequad x \b, pool %#16.16llx... 100*ae316d1dSXin LI 101*ae316d1dSXin LI0 belong 0xcafed00d JAR compressed with pack200, 102*ae316d1dSXin LI>5 byte x version %d. 103*ae316d1dSXin LI>4 byte x \b%d 104*ae316d1dSXin LI!:mime application/x-java-pack200 105*ae316d1dSXin LI 106*ae316d1dSXin LI### JAVA END ### 107