@@ -15,35 +15,49 @@ Module: Read Mach-O
15
15
#include < util/exception_utils.h>
16
16
#include < util/invariant.h>
17
17
18
+ // we define file-type magic values for all platforms to detect when we find a
19
+ // file that we might not be able to process
20
+ #define CPROVER_FAT_MAGIC 0xcafebabe
21
+ #define CPROVER_FAT_CIGAM 0xbebafeca
22
+ #define CPROVER_MH_MAGIC 0xfeedface
23
+ #define CPROVER_MH_CIGAM 0xcefaedfe
24
+ #define CPROVER_MH_MAGIC_64 0xfeedfacf
25
+ #define CPROVER_MH_CIGAM_64 0xcffaedfe
26
+
18
27
#ifdef __APPLE__
19
28
# include < architecture/byte_order.h>
20
29
# include < mach-o/fat.h>
21
30
# include < mach-o/loader.h>
22
31
# include < mach-o/swap.h>
32
+
33
+ # if (CPROVER_FAT_MAGIC != FAT_MAGIC) || (CPROVER_FAT_CIGAM != FAT_CIGAM) || \
34
+ (CPROVER_MH_MAGIC != MH_MAGIC) || (CPROVER_MH_CIGAM != MH_CIGAM) || \
35
+ (CPROVER_MH_MAGIC_64 != MH_MAGIC_64) || \
36
+ (CPROVER_MH_CIGAM_64 != MH_CIGAM_64)
37
+ # error "Mach-O magic has inconsistent value"
38
+ # endif
23
39
#endif
24
40
25
41
#include < util/run.h>
26
42
27
43
bool is_osx_fat_magic (char hdr[4 ])
28
44
{
29
- #ifdef __APPLE__
30
45
uint32_t *magic=reinterpret_cast <uint32_t *>(hdr);
31
46
32
47
switch (*magic)
33
48
{
34
- case FAT_MAGIC :
35
- case FAT_CIGAM :
36
- return true ;
49
+ case CPROVER_FAT_MAGIC :
50
+ case CPROVER_FAT_CIGAM :
51
+ return true ;
37
52
}
38
- #else
39
- (void )hdr; // unused parameter
40
- #endif
41
53
42
54
return false ;
43
55
}
44
56
45
- osx_fat_readert::osx_fat_readert (std::ifstream &in) :
46
- has_gb_arch(false )
57
+ osx_fat_readert::osx_fat_readert (
58
+ std::ifstream &in,
59
+ message_handlert &message_handler)
60
+ : log(message_handler), has_gb_arch(false )
47
61
{
48
62
#ifdef __APPLE__
49
63
// NOLINTNEXTLINE(readability/identifiers)
@@ -82,6 +96,9 @@ osx_fat_readert::osx_fat_readert(std::ifstream &in) :
82
96
}
83
97
#else
84
98
(void )in; // unused parameter
99
+
100
+ log .warning () << " Cannot read OSX fat archive on this platform"
101
+ << messaget::eom;
85
102
#endif
86
103
}
87
104
@@ -99,20 +116,16 @@ bool osx_fat_readert::extract_gb(
99
116
// guided by https://lowlevelbits.org/parsing-mach-o-files/
100
117
bool is_osx_mach_object (char hdr[4 ])
101
118
{
102
- #ifdef __APPLE__
103
119
uint32_t *magic = reinterpret_cast <uint32_t *>(hdr);
104
120
105
121
switch (*magic)
106
122
{
107
- case MH_MAGIC :
108
- case MH_CIGAM :
109
- case MH_MAGIC_64 :
110
- case MH_CIGAM_64 :
123
+ case CPROVER_MH_MAGIC :
124
+ case CPROVER_MH_CIGAM :
125
+ case CPROVER_MH_MAGIC_64 :
126
+ case CPROVER_MH_CIGAM_64 :
111
127
return true ;
112
128
}
113
- #else
114
- (void )hdr; // unused parameter
115
- #endif
116
129
117
130
return false ;
118
131
}
@@ -236,7 +249,10 @@ void osx_mach_o_readert::process_commands(
236
249
#endif
237
250
}
238
251
239
- osx_mach_o_readert::osx_mach_o_readert (std::istream &_in) : in(_in)
252
+ osx_mach_o_readert::osx_mach_o_readert (
253
+ std::istream &_in,
254
+ message_handlert &message_handler)
255
+ : log(message_handler), in(_in)
240
256
{
241
257
// read magic
242
258
uint32_t magic;
@@ -249,16 +265,16 @@ osx_mach_o_readert::osx_mach_o_readert(std::istream &_in) : in(_in)
249
265
bool is_64 = false , need_swap = false ;
250
266
switch (magic)
251
267
{
252
- case MH_CIGAM :
268
+ case CPROVER_MH_CIGAM :
253
269
need_swap = true ;
254
270
break ;
255
- case MH_MAGIC :
271
+ case CPROVER_MH_MAGIC :
256
272
break ;
257
- case MH_CIGAM_64 :
273
+ case CPROVER_MH_CIGAM_64 :
258
274
need_swap = true ;
259
275
is_64 = true ;
260
276
break ;
261
- case MH_MAGIC_64 :
277
+ case CPROVER_MH_MAGIC_64 :
262
278
is_64 = true ;
263
279
break ;
264
280
default :
@@ -303,5 +319,7 @@ osx_mach_o_readert::osx_mach_o_readert(std::istream &_in) : in(_in)
303
319
}
304
320
305
321
process_commands (ncmds, offset, need_swap);
322
+ #else
323
+ log .warning () << " Cannot read OSX Mach-O on this platform" << messaget::eom;
306
324
#endif
307
325
}
0 commit comments