llvm.org GIT mirror llvm / d9757b4
Add the start of the support for llvm-objdump’s -private-headers for Mach-O files. This adds the printing of the mach header. Load command printing will be next. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216285 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 6 years ago
5 changed file(s) with 302 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 // RUN: llvm-objdump -p %p/Inputs/hello.obj.macho-x86_64 | FileCheck %s
1
2 CHECK: Mach header
3 CHECK: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
4 CHECK: MH_MAGIC_64 X86_64 ALL 0x00 OBJECT 3 496 SUBSECTIONS_VIA_SYMBOLS
10011001
10021002 }
10031003 }
1004
1005 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
1006 uint32_t cpusubtype, uint32_t filetype,
1007 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
1008 bool verbose) {
1009 outs() << "Mach header\n";
1010 outs() << " magic cputype cpusubtype caps filetype ncmds "
1011 "sizeofcmds flags\n";
1012 if (verbose) {
1013 if (magic == MachO::MH_MAGIC)
1014 outs() << " MH_MAGIC";
1015 else if (magic == MachO::MH_MAGIC_64)
1016 outs() << "MH_MAGIC_64";
1017 else
1018 outs() << format(" 0x%08" PRIx32, magic);
1019 switch (cputype) {
1020 case MachO::CPU_TYPE_I386:
1021 outs() << " I386";
1022 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1023 case MachO::CPU_SUBTYPE_I386_ALL:
1024 outs() << " ALL";
1025 break;
1026 default:
1027 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1028 break;
1029 }
1030 break;
1031 case MachO::CPU_TYPE_X86_64:
1032 outs() << " X86_64";
1033 case MachO::CPU_SUBTYPE_X86_64_ALL:
1034 outs() << " ALL";
1035 break;
1036 case MachO::CPU_SUBTYPE_X86_64_H:
1037 outs() << " Haswell";
1038 break;
1039 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1040 default:
1041 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1042 break;
1043 }
1044 break;
1045 case MachO::CPU_TYPE_ARM:
1046 outs() << " ARM";
1047 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1048 case MachO::CPU_SUBTYPE_ARM_ALL:
1049 outs() << " ALL";
1050 break;
1051 case MachO::CPU_SUBTYPE_ARM_V4T:
1052 outs() << " V4T";
1053 break;
1054 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1055 outs() << " V5TEJ";
1056 break;
1057 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1058 outs() << " XSCALE";
1059 break;
1060 case MachO::CPU_SUBTYPE_ARM_V6:
1061 outs() << " V6";
1062 break;
1063 case MachO::CPU_SUBTYPE_ARM_V6M:
1064 outs() << " V6M";
1065 break;
1066 case MachO::CPU_SUBTYPE_ARM_V7:
1067 outs() << " V7";
1068 break;
1069 case MachO::CPU_SUBTYPE_ARM_V7EM:
1070 outs() << " V7EM";
1071 break;
1072 case MachO::CPU_SUBTYPE_ARM_V7K:
1073 outs() << " V7K";
1074 break;
1075 case MachO::CPU_SUBTYPE_ARM_V7M:
1076 outs() << " V7M";
1077 break;
1078 case MachO::CPU_SUBTYPE_ARM_V7S:
1079 outs() << " V7S";
1080 break;
1081 default:
1082 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1083 break;
1084 }
1085 break;
1086 case MachO::CPU_TYPE_ARM64:
1087 outs() << " ARM64";
1088 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1089 case MachO::CPU_SUBTYPE_ARM64_ALL:
1090 outs() << " ALL";
1091 break;
1092 default:
1093 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1094 break;
1095 }
1096 break;
1097 case MachO::CPU_TYPE_POWERPC:
1098 outs() << " PPC";
1099 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1100 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1101 outs() << " ALL";
1102 break;
1103 default:
1104 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1105 break;
1106 }
1107 break;
1108 case MachO::CPU_TYPE_POWERPC64:
1109 outs() << " PPC64";
1110 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1111 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1112 outs() << " ALL";
1113 break;
1114 default:
1115 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1116 break;
1117 }
1118 break;
1119 }
1120 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
1121 outs() << " LIB64 ";
1122 } else {
1123 outs() << format(" 0x%02" PRIx32,
1124 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
1125 }
1126 switch (filetype) {
1127 case MachO::MH_OBJECT:
1128 outs() << " OBJECT";
1129 break;
1130 case MachO::MH_EXECUTE:
1131 outs() << " EXECUTE";
1132 break;
1133 case MachO::MH_FVMLIB:
1134 outs() << " FVMLIB";
1135 break;
1136 case MachO::MH_CORE:
1137 outs() << " CORE";
1138 break;
1139 case MachO::MH_PRELOAD:
1140 outs() << " PRELOAD";
1141 break;
1142 case MachO::MH_DYLIB:
1143 outs() << " DYLIB";
1144 break;
1145 case MachO::MH_DYLIB_STUB:
1146 outs() << " DYLIB_STUB";
1147 break;
1148 case MachO::MH_DYLINKER:
1149 outs() << " DYLINKER";
1150 break;
1151 case MachO::MH_BUNDLE:
1152 outs() << " BUNDLE";
1153 break;
1154 case MachO::MH_DSYM:
1155 outs() << " DSYM";
1156 break;
1157 case MachO::MH_KEXT_BUNDLE:
1158 outs() << " KEXTBUNDLE";
1159 break;
1160 default:
1161 outs() << format(" %10u", filetype);
1162 break;
1163 }
1164 outs() << format(" %5u", ncmds);
1165 outs() << format(" %10u", sizeofcmds);
1166 uint32_t f = flags;
1167 if (f & MachO::MH_NOUNDEFS) {
1168 outs() << " NOUNDEFS";
1169 f &= ~MachO::MH_NOUNDEFS;
1170 }
1171 if (f & MachO::MH_INCRLINK) {
1172 outs() << " INCRLINK";
1173 f &= ~MachO::MH_INCRLINK;
1174 }
1175 if (f & MachO::MH_DYLDLINK) {
1176 outs() << " DYLDLINK";
1177 f &= ~MachO::MH_DYLDLINK;
1178 }
1179 if (f & MachO::MH_BINDATLOAD) {
1180 outs() << " BINDATLOAD";
1181 f &= ~MachO::MH_BINDATLOAD;
1182 }
1183 if (f & MachO::MH_PREBOUND) {
1184 outs() << " PREBOUND";
1185 f &= ~MachO::MH_PREBOUND;
1186 }
1187 if (f & MachO::MH_SPLIT_SEGS) {
1188 outs() << " SPLIT_SEGS";
1189 f &= ~MachO::MH_SPLIT_SEGS;
1190 }
1191 if (f & MachO::MH_LAZY_INIT) {
1192 outs() << " LAZY_INIT";
1193 f &= ~MachO::MH_LAZY_INIT;
1194 }
1195 if (f & MachO::MH_TWOLEVEL) {
1196 outs() << " TWOLEVEL";
1197 f &= ~MachO::MH_TWOLEVEL;
1198 }
1199 if (f & MachO::MH_FORCE_FLAT) {
1200 outs() << " FORCE_FLAT";
1201 f &= ~MachO::MH_FORCE_FLAT;
1202 }
1203 if (f & MachO::MH_NOMULTIDEFS) {
1204 outs() << " NOMULTIDEFS";
1205 f &= ~MachO::MH_NOMULTIDEFS;
1206 }
1207 if (f & MachO::MH_NOFIXPREBINDING) {
1208 outs() << " NOFIXPREBINDING";
1209 f &= ~MachO::MH_NOFIXPREBINDING;
1210 }
1211 if (f & MachO::MH_PREBINDABLE) {
1212 outs() << " PREBINDABLE";
1213 f &= ~MachO::MH_PREBINDABLE;
1214 }
1215 if (f & MachO::MH_ALLMODSBOUND) {
1216 outs() << " ALLMODSBOUND";
1217 f &= ~MachO::MH_ALLMODSBOUND;
1218 }
1219 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
1220 outs() << " SUBSECTIONS_VIA_SYMBOLS";
1221 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
1222 }
1223 if (f & MachO::MH_CANONICAL) {
1224 outs() << " CANONICAL";
1225 f &= ~MachO::MH_CANONICAL;
1226 }
1227 if (f & MachO::MH_WEAK_DEFINES) {
1228 outs() << " WEAK_DEFINES";
1229 f &= ~MachO::MH_WEAK_DEFINES;
1230 }
1231 if (f & MachO::MH_BINDS_TO_WEAK) {
1232 outs() << " BINDS_TO_WEAK";
1233 f &= ~MachO::MH_BINDS_TO_WEAK;
1234 }
1235 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
1236 outs() << " ALLOW_STACK_EXECUTION";
1237 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
1238 }
1239 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
1240 outs() << " DEAD_STRIPPABLE_DYLIB";
1241 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
1242 }
1243 if (f & MachO::MH_PIE) {
1244 outs() << " PIE";
1245 f &= ~MachO::MH_PIE;
1246 }
1247 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
1248 outs() << " NO_REEXPORTED_DYLIBS";
1249 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
1250 }
1251 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
1252 outs() << " MH_HAS_TLV_DESCRIPTORS";
1253 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
1254 }
1255 if (f & MachO::MH_NO_HEAP_EXECUTION) {
1256 outs() << " MH_NO_HEAP_EXECUTION";
1257 f &= ~MachO::MH_NO_HEAP_EXECUTION;
1258 }
1259 if (f & MachO::MH_APP_EXTENSION_SAFE) {
1260 outs() << " APP_EXTENSION_SAFE";
1261 f &= ~MachO::MH_APP_EXTENSION_SAFE;
1262 }
1263 if (f != 0 || flags == 0)
1264 outs() << format(" 0x%08" PRIx32, f);
1265 } else {
1266 outs() << format(" 0x%08" PRIx32, magic);
1267 outs() << format(" %7d", cputype);
1268 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1269 outs() << format(" 0x%02" PRIx32,
1270 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
1271 outs() << format(" %10u", filetype);
1272 outs() << format(" %5u", ncmds);
1273 outs() << format(" %10u", sizeofcmds);
1274 outs() << format(" 0x%08" PRIx32, flags);
1275 }
1276 outs() << "\n";
1277 }
1278
1279 static void getAndPrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
1280 if (Obj->is64Bit()) {
1281 MachO::mach_header_64 H_64;
1282 H_64 = Obj->getHeader64();
1283 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
1284 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
1285 } else {
1286 MachO::mach_header H;
1287 H = Obj->getHeader();
1288 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
1289 H.sizeofcmds, H.flags, verbose);
1290 }
1291 }
1292
1293 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
1294 const MachOObjectFile *file = dyn_cast(Obj);
1295 getAndPrintMachHeader(file, true);
1296 // TODO: next get and print the load commands.
1297 }
833833 printELFFileHeader(o);
834834 } else if (o->isCOFF()) {
835835 printCOFFFileHeader(o);
836 } else if (o->isMachO()) {
837 printMachOFileHeader(o);
836838 }
837839 }
838840
3737
3838 void printELFFileHeader(const object::ObjectFile *o);
3939 void printCOFFFileHeader(const object::ObjectFile *o);
40 void printMachOFileHeader(const object::ObjectFile *o);
4041
4142 } // end namespace llvm
4243