]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Darwin: add code for text_slide
authorPaul Floyd <pjfloyd@wanadoo.fr>
Sun, 30 Nov 2025 14:51:20 +0000 (15:51 +0100)
committerPaul Floyd <pjfloyd@wanadoo.fr>
Sun, 30 Nov 2025 14:51:20 +0000 (15:51 +0100)
Code from Louis Brunner

coregrind/m_mach/mach_basics.c
coregrind/m_mach/mach_msg.c
coregrind/m_ume/macho.c

index f0f45f5735530baaadbca19777f2b09d2cf42ae1..73b802bdd6b013f1ca6db0f3d2e7bf0462bb6e41 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "pub_core_basics.h"
 #include "pub_core_mach.h"
+#include "pub_core_libcassert.h" // vg_assert
 
 #include <mach/mach.h>
 #include <mach/machine/ndr_def.h>
@@ -40,6 +41,7 @@ extern mach_port_name_t thread_self_trap(void);
 extern mach_port_t mach_reply_port(void);
 
 /* Global variables set in mach_init() */
+int vm_page_shift = 0;
 vm_size_t vm_page_size = 0;
 mach_port_name_t mach_task_self_ = 0;
 
@@ -60,6 +62,10 @@ mach_port_t mig_get_reply_port(void)
     // its own behalf, and doesn't call mig outside the semaphore
 }
 
+void mach_msg_destroy(mach_msg_header_t *msg)
+{
+  // TODO: copy from XNU?
+}
 
 void mig_dealloc_reply_port(mach_port_t reply_port)
 {
@@ -79,7 +85,11 @@ void VG_(mach_init)(void)
     mach_task_self_ = task_self_trap();
 
     // GrP fixme host_page_size(host_self_trap(), &vm_page_size);
-    vm_page_size = 4096;
+    vm_page_shift = 12;
+    // FIXME: stored in COMM_PAGE + 0x025, (1 << 12) = 4096
+    vm_page_size = 0x1000;
+
+  vg_assert(1 << vm_page_shift == vm_page_size);
 }
 
 #endif // defined(VGO_darwin) 
index c21425472ae79b2b9ce4038b4e387788b10a9b70..adf95c1cf4a620f3ba924036f03107b9bbc4ef72 100644 (file)
@@ -39,6 +39,7 @@
 
 #if defined(VGO_darwin) 
 
+#include "config.h" // for DARWIN_VERS
 #include "pub_core_basics.h"
 #include "pub_core_mach.h"
 
@@ -56,16 +57,15 @@ mach_msg_trap(mach_msg_header_t *msg,
               mach_msg_timeout_t timeout,
               mach_port_t notify);
 
-mach_msg_return_t
-mach_msg(msg, option, send_size, rcv_size, rcv_name, timeout, notify)
-    mach_msg_header_t *msg;
-    mach_msg_option_t option;
-    mach_msg_size_t send_size;
-    mach_msg_size_t rcv_size;
-    mach_port_t rcv_name;
-    mach_msg_timeout_t timeout;
-    mach_port_t notify;
-{
+mach_msg_return_t mach_msg(
+  mach_msg_header_t *msg,
+  mach_msg_option_t option,
+  mach_msg_size_t send_size,
+  mach_msg_size_t rcv_size,
+  mach_port_t rcv_name,
+  mach_msg_timeout_t timeout,
+  mach_port_t notify
+) {
     mach_msg_return_t mr;
 
     /*
index 239b7fc0263a2261cc1711235c9c65ad71a2d52d..a8fdfd7088c4f030c1bce8e25e6dcb74d80da95e 100644 (file)
@@ -74,6 +74,7 @@ typedef struct load_info_t {
   vki_uint8_t *linker_entry; // dylinker entry point
   Addr linker_offset; // dylinker text offset
   vki_size_t max_addr; // biggest address reached while loading segments
+  Addr text_slide; // slide of the text segment because of "ASLR" (arm64-only)
 } load_info_t;
 
 static void print(const HChar *str)
@@ -180,7 +181,7 @@ load_segment(int fd, vki_off_t offset, vki_off_t size,
    vki_size_t vmsize;   // page-aligned
    vki_size_t vmend;    // page-aligned
    unsigned int prot;
-   Addr slided_addr = segcmd->vmaddr + out_info->linker_offset;
+   Addr slided_addr = segcmd->vmaddr + out_info->linker_offset + out_info->text_slide;
 
    // GrP fixme mark __UNIXSTACK as SF_STACK
     
@@ -449,6 +450,7 @@ load_dylinker(struct dylinker_command *dycmd, load_info_t *out_info)
    linker_info.entry = NULL;
    linker_info.linker_entry = NULL;
    linker_info.linker_offset = 0;
+   linker_info.text_slide = 0;
    linker_info.max_addr = out_info->max_addr;
 
    if (dycmd->name.offset >= dycmd->cmdsize) {
@@ -816,7 +818,7 @@ Bool VG_(match_macho)(const void *hdr, SizeT len)
 
    // GrP fixme check more carefully for matching fat arch?
 
-   return (len >= VKI_PAGE_SIZE  &&  
+   return (len >= sizeof(*magic)  &&
            (*magic == MAGIC  ||  *magic == VG_(ntohl)(FAT_MAGIC))) 
       ? True : False;
 }
@@ -834,6 +836,7 @@ Int VG_(load_macho)(Int fd, const HChar *name, ExeInfo *info)
    load_info.linker_entry = NULL;
    load_info.linker_offset = 0;
    load_info.max_addr = 0;
+   load_info.text_slide = 0;
 
    err = VG_(fstat)(fd, &sb);
    if (err) {
@@ -856,6 +859,11 @@ Int VG_(load_macho)(Int fd, const HChar *name, ExeInfo *info)
    info->text = (Addr) load_info.text;
    info->dynamic = load_info.linker_entry ? True : False;
 
+   if (!info->dynamic && load_info.text_slide) {
+      print("cannot slide static executables\n");
+      return VKI_ENOEXEC;
+   }
+
    info->executable_path = VG_(strdup)("ume.macho.executable_path", name);
 
    SysRes res = VG_(dup)(fd);