# Adding new executable formats



## tomaz (Aug 19, 2013)

FreeBSD can run Linux binaries. Say I wanted to write a module for FreeBSD to recognize another kind of binary (either by parsing the file's contents or by reading some extended attribute).

Would this be hard? Could it be done in user space, or would it require a kernel extension? Does the infrastructure to aid this already exist? To learn more about this, where do I start?

Much appreciate any information or pointers to the relevant documentation/source files you can give me.


----------



## TiberiusDuval (Aug 20, 2013)

As far as I know FreeBSD and Linux use the same binary format, ELF. Linux compatibility means that compatibility layer has needed Linux libraries, and it translates Linux kernel calls to those that FreeBSD's kernel understands. That's just simple user's understanding of things, I'm quite sure that in this forum there are quite many more knowledgeable persons.


----------



## wblock@ (Aug 20, 2013)

What executable format do you want to add?  There is more than one approach.  emulators/wine, for example.


----------



## tomaz (Aug 21, 2013)

I'd like the executables to act as normal executables executable with exec* calls, where I'd write a translation layer for system calls. E.g. something like a Mach-O executable.


----------



## SirDice (Aug 21, 2013)

I have no idea how the system acts upon them but I think a good place to start looking is with brandelf(1).


```
# brandelf -l
known ELF types are: FreeBSD(9) Linux(3) Solaris(6) SVR4(0)
```


----------



## expl (Aug 21, 2013)

In regards to Mach-O, as long as the executable is in x86 I don't think its hard to implement a loader as OSX shares most (if not all, not sure) system calls with FreeBSD. You probably can hack up a CLI (without cocoa) program loader in couple of weeks.

There is a Darling project for Linux that you can take a look at.


----------



## kpa (Aug 22, 2013)

The formats supported by brandelf(1) are just what it was configured to support at compile time. The formats recognized by the kernel are configured separately.


----------



## tomaz (Aug 22, 2013)

expl said:
			
		

> You probably can hack up a CLI (without cocoa) program loader in couple of weeks.
> 
> There is a Darling project for Linux that you can take a look at.


Just to clarify, I know how to load and execute the code I'm trying to execute, and how to map the respective system calls. What I do not know is how to make the executable format to act as if it were native, i.e. so that any program calling execve and supplying this executable's filename to execve â€” a program which is not necessarily aware that it is a different executable format â€” will run it seamlessly.

The above project seems to require the use of `<run-darwin> <command-line>` (where <run-darwin> is "dyld", if I understand correctly) to run the different format executable.

The implementation of execve checks if the appropriate permissions are set, then parses the beginning of the file to see if it begins with a shebang. If so, it hands it off to the appropriate interpreter. Otherwise, it loads it into memory and executes it natively. I think it's either the source file of this part of the implementation of execve I'm looking for, or, if that code already has hooks in it to allow the addition of other formats in user space, the API for doing this.


----------

