···11/*
22- * Copyright (c) 1999-2008 Apple Inc. All Rights Reserved.
22+ * Copyright (c) 1999-2010 Apple Inc. All Rights Reserved.
33 *
44 * @APPLE_LICENSE_HEADER_START@
55 *
···174174 in the task will be given stack
175175 execution privilege. Only used in
176176 MH_EXECUTE filetypes. */
177177-#define MH_DEAD_STRIPPABLE_DYLIB 0x400000 /* Only for use on dylibs. When
178178- linking against a dylib that
179179- has this bit set, the static linker
180180- will automatically not create a
181181- LC_LOAD_DYLIB load command to the
182182- dylib if no symbols are being
183183- referenced from the dylib. */
184177#define MH_ROOT_SAFE 0x40000 /* When this bit is set, the binary
185178 declares it is safe for use in
186179 processes with uid zero */
···197190 load the main executable at a
198191 random address. Only used in
199192 MH_EXECUTE filetypes. */
193193+#define MH_DEAD_STRIPPABLE_DYLIB 0x400000 /* Only for use on dylibs. When
194194+ linking against a dylib that
195195+ has this bit set, the static linker
196196+ will automatically not create a
197197+ LC_LOAD_DYLIB load command to the
198198+ dylib if no symbols are being
199199+ referenced from the dylib. */
200200+#define MH_HAS_TLV_DESCRIPTORS 0x800000 /* Contains a section of type
201201+ S_THREAD_LOCAL_VARIABLES */
202202+203203+#define MH_NO_HEAP_EXECUTION 0x1000000 /* When this bit is set, the OS will
204204+ run the main executable with
205205+ a non-executable heap even on
206206+ platforms (e.g. i386) that don't
207207+ require it. Only used in MH_EXECUTE
208208+ filetypes. */
200209201210/*
202211 * The load commands directly follow the mach_header. The total size of all
···275284#define LC_ENCRYPTION_INFO 0x21 /* encrypted segment information */
276285#define LC_DYLD_INFO 0x22 /* compressed dyld information */
277286#define LC_DYLD_INFO_ONLY (0x22|LC_REQ_DYLD) /* compressed dyld information only */
287287+#define LC_LOAD_UPWARD_DYLIB (0x23 | LC_REQ_DYLD) /* load upward dylib */
288288+#define LC_VERSION_MIN_MACOSX 0x24 /* build for MacOSX min OS version */
289289+#define LC_VERSION_MIN_IPHONEOS 0x25 /* build for iPhoneOS min OS version */
290290+#define LC_FUNCTION_STARTS 0x26 /* compressed table of function start addresses */
291291+#define LC_DYLD_ENVIRONMENT 0x27 /* string for dyld to treat
292292+ like environment variable */
293293+#define LC_MAIN (0x28|LC_REQ_DYLD) /* replacement for LC_UNIXTHREAD */
294294+#define LC_DATA_IN_CODE 0x29 /* table of non-instructions in __text */
295295+#define LC_SOURCE_VERSION 0x2A /* source version used to build binary */
296296+#define LC_DYLIB_CODE_SIGN_DRS 0x2B /* Code signing DRs copied from linked dylibs */
297297+278298279299/*
280300 * A variable length string in a load command is represented by an lc_str
···465485 symbol pointers to lazy
466486 loaded dylibs */
467487/*
488488+ * Section types to support thread local variables
489489+ */
490490+#define S_THREAD_LOCAL_REGULAR 0x11 /* template of initial
491491+ values for TLVs */
492492+#define S_THREAD_LOCAL_ZEROFILL 0x12 /* template of initial
493493+ values for TLVs */
494494+#define S_THREAD_LOCAL_VARIABLES 0x13 /* TLV descriptors */
495495+#define S_THREAD_LOCAL_VARIABLE_POINTERS 0x14 /* pointers to TLV
496496+ descriptors */
497497+#define S_THREAD_LOCAL_INIT_FUNCTION_POINTERS 0x15 /* functions to call
498498+ to initialize TLV
499499+ values */
500500+501501+/*
468502 * Constants for the section attributes part of the flags field of a section
469503 * structure.
470504 */
···710744 * the name of the dynamic linker (LC_LOAD_DYLINKER). And a dynamic linker
711745 * contains a dylinker_command to identify the dynamic linker (LC_ID_DYLINKER).
712746 * A file can have at most one of these.
747747+ * This struct is also used for the LC_DYLD_ENVIRONMENT load command and
748748+ * contains string for dyld to treat like environment variable.
713749 */
714750struct dylinker_command {
715715- uint32_t cmd; /* LC_ID_DYLINKER or LC_LOAD_DYLINKER */
751751+ uint32_t cmd; /* LC_ID_DYLINKER, LC_LOAD_DYLINKER or
752752+ LC_DYLD_ENVIRONMENT */
716753 uint32_t cmdsize; /* includes pathname string */
717754 union lc_str name; /* dynamic linker's path name */
718755};
···11161153 * of data in the __LINKEDIT segment.
11171154 */
11181155struct linkedit_data_command {
11191119- uint32_t cmd; /* LC_CODE_SIGNATURE or LC_SEGMENT_SPLIT_INFO */
11561156+ uint32_t cmd; /* LC_CODE_SIGNATURE, LC_SEGMENT_SPLIT_INFO,
11571157+ LC_FUNCTION_STARTS, LC_DATA_IN_CODE,
11581158+ or LC_DYLIB_CODE_SIGN_DRS */
11201159 uint32_t cmdsize; /* sizeof(struct linkedit_data_command) */
11211160 uint32_t dataoff; /* file offset of data in __LINKEDIT segment */
11221161 uint32_t datasize; /* file size of data in __LINKEDIT segment */
···11361175};
1137117611381177/*
11781178+ * The version_min_command contains the min OS version on which this
11791179+ * binary was built to run.
11801180+ */
11811181+struct version_min_command {
11821182+ uint32_t cmd; /* LC_VERSION_MIN_MACOSX or
11831183+ LC_VERSION_MIN_IPHONEOS */
11841184+ uint32_t cmdsize; /* sizeof(struct min_version_command) */
11851185+ uint32_t version; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
11861186+ uint32_t sdk; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
11871187+};
11881188+11891189+/*
11391190 * The dyld_info_command contains the file offsets and sizes of
11401191 * the new compressed form of the information dyld needs to
11411192 * load the image. This information is used by dyld on Mac OS X
···11641215 /*
11651216 * Dyld binds an image during the loading process, if the image
11661217 * requires any pointers to be initialized to symbols in other images.
11671167- * The rebase information is a stream of byte sized
12181218+ * The bind information is a stream of byte sized
11681219 * opcodes whose symbolic names start with BIND_OPCODE_.
11691220 * Conceptually the bind information is a table of tuples:
11701221 * <seg-index, seg-offset, type, symbol-library-ordinal, symbol-name, addend>
···12171268 * The export area is a stream of nodes. The first node sequentially
12181269 * is the start node for the trie.
12191270 *
12201220- * Nodes for a symbol start with a byte that is the length of
12711271+ * Nodes for a symbol start with a uleb128 that is the length of
12211272 * the exported symbol information for the string so far.
12221222- * If there is no exported symbol, the byte is zero. If there
12231223- * is exported info, it follows the length byte. The exported
12241224- * info normally consists of a flags and offset both encoded
12251225- * in uleb128. The offset is location of the content named
12261226- * by the symbol. It is the offset from the mach_header for
12271227- * the image.
12731273+ * If there is no exported symbol, the node starts with a zero byte.
12741274+ * If there is exported info, it follows the length.
12751275+ *
12761276+ * First is a uleb128 containing flags. Normally, it is followed by
12771277+ * a uleb128 encoded offset which is location of the content named
12781278+ * by the symbol from the mach_header for the image. If the flags
12791279+ * is EXPORT_SYMBOL_FLAGS_REEXPORT, then following the flags is
12801280+ * a uleb128 encoded library ordinal, then a zero terminated
12811281+ * UTF8 string. If the string is zero length, then the symbol
12821282+ * is re-export from the specified dylib with the same name.
12831283+ * If the flags is EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER, then following
12841284+ * the flags is two uleb128s: the stub offset and the resolver offset.
12851285+ * The stub is used by non-lazy pointers. The resolver is used
12861286+ * by lazy pointers and must be called to get the actual address to use.
12281287 *
12291229- * After the initial byte and optional exported symbol information
12301230- * is a byte of how many edges (0-255) that this node has leaving
12311231- * it, followed by each edge.
12321232- * Each edge is a zero terminated cstring of the addition chars
12881288+ * After the optional exported symbol information is a byte of
12891289+ * how many edges (0-255) that this node has leaving it,
12901290+ * followed by each edge.
12911291+ * Each edge is a zero terminated UTF8 of the addition chars
12331292 * in the symbol, followed by a uleb128 offset for the node that
12341293 * edge points to.
12351294 *
···12971356#define EXPORT_SYMBOL_FLAGS_KIND_REGULAR 0x00
12981357#define EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL 0x01
12991358#define EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION 0x04
13001300-#define EXPORT_SYMBOL_FLAGS_INDIRECT_DEFINITION 0x08
13011301-#define EXPORT_SYMBOL_FLAGS_HAS_SPECIALIZATIONS 0x10
13591359+#define EXPORT_SYMBOL_FLAGS_REEXPORT 0x08
13601360+#define EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER 0x10
1302136113031362/*
13041363 * The symseg_command contains the offset and size of the GNU style
···13381397 uint32_t cmdsize; /* includes pathname string */
13391398 union lc_str name; /* files pathname */
13401399 uint32_t header_addr; /* files virtual address */
14001400+};
14011401+14021402+14031403+/*
14041404+ * The entry_point_command is a replacement for thread_command.
14051405+ * It is used for main executables to specify the location (file offset)
14061406+ * of main(). If -stack_size was used at link time, the stacksize
14071407+ * field will contain the stack size need for the main thread.
14081408+ */
14091409+struct entry_point_command {
14101410+ uint32_t cmd; /* LC_MAIN only used in MH_EXECUTE filetypes */
14111411+ uint32_t cmdsize; /* 24 */
14121412+ uint64_t entryoff; /* file (__TEXT) offset of main() */
14131413+ uint64_t stacksize;/* if not zero, initial stack size */
14141414+};
14151415+14161416+14171417+/*
14181418+ * The source_version_command is an optional load command containing
14191419+ * the version of the sources used to build the binary.
14201420+ */
14211421+struct source_version_command {
14221422+ uint32_t cmd; /* LC_SOURCE_VERSION */
14231423+ uint32_t cmdsize; /* 16 */
14241424+ uint64_t version; /* A.B.C.D.E packed as a24.b10.c10.d10.e10 */
14251425+};
14261426+14271427+14281428+/*
14291429+ * The LC_DATA_IN_CODE load commands uses a linkedit_data_command
14301430+ * to point to an array of data_in_code_entry entries. Each entry
14311431+ * describes a range of data in a code section. This load command
14321432+ * is only used in final linked images.
14331433+ */
14341434+struct data_in_code_entry {
14351435+ uint32_t offset; /* from mach_header to start of data range*/
14361436+ uint16_t length; /* number of bytes in data range */
14371437+ uint16_t kind; /* a DICE_KIND_* value */
14381438+};
14391439+#define DICE_KIND_DATA 0x0001 /* L$start$data$... label */
14401440+#define DICE_KIND_JUMP_TABLE8 0x0002 /* L$start$jt8$... label */
14411441+#define DICE_KIND_JUMP_TABLE16 0x0003 /* L$start$jt16$... label */
14421442+#define DICE_KIND_JUMP_TABLE32 0x0004 /* L$start$jt32$... label */
14431443+#define DICE_KIND_ABS_JUMP_TABLE32 0x0005 /* L$start$jta32$... label */
14441444+14451445+14461446+14471447+/*
14481448+ * Sections of type S_THREAD_LOCAL_VARIABLES contain an array
14491449+ * of tlv_descriptor structures.
14501450+ */
14511451+struct tlv_descriptor
14521452+{
14531453+ void* (*thunk)(struct tlv_descriptor*);
14541454+ unsigned long key;
14551455+ unsigned long offset;
13411456};
1342145713431458#endif /* _MACHO_LOADER_H_ */
+30-1
src/libmach-o/MachOImpl.cpp
···246246 exp->flag = uleb128(p);
247247248248 // TODO: flag == 8 (EXPORT_SYMBOL_FLAGS_REEXPORT)
249249- if (exp->flag & 8)
249249+ if (exp->flag & EXPORT_SYMBOL_FLAGS_REEXPORT)
250250 {
251251 LOG << "FIXME: reexports not currently handled\n";
252252 return;
···254254255255 exp->addr = uleb128(p);
256256 LOG << "export: " << name_buf << " flags=" << std::hex << exp->flag << std::dec << " addr=" << (void*)exp->addr << std::endl;
257257+258258+ if (exp->flag & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
259259+ {
260260+ (void)uleb128(p); // TODO: save & use the resolver info for lazy pointers
261261+ LOG << "FIXME: resolver not currently handled\n";
262262+ }
257263258264 m_exports.push_back(exp);
259265···564570 }
565571566572 case LC_LOAD_DYLIB:
573573+ case LC_LOAD_WEAK_DYLIB:
574574+ case LC_LAZY_LOAD_DYLIB:
575575+ case LC_REEXPORT_DYLIB:
576576+ case LC_LOAD_UPWARD_DYLIB:
567577 {
568578 dylib* lib = &reinterpret_cast<dylib_command*>(cmds_ptr)->dylib;
569579 const char* name = (char*)cmds_ptr + lib->name.offset;
···580590 m_rpaths.push_back(rpath);
581591 break;
582592 }
593593+594594+ case LC_SUB_FRAMEWORK:
595595+ case LC_SUB_UMBRELLA:
596596+ case LC_SUB_CLIENT:
597597+ case LC_SUB_LIBRARY:
598598+ case LC_FUNCTION_STARTS:
599599+ case LC_DATA_IN_CODE:
600600+ case LC_CODE_SIGNATURE:
601601+ case LC_SEGMENT_SPLIT_INFO:
602602+ case LC_ID_DYLIB:
603603+ case LC_VERSION_MIN_MACOSX:
604604+ case LC_VERSION_MIN_IPHONEOS:
605605+ case LC_SOURCE_VERSION:
606606+ case LC_DYLIB_CODE_SIGN_DRS:
607607+ break;
608608+609609+ default:
610610+ std::cerr << "Unhandled loader command " << std::hex << (int)cmds_ptr->cmd << std::dec << std::endl;
611611+ break;
583612584613 }
585614
-8
src/libmach-o/MachOImpl.h
···9797 };
9898};
9999100100-#define LC_MAIN (0x28|LC_REQ_DYLD)
101101-struct entry_point_command {
102102- uint32_t cmd; /* LC_MAIN only used in MH_EXECUTE filetypes */
103103- uint32_t cmdsize; /* 24 */
104104- uint64_t entryoff; /* file (__TEXT) offset of main() */
105105- uint64_t stacksize;/* if not zero, initial stack size */
106106-};
107107-108100#endif