[Nasm-devel] [PATCH 2/2] outmacho: Emit a dummy __asm section for the Apple tool compatibility.

Byoungchan Lee byoungchan.lee at gmx.com
Sat Jul 17 18:11:01 PDT 2021


From: Byoungchan Lee <daniel.l at hpcnt.com>

Currently, some Apple platforms like watchOS/tvOS require bitcode enabled.
nasm need to emit the __LLVM segment, __asm section to tell the Apple
toolchain that this object is from assembler and has no bitcode.
This trick is used in Kotlin/Native, Rust, Flutter, Golang and yasm.

Signed-off-by: Byoungchan Lee <daniel.l at hpcnt.com>
---
 output/outmacho.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/output/outmacho.c b/output/outmacho.c
index 61f68fcf..e9c24d7c 100644
--- a/output/outmacho.c
+++ b/output/outmacho.c
@@ -754,6 +754,7 @@ static const struct macho_known_section {
     { ".data",          "__DATA",   "__data",           S_REGULAR       },
     { ".rodata",        "__DATA",   "__const",          S_REGULAR       },
     { ".bss",           "__DATA",   "__bss",            S_ZEROFILL      },
+    {".llvmasm",        "__LLVM",   "__asm",            S_REGULAR       },
     { ".debug_abbrev",  "__DWARF",  "__debug_abbrev",   S_ATTR_DEBUG    },
     { ".debug_info",    "__DWARF",  "__debug_info",     S_ATTR_DEBUG    },
     { ".debug_line",    "__DWARF",  "__debug_line",     S_ATTR_DEBUG    },
@@ -1713,9 +1714,20 @@ static void macho_cleanup(void)
     struct section *s;
     struct reloc *r;
     struct symbol *sym;
+    int bits;
 
     dfmt->cleanup();
 
+    /* create a dummy __asm section with a single zero byte.
+     * this is a workaround for making binaries compatible with
+     * bitcode enabled, which is required for watchOS and tvOS */
+    macho_section(".llvmasm", &bits);
+    s = get_section_by_name("__LLVM", "__asm");
+    if (s != NULL) {
+      saa_write8(s->data, 0);
+      s->size += 1;
+    }
+
     /* Sort all symbols.  */
     macho_layout_symbols (&nsyms, &strslen);
 
-- 
2.30.0



More information about the Nasm-devel mailing list