Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

x86 inline assembly, position independent code, and globals access




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	%ebp
	movl	%esp, %ebp
	pushl	%ebx
	subl	$20, %esp
	call	___i686.get_pc_thunk.bx
"L00000000001$pb":
	leal	L_gSomeGlobal$non_lazy_ptr-"L00000000001$pb"(%ebx), %eax
	movl	(%eax), %eax
	movl	4(%eax), %edx
	movl	(%eax), %eax
	movl	%eax, -16(%ebp)
	movl	%edx, -12(%ebp)
	leal	L_gSomeGlobal$non_lazy_ptr-"L00000000001$pb"(%ebx), %ecx
	movl	(%ecx), %ecx
	movl	-16(%ebp), %eax
	movl	-12(%ebp), %edx
	movl	%eax, (%ecx)
	movl	%edx, 4(%ecx)
	addl	$20, %esp
	popl	%ebx
	popl	%ebp
	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), %ebx
	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:
http://lists.apple.com/mailman/options/xcode-users/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.