cprover
osx_fat_reader.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Read Mach-O
4 
5 Author:
6 
7 \*******************************************************************/
8 
11 
12 #include "osx_fat_reader.h"
13 
14 #include <cassert>
15 #include <cstdlib>
16 
17 #ifdef __APPLE__
18 #include <mach-o/fat.h>
19 #endif
20 
21 bool is_osx_fat_magic(char hdr[4])
22 {
23 #ifdef __APPLE__
24  uint32_t *magic=reinterpret_cast<uint32_t*>(hdr);
25 
26  switch(*magic)
27  {
28  case FAT_MAGIC:
29  case FAT_CIGAM:
30  return true;
31  }
32 #endif
33 
34  return false;
35 }
36 
38  has_gb_arch(false)
39 {
40 #ifdef __APPLE__
41  // NOLINTNEXTLINE(readability/identifiers)
42  struct fat_header fh;
43  // NOLINTNEXTLINE(readability/identifiers)
44  in.read(reinterpret_cast<char*>(&fh), sizeof(struct fat_header));
45 
46  if(!in)
47  throw "failed to read OSX fat header";
48 
49  if(!is_osx_fat_magic(reinterpret_cast<char*>(&(fh.magic))))
50  throw "OSX fat header malformed (magic)"; // NOLINT(readability/throw)
51 
52  assert(sizeof(fh.nfat_arch)==4);
53  unsigned narch=__builtin_bswap32(fh.nfat_arch);
54 
55  for(unsigned i=0; !has_gb_arch && i<narch; ++i)
56  {
57  // NOLINTNEXTLINE(readability/identifiers)
58  struct fat_arch fa;
59  // NOLINTNEXTLINE(readability/identifiers)
60  in.read(reinterpret_cast<char*>(&fa), sizeof(struct fat_arch));
61 
62  assert(sizeof(fa.cputype)==4 &&
63  sizeof(fa.cpusubtype)==4 &&
64  sizeof(fa.size)==4);
65  int cputype=__builtin_bswap32(fa.cputype);
66  int cpusubtype=__builtin_bswap32(fa.cpusubtype);
67  unsigned size=__builtin_bswap32(fa.size);
68 
69  has_gb_arch=cputype==CPU_TYPE_HPPA &&
70  cpusubtype==CPU_SUBTYPE_HPPA_7100LC &&
71  size > 0;
72  }
73 #endif
74 }
75 
77  const std::string &source,
78  const std::string &dest) const
79 {
80  assert(has_gb_arch);
81 
82  std::string command=
83  "lipo -thin hppa7100LC -output \""+dest+"\" \""+source+"\"";
84  return system(command.c_str())!=0;
85 }
unsigned int __builtin_bswap32(unsigned int)
Read OS X Fat Binaries.
bool extract_gb(const std::string &source, const std::string &dest) const
osx_fat_readert(std::ifstream &in)
bool is_osx_fat_magic(char hdr[4])