Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:47:58+00:00 2026-06-15T14:47:58+00:00

I’m trying to compile a binary file into a MACH_O object file so that

  • 0

I’m trying to compile a binary file into a MACH_O object file so that it can be linked it into a dylib. The dylib is written in c/c++.

On linux the following command is used:
ld -r -b binary -o foo.o foo.bin

I have tried various option on OSX but to no avail:

ld -r foo.bin -o foo.o
gives:
ld: warning: -arch not specified
ld: warning: ignoring file foo.bin, file was built for unsupported file format which is not the architecture being linked (x86_64)

An empty .o file is created

ld -arch x86_64 -r foo.bin -o foo.o 
ld: warning: ignoring file foo.bin, file was built for unsupported file format which is not the architecture being linked (x86_64)

Again and empty .o file is created. Checking the files with nm gives:
nm foo.o
nm: no name list

The binary file is actually, firmware that will be downloaded to an external device.

Thanks for looking

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:47:59+00:00Added an answer on June 15, 2026 at 2:47 pm

    Here’s the closest translation to the Linux linker command to perform binary embedding with the OSX linker:

    touch stub.c
    gcc -o stub.o -c stub.c
    ld -r -o foo.o -sectcreate binary foo_bin foo.bin stub.o
    

    foo.bin will be stored in segment binary, section foo_bin (both names are arbitrary but chosen to mimic GNU ld for ELF on Linux) of the foo.o object.

    stub is necessary because ld refuses to create just a custom segment/section. You don’t need it if you link directly with a real code object.

    To get data back from the section, use getsectbyname (struct is defined in mach-o/loader.h):

    #include <mach-o/getsect.h>
    const struct section_64 *sect = getsectbyname("binary", "foo_bin");
    char *buffer = calloc(1, sect->size+1);
    memcpy(buffer, sect->addr, sect->size); // whatever
    

    or getsectdata:

    #include <mach-o/getsect.h>
    size_t size;
    char *data = getsectdata("binary", "foo_bin", &size);
    char *buffer = calloc(1, size+1);
    memcpy(buffer, data, size); // whatever
    

    (I used it to store text data, hence the stringification via calloc zeroing of size+1 plus blob copying)

    Warning: Since 10.7, ASLR got stronger and messes badly with getsect* functions, resulting in segfaults. set disable-aslr off in GDB before running to reproduce EXC_BAD_ACCESS (SIGSEGV) in debug conditions. People had to jump through inordinate hoops to find the real address and get this working again.

    A simple workaround is to get the offset and size, open the binary and read the data straight from disk. Here is a working example:

    // main.c, build with gcc -o main main.c foo.o
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdio.h>
    #include <mach-o/getsect.h>
    
    int main() {
        // finding the filename of the running binary is left as an exercise to the reader
        char *filename = "main";
    
        const struct section_64 *sect = getsectbyname("binary", "foo_bin");
        if (sect == NULL) {
            exit(1);
        }
    
        char *buffer = calloc(1, sect->size+1);
        int fd = open(filename, O_RDONLY);
        if (fd < 0) {
            exit(1);
        }
        lseek(fd, sect->offset, SEEK_SET);
        if (read(fd, buffer, sect->size) != sect->size) {
            close(fd);
            exit(1);
        }
    
        printf("%s", buffer);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.