+bool MachORep::needsExecSeg(const MachO& macho) {
+ uint32_t platform = macho.platform();
+ // Everything embedded gets an exec segment.
+ return platform != 0 && platform != PLATFORM_MACOS;
+}
+
+size_t MachORep::execSegBase(const Architecture *arch)
+{
+ auto_ptr<MachO> macho(arch ? mExecutable->architecture(*arch) : mExecutable->architecture());
+
+ if (!needsExecSeg(*macho)) {
+ return 0;
+ }
+
+ segment_command const * const text_cmd = macho->findSegment("__TEXT");
+
+ if (text_cmd == NULL) {
+ return 0;
+ }
+
+ size_t off = 0;
+
+ if (macho->is64()) {
+ off = int_cast<uint64_t,size_t>(reinterpret_cast<segment_command_64 const * const>(text_cmd)->fileoff);
+ } else {
+ off = text_cmd->fileoff;
+ }
+
+ return off;
+}
+
+size_t MachORep::execSegLimit(const Architecture *arch)
+{
+ auto_ptr<MachO> macho(arch ? mExecutable->architecture(*arch) : mExecutable->architecture());
+
+ if (!needsExecSeg(*macho)) {
+ return 0;
+ }
+
+ segment_command const * const text_cmd = macho->findSegment("__TEXT");
+
+ if (text_cmd == NULL) {
+ return 0;
+ }
+
+ size_t size = 0;
+
+ if (macho->is64()) {
+ size = int_cast<uint64_t,size_t>(reinterpret_cast<segment_command_64 const * const>(text_cmd)->filesize);
+ } else {
+ size = text_cmd->filesize;
+ }
+
+ return size;
+}
+