• 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: Chall Fry <email@hidden>
  • Date: Thu, 26 Apr 2007 17:33:07 -0700

I am porting some windows code to the mac; it is a bunch of Visual C++ code that is now being built into a Quicktime component. The windows sources make liberal use of Visual Studio style inline assembly, which I would like to take advantage of for the x86 build (we're building a universal component). However, 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
...
}
}

--Except, imagine 40-50 such global constants per file, and 40-50 functions that use them.

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.

If it was only a few globals, this can be worked around by creating a local variable that points to the global--or copies it, whichever is faster. E.g:

long long local_MMX_Copy = g_Some_MMX_Global;
asm {
movq mm0, local_MMX_Copy
}

However, 40 to 50 of these at the start of every function starts getting ridiculous. I could bundle all the globals into a single global struct, and then just make a local pointer to that struct in every function, but the globals used by the asm blocks are widely scattered, with some globals defined in one file being used in asm blocks in another, and some of the values being function statics (with the same name but different values in different functions). Not insurmountable, but the "right" solution is probably to give the inline assembly real globals access.

Everything I've read indicates that the way to do this on x86 is to use a thunk to get the absolute address of a known instruction, and then do math to get the offset of the global you want to access relative to that instruction. Therefore:

void SomeFunctionMMX(void)
{
asm {
jmp CallThunk
LocalThunk: mov ebx, [esp] // CALL pushes the PC of GlobalsBase onto the stack. This snags it.
ret
CallThunk: call LocalThunk
GlobalsBase: // At this point, ebx holds the address of the instruction
// that GlobalsBase is a label for.

movq mm0, [ebx + g_Some_MMX_Global - GlobalsBase]
// Absolute address of GlobalsBase, minus offset of GlobalsBase,
// plus offset of g_Some_MMX_Constant should give absolute addr
// of the global.

or,

sub ebx, OFFSET GlobalsBase
movq mm0, OFFSET g_Some_MMX_Global[ebx]
}

Except that this doesn't work.  I've tried several different permutations, but I believe there is some syntax that I'm just missing. I've tried OFFSET, LROFFSET and IMAGEREL and the C-style address-of operator. I've looked at Microsoft's MASM documentation, Microsoft's documentation on inline assembly blocks in VC++, and Apple's docs on their GCC extension that handles Visual Studio-style asm blocks.

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?

The somewhat wider scope questions: Am I way off base? Can this be done currently in XCode?

Thanks in advance,

--Chall Fry
Critical Path Software

 _______________________________________________
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: Fwd: Including Private Frameworks
  • Next by Date: x86 inline assembly, position independent code, and globals access
  • Previous by thread: Fwd: Including Private Frameworks
  • Next by thread: Re: x86 inline assembly, position independent code, and globals access
  • Index(es):
    • Date
    • Thread