OSX: High Level Assembly on Modern OSX

Posted on December 18, 2016

High Level Assembly (HLA) by Randall Hyde

If you’ve gotten yourself a copy of “Art of Assembly Language” by Randall Hyde, well, good on you. However, if you’re running on a modern version of OSX, you’ve probably run into a snag right out of the gate! It’s never a good sign when Hello World won’t compile, right?

First things first though, you need to have Xcode installed and accept it’s license (‘sudo xcodebuild -license’) - I don’t think this is mentioned in the book or installation instructions (and I don’t think the linker will exist or work otherwise). Get that taken care of, first.

It should go without saying that you need to have downloaded and installed Randall’s HLA. I won’t go into that here, because the book and his website go over that already.

undefined symbol ‘_main’ - what?

Now, on to the problem. You’ve probably tried to run the helloWorld example and found it didn’t work, right? Something like this?

Undefined symbols for architecture i386:
  "_main", referenced from:
       implicit entry/start for main executable
       ld: symbol(s) not found for architecture i386
       Error returned by ld = 256

I’m not sure what’s gone wrong in the first place (apparently this used to work out of the box?) but here’s how to fix it. You need to specify a bunch of arguments to hla to get the linker to do what it’s supposed to be doing. Namely there’s a system library that needs to be linked in, but also a few other things to quiet some silly warnings.

  • Before: /usr/hla/hla helloWorld.hla
  • After: /usr/hla/hla -main:_main -l”macosx_version_min 10.10” -l”lSystem” -l”no_pie” helloWorld.hla

This does a few things:

  • Tells hla to use ‘_main’ as the entry point
  • Provides a few arguments to the linker:
    • Specify minimum supported OSX version for the output binary (you can change the version if you want or need - if this isn’t specified the linker makes an assumption and whines that it had to do so)
    • Link with libSystem
    • STFU about PIE (think ASLR). You probably don’t care about this if you’re just learning assembly. It certainly complicates things, so for now turn it off and ignore it. You should go back and reinvestigate this however before you start using this stuff for production…

Well, crap. That’s going to get old, fast. Well… you can always set an alias! We’ll also fix your ‘$PATH’ variable while we’re at it.

.bash_profile is your friend

If you don’t know what “environment” means in the context of shells, you should probably go read this first. I’m going to assume you know what ‘.bash_profile’, ‘alias’, and ‘$PATH’ are.

if [ -x /usr/hla ]; then
 export PATH=${PATH}:/usr/hla
 alias hla='hla -main:_main -l"macosx_version_min 10.10" -l"lSystem" -l"no_pie"'
fi

This should be straightforward if you’re at all familiar with bash. If you’re not, go read that beginner’s guide at some point, but for now:

  1. Tests if ‘/usr/hla’ is a directory and you have permissions to read into it. Nothing happens if this test fails.
  2. Adds ‘/usr/hla’ to your ‘$PATH’ which is used to search for commands when the absolute path wasn’t specified. I add this last, just to ensure the generically named ‘delete’ command that’s packaged with hla doesn’t replace anything important.
  3. Defines an alias for ‘hla’ that includes the required arguments. TL;DR when you type ‘hla’ it’s replaced with the alias.

Happy HLA-ing!

That should be enough to get it working such that you can dive in to the book. Enjoy!

Disclaimer/Copyright

The information, views, and opinions published on this website were done so in the author's personal capacity. The information, views, and opinions expressed in this article are the author's own and do not reflect the view of their employer, or any other entity unless explicitly stated otherwise.

All data and information provided on this site is for informational purposes only. This website and it's operators makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.

All original content on this website is, unless explicitly stated otherwise, licensed under the MIT license. Full license text is available here. Non-original content that is included on this website in whole or in part, linked, or otherwise made available remains under copyright of the original owners.