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