xref: /freebsd/contrib/elftoolchain/common/native-elf-format (revision 8c22b9f3ba586e008e8e55a6215a1d46eb6830b9)
12de3b87aSKai Wang#!/bin/sh
22de3b87aSKai Wang#
3d003e0d7SEd Maste# $Id: native-elf-format 3735 2019-04-25 19:44:47Z jkoshy $
42de3b87aSKai Wang#
52de3b87aSKai Wang# Find the native ELF format for a host platform by compiling a
62de3b87aSKai Wang# test object and examining the resulting object.
72de3b87aSKai Wang#
82de3b87aSKai Wang# This script is used if there is no easy way to determine this
92de3b87aSKai Wang# information statically at compile time.
102de3b87aSKai Wang
112de3b87aSKai Wangprogram=`basename $0`
122de3b87aSKai Wangtmp_c=`mktemp -u nefXXXXXX`.c
132de3b87aSKai Wangtmp_o=`echo ${tmp_c} | sed -e 's/.c$/.o/'`
142de3b87aSKai Wang
152de3b87aSKai Wangtrap "rm -f ${tmp_c} ${tmp_o}"	0 1 2 3 15
162de3b87aSKai Wang
172de3b87aSKai Wangtouch ${tmp_c}
182de3b87aSKai Wang
192de3b87aSKai Wangecho "/* Generated by ${program} on `date` */"
202de3b87aSKai Wang
212de3b87aSKai Wangcc -c ${tmp_c} -o ${tmp_o}
2267d97fe7SEd MasteLC_ALL=C readelf -h ${tmp_o} | awk '
232de3b87aSKai Wang$1 ~ "Class:" {
242de3b87aSKai Wang        sub("ELF","",$2); elfclass = $2;
252de3b87aSKai Wang    }
262de3b87aSKai Wang$1 ~ "Data:"  {
272de3b87aSKai Wang        if (match($0, "little")) {
282de3b87aSKai Wang            elfdata = "LSB";
292de3b87aSKai Wang 	} else {
302de3b87aSKai Wang            elfdata = "MSB";
312de3b87aSKai Wang	}
322de3b87aSKai Wang    }
332de3b87aSKai Wang$1 ~ "Machine:" {
342de3b87aSKai Wang        if (match($0, "Intel.*386")) {
352de3b87aSKai Wang            elfarch = "EM_386";
36839529caSEd Maste        } else if (match($0, "MIPS")) {
37839529caSEd Maste            elfarch = "EM_MIPS";
38ae500c1fSEd Maste        } else if (match($0, ".*[xX]86[-_]64")) {
392de3b87aSKai Wang            elfarch = "EM_X86_64";
40d003e0d7SEd Maste        } else if (match($0, "PowerPC64")) {
41d003e0d7SEd Maste            elfarch = "EM_PPC64";
42*8c22b9f3SKyle Evans        } else if (match($0, "AArch64")) {
43*8c22b9f3SKyle Evans            elfarch = "EM_AARCH64";
442de3b87aSKai Wang        } else {
452de3b87aSKai Wang            elfarch = "unknown";
462de3b87aSKai Wang        }
472de3b87aSKai Wang    }
482de3b87aSKai WangEND {
492de3b87aSKai Wang    printf("#define	ELFTC_CLASS	ELFCLASS%s\n", elfclass);
502de3b87aSKai Wang    printf("#define	ELFTC_ARCH	%s\n", elfarch);
512de3b87aSKai Wang    printf("#define	ELFTC_BYTEORDER	ELFDATA2%s\n", elfdata);
522de3b87aSKai Wang}'
532de3b87aSKai Wang
54