• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: trouble getting Hello World / gas / Mac OS X to work
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: trouble getting Hello World / gas / Mac OS X to work


  • Subject: Re: trouble getting Hello World / gas / Mac OS X to work
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Mon, 15 Oct 2012 23:59:54 +0200


Le 15 oct. 2012 à 21:25, Chris Lattner <email@hidden> a écrit :


On Oct 15, 2012, at 11:56 AM, Andrew Pennebaker <email@hidden> wrote:

I'm trying to write Hello World in GNU assembler for Mac OS X, but it's bus erroring before it can print "Hello World".

Hi Andrew,

Mac OS/X and the linux/freebsd kernels work very differently from each other.  "int $80" is the way to enter the kernel on Linux (at least), but the kernel interface on the Mac is a completely different.  The supported way to access the kernel is through the public APIs exposed by libsystem.

If you'd like to hack something up for fun and experimentation, you can try disassembling libsystem with otool to find out more details about specific entrypoints you're interested in.   However, note that this is just for the system you are currently working on and they change frequently.  This is not a supported or endorsed way to build an application!


Note that a more user friendly way to know how libsystem works is to dig in the sources distributed at http://opensource.apple.com/. Not all libSystem is available, but there is interesting parts.

And also, you may be interested by this previous experiment to get a working exec from bare asm:  http://www.feiri.de/macho/


-Chris 


hello.s:
.global start

.data

.equ stdout, 1

.equ sys_write, 4
.equ sys_exit, 1

.equ kernel, 0x80

msg: .asciz "Hello World!\n"
.equ len, .-msg

.text

start:
	push $len
	push $msg
	push $stdout
	mov $sys_write, êx
	sub $4, %esp
	int $kernel
	add $4 + $4 * $3, %esp

	push $0
	mov $sys_exit, êx
	sub $4, %esp
	int $kernel
Trace:

$ clang -c -o hello.o hello.s
$ ld -o hello -macosx_version_min 10.6 hello.o
$ ./hello
Bus error: 10

$ gdb hello
(gdb) run
Starting program: /Users/andrew/Desktop/src/mcandre/gas/hello/freebsd/hello 

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x0000000000002000
0x0000000000002000 in msg ()

I'm basing my code off of a couple things:
  • Working NASM / Mac OS X code for Hello World
  • Working Gas / Linux code for Hello World
I took the semantics from NASM / Mac OS X and the syntax from Gas / Linux.

I also tried the example program on the GNU Assembler Wikipedia page, but it also bus error's.

I filed a bug report with Apple due to its GNU assembler being horribly out of date (as 1.38). They said to use clang instead, so here I am.

What am I doing wrong?

Specs:

* ld64-134.9
* clang 4.1
* Xcode 4.5
* Mac OS X 10.8.2
* MacBook Pro 2009

--
Cheers,

Andrew Pennebaker
www.yellosoft.us
-- Jean-Daniel




 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >trouble getting Hello World / gas / Mac OS X to work (From: Andrew Pennebaker <email@hidden>)
 >Re: trouble getting Hello World / gas / Mac OS X to work (From: Chris Lattner <email@hidden>)

  • Prev by Date: Re: Xcode do not jump to breakpoints
  • Next by Date: Why ARC over garbage collection?
  • Previous by thread: Re: trouble getting Hello World / gas / Mac OS X to work
  • Next by thread: ld linker freezes my computer
  • Index(es):
    • Date
    • Thread