• 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
x86 inline assembly, position independent code, and globals access
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

x86 inline assembly, position independent code, and globals access


  • Subject: x86 inline assembly, position independent code, and globals access
  • From: Robert Purves <email@hidden>
  • Date: Fri, 27 Apr 2007 14:12:59 +1200


Chall Fry wrote:

 the code has many
constructs which look somewhat like this:

static long long g_Some_MMX_Global = 0x8080808080808080LL;

void SomeFunctionMMX(void)
{
	asm {
		movq	mm0, g_Some_MMX_Global
		...
	}
}

This code worked fine in the fixed-loading-location Windows world; it
would probably work fine if I was building an application. However,
my component's code needs to be position-independent, as I understand
it, and this means that the inline assembly can't access
g_Some_MMX_Global without offsetting from a base pointer.

Yes, indeed. Building a dylib, you'll get a link error from g_Some_MMX_Global. Building an app, the code works as is.


What is the correct way to do this? The narrow questions I'm asking
are: How do I write an expression that evaluates to a global
variable's offset within the mach-o section, without creating a local
relocation entry? Can I then use that offset as a displacement from a
base pointer to access the global variable?

Compile (as a dylib) this function, modified from yours to read/write the global without asm:


static long long gSomeGlobal = 12345678LL;

void SomeFunction( void )
{
	long long t;
	t = gSomeGlobal;
	gSomeGlobal = t;
}

Then look at the disassembly, which shows the official method of access, involving a little cutie named ___i686.get_pc_thunk.bx:

	.data
	.align 3
_gSomeGlobal:
	.long	12345678
	.long	0
	.text
.globl _SomeFunction
_SomeFunction:
	nop
	nop
	nop
	nop
	nop
	nop
	pushl	ëp
	movl	%esp, ëp
	pushl	ëx
	subl	$20, %esp
	call	___i686.get_pc_thunk.bx
"L00000000001$pb":
	leal	L_gSomeGlobal$non_lazy_ptr-"L00000000001$pb"(ëx), êx
	movl	(êx), êx
	movl	4(êx), íx
	movl	(êx), êx
	movl	êx, -16(ëp)
	movl	íx, -12(ëp)
	leal	L_gSomeGlobal$non_lazy_ptr-"L00000000001$pb"(ëx), ìx
	movl	(ìx), ìx
	movl	-16(ëp), êx
	movl	-12(ëp), íx
	movl	êx, (ìx)
	movl	íx, 4(ìx)
	addl	$20, %esp
	popl	ëx
	popl	ëp
	ret
	.section __IMPORT,__pointers,non_lazy_symbol_pointers
L_gSomeGlobal$non_lazy_ptr:
	.indirect_symbol _gSomeGlobal
	.long	_gSomeGlobal
	.subsections_via_symbols
	.section __TEXT,__textcoal_nt,coalesced,pure_instructions
.weak_definition	___i686.get_pc_thunk.bx
.private_extern	___i686.get_pc_thunk.bx
___i686.get_pc_thunk.bx:
	movl	(%esp), ëx
	ret

Also see
<http://developer.apple.com/documentation/DeveloperTools/Conceptual/ MachOTopics/Articles/dynamic_code.html#//apple_ref/doc/uid/TP40002528- SW1>


HTH,
Robert P.

_______________________________________________
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


  • Prev by Date: x86 inline assembly, position independent code, and globals access
  • Next by Date: Re: x86 inline assembly, position independent code, and globals access
  • Previous by thread: Re: x86 inline assembly, position independent code, and globals access
  • Next by thread: Build for Tiger on Leopard?
  • Index(es):
    • Date
    • Thread