time to bleed by Joe Damato

technical ramblings from a wanna-be unix dinosaur

Dynamic Linking: ELF vs. Mach-O

View Comments


If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter.

The intention of this post is to highlight some of the similarities and differences between ELF and Mach-O dynamic linking that I encountered while building memprof.

I hope to write more posts about similarities and differences in other aspects of Mach-O and ELF that I stumbled across to shed some light on what goes on down there and provide (in some cases) the only documentation.

Procedure Linkage Table

The procedure linkage table (PLT) is used to determine the absolute address of a function at runtime. Both Mach-O and ELF objects have PLTs that are generated at compile time. The initial table simply invokes the dynamic linker which finds the symbol you want. The way this works is very similar at a high level in ELF and Mach-O, but there are some implementation differences that I thought were worth mentioning.

Mach-O PLT arrangement

Mach-O objects have several different sections across different segments that are all involved to create a PLT entry for a specific symbol.

Consider the following assembly stub which calls out to the PLT entry for malloc:

# MACH-O calling a PLT entry (ELF is nearly identical)
0x000000010008c504 [str_new+52]:	callq  0x10009ebbc [dyld_stub_malloc]

The dyld_stub prefix is added by GDB to let the user know that the callq instruction is calling a PLT entry and not malloc itself. The address 0x10009ebbc is the first instruction of malloc‘s PLT entry in this Mach-O object. In Mach-O terminology, the instruction at 0x10009ebbc is called a symbol stub. Symbol stubs in Mach-O objects are found in the __TEXT segment in the __symbol_stub1 section.

Let’s examine some instructions at the symbol stub address above:

# MACH-O "symbol stubs" for malloc and other functions
0x10009ebbc [dyld_stub_malloc]:	  jmpq   *0x3ae46(%rip)        # 0x1000d9a08
0x10009ebc2 [dyld_stub_realloc]:  jmpq   *0x3ae48(%rip)        # 0x1000d9a10
0x10009ebc8 [dyld_stub_seekdir$INODE64]:	jmpq   *0x3ae4c(%rip)  # 0x1000d9a20
. . . .

Each Mach-O symbol stub is just a single jmpq instruction. That jmpq instruction either:

  • Invokes the dynamic linker to find the symbol and transfer execution there
  • OR

  • Transfers execution directly to the function.

via an entry in a table.

In the example above, GDB is telling us that the address of the table entry for malloc is 0x1000d9a08. This table entry is stored in a section called the __la_symbol_ptr within the __DATA segment.

Before malloc has been resolved, the address in that table entry points to a helper function which (eventually) invokes the dynamic linker to find malloc and fill in its address in the table entry.

Let’s take a look at what a few entries of the helper functions look like:

# MACH-O stub helpers
0x1000a08d4 [stub helpers+6986]:	pushq  $0x3b73
0x1000a08d9 [stub helpers+6991]:	jmpq   0x10009ed8a [stub helpers]
0x1000a08de [stub helpers+6996]:	pushq  $0x3b88
0x1000a08e3 [stub helpers+7001]:	jmpq   0x10009ed8a [stub helpers]
0x1000a08e8 [stub helpers+7006]:	pushq  $0x3b9e
0x1000a08ed [stub helpers+7011]:	jmpq   0x10009ed8a [stub helpers]
. . . .

Each symbol that has a PLT entry has 2 instructions above; a pair of pushq and jmpq. This instruction sequence sets an ID for the desired function and then invokes the dynamic linker. The dynamic linker looks up this ID so it knows which function it should be looking for.

ELF PLT arrangement

ELF objects have the same mechanism, but organize each PLT entry into chunks instead of splicing them out across different sections. Let’s take a look at a PLT entry for malloc in an ELF object:

# ELF complete PLT entry for malloc
0x40f3d0 [malloc@plt]:	jmpq   *0x2c91fa(%rip)        # 0x6d85d0
0x40f3d6 [malloc@plt+6]:	pushq  $0x2f
0x40f3db [malloc@plt+11]:	jmpq   0x40f0d0
. . . .

Much like a Mach-O object, an ELF object uses a table entry to direct the flow of execution to either invoke the dynamic linker or transfer directly to the desired function if it has already been resolved.

Two differences to point out here:

  1. ELF puts the entire PLT entry together in nicely named section called plt instead of splicing it out across multiple sections.
  2. The table entries indirected through with the initial jmpq instruction are stored in a section named: .got.plt.

Both invoke an assembly trampoline…

Both Mach-O and ELF objects are set up to invoke the runtime dynamic linker. Both need an assembly trampoline to bridge the gap between the application and the linker. On 64bit Intel based systems, linkers in both systems must comply to the same Application Binary Interace (ABI).

Strangely enough, the two linkers have slightly different assembly trampolines even though they share the same calling convention1 2.

Both trampolines ensure that the program stack is 16-byte aligned to comply with the amd64 ABI’s calling convention. Both trampolines also take care to save the “general purpose” caller-saved registers prior to invoking the dynamic link, but it turns out that the trampoline in Linux does not save or restore the SSE registers. It turns out that this “shouldn’t” matter, so long as glibc takes care not to use any of those registers in the dynamic linker. OSX takes a more conservative approach and saves and restores the SSE registers before and after calling out the dynamic linker.

I’ve included a snippet from the two trampolines below and some comments so you can see the differences up close.

Different trampolines for the same ABI

The OSX trampoline:

dyld_stub_binder:
  pushq   %rbp
  movq    %rsp,%rbp
  subq    $STACK_SIZE,%rsp  # at this point stack is 16-byte aligned because two meta-parameters where pushed
  movq    %rdi,RDI_SAVE(%rsp) # save registers that might be used as parameters
  movq    %rsi,RSI_SAVE(%rsp)
  movq    %rdx,RDX_SAVE(%rsp)
  movq    %rcx,RCX_SAVE(%rsp)
  movq    %r8,R8_SAVE(%rsp)
  movq    %r9,R9_SAVE(%rsp)
  movq    %rax,RAX_SAVE(%rsp)
  movdqa    %xmm0,XMMM0_SAVE(%rsp)
  movdqa    %xmm1,XMMM1_SAVE(%rsp)
  movdqa    %xmm2,XMMM2_SAVE(%rsp)
  movdqa    %xmm3,XMMM3_SAVE(%rsp)
  movdqa    %xmm4,XMMM4_SAVE(%rsp)
  movdqa    %xmm5,XMMM5_SAVE(%rsp)
  movdqa    %xmm6,XMMM6_SAVE(%rsp)
  movdqa    %xmm7,XMMM7_SAVE(%rsp)
  movq    MH_PARAM_BP(%rbp),%rdi  # call fastBindLazySymbol(loadercache, lazyinfo)
  movq    LP_PARAM_BP(%rbp),%rsi
  call    __Z21_dyld_fast_stub_entryPvl

The OSX trampoline saves all the caller saved registers as well as the the %xmm0 - %xmm7 registers prior to invoking the dynamic linker with that last call instruction. These registers are all restored after the call instruction, but I left that out for the sake of brevity.

The Linux trampoline:

  subq $56,%rsp
  cfi_adjust_cfa_offset(72) # Incorporate PLT
  movq %rax,(%rsp)  # Preserve registers otherwise clobbered.
  movq %rcx, 8(%rsp)
  movq %rdx, 16(%rsp)
  movq %rsi, 24(%rsp)
  movq %rdi, 32(%rsp)
  movq %r8, 40(%rsp)
  movq %r9, 48(%rsp)
  movq 64(%rsp), %rsi # Copy args pushed by PLT in register.
  movq %rsi, %r11   # Multiply by 24
  addq %r11, %rsi
  addq %r11, %rsi
  shlq $3, %rsi
  movq 56(%rsp), %rdi # %rdi: link_map, %rsi: reloc_offset
  call _dl_fixup    # Call resolver.

The Linux trampoline doesn’t touch the SSE registers because it assumes that the dynamic linker will not modify them thus avoiding a save and restore.

Conclusion

  • Tracing program execution from call site to the dynamic linker is pretty interesting and there is a lot to learn along the way.
  • glibc not saving and restoring %xmm0-%xmm7 kind of scares me, but there is a unit test included that disassembles the built ld.so searching it to make sure that those registers are never touched. It is still a bit frightening.
  • Stay tuned for more posts explaining other interesting similarities and differences between Mach-O and ELF coming soon.

Thanks for reading and don’t forget to subscribe (via RSS or e-mail) and follow me on twitter.

References

  1. http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/LowLevelABI/140-x86-64_Function_Calling_Conventions/x86_64.html#//apple_ref/doc/uid/TP40005035-SW1 []
  2. http://www.x86-64.org/documentation/abi.pdf []

Written by Joe Damato

May 12th, 2010 at 7:00 am

  • Florin Andrei
    That explains the otherwise unexplainable "event" last year, when we had our AmEx card hijacked during a trip to Eastern Europe. All they needed was one attempt to login to the AmEx site.
  • Wow... that's amazing! What a screw up, somebody is getting fired...
  • skhan
    This is the cost of outsourcing...I could care less what they say!
  • given that AMEX and other credit card companies (usury companies) steal your money as fast as they can, why should they care if other people steal it too? They still make their cut when someone steals your money.
  • Brad
    Can you post the URL from the email? There is no proof anywhere in this that it loads over HTTP.

    From looking at the URL it looks like its built to load inside an IFRAME on the page.
    If the IFRAME is loaded under HTTPS there wouldn't be a problem except for if someone is in your network and forces you to load the HTTP version.
  • wireshark output is proof that the post went back over cleartext.
  • George
    That's not their only problem. Go to that site and use Wireshark to watch what's loaded by that page. The AMEX site loads a bunch of markup & JavaScript from other domains including sharethis.com, googleadservices.com, google-analytics.com, and doubleclick.net.

    They've basically reduced their page security to the lowest common denominator. If any of those sites or DNS entries are compromised the AMEX site is compromised. Doesn't the PCI DSS also say something about reviewing custom code? I wonder if they've reviewed all that code they load from others? It's hard to belive a card brand rolled a site with so many issues.
  • I agree this is a huge issue.

    Say for example someone loads a plugin for doubleclick that captures all the inputs on a page and logs them for tracking purposes. All those form fields are sent plain text to a seperate server, where it could potentially be seen by countless people - both at amex and doubleclick. A bean counter at amex could update the tracking without anyone knowing and grab everyone's credit card info easy as pie!

    EDIT: Looks like they updated the page to load the form in https (same domain) inside an iframe. Fortunately, that means scripts in the parent window can't access the form. Glad they updated, but great to make an example of this for other devs to learn from!
  • George
    The unfortunate thing is that iframe jails do very little to secure anything & the fact that the form now submits via HTTPS means nothing so long as the page loaded via HTTP. How would my mom for instance know it's safe to provide her card information since there's no https: and lock for her to check? Should she fire up Wireshark and do a test transaction? LOL!

    It gets worse. In the "old days" of http based MitM it took a skilled person to do anything more than sniff the connection. Now days there are plenty of freely available packages, including some "point and click" ones that allow attackers to insert their own markup and scripts into a an HTTP delivered page to then deliver whatever they'd like.

    The only way for AMEX to fix this is to turn the entire site to HTTPS & stop loading third-party JavaScript which they have zero control over.
  • Great point, seems like just a temporary "technical" fix rather than a comprehensive solution.

    If I don't see that lock up there and special treatment of the url box, I'm not putting in my CC info. Even then, I'm fairly certain that if they have the page and the iframe on HTTPS, those 3rd party scripts can then access all the data in the iframe. Ironic that adding HTTPS to the parent page reduces security!
  • The account management interface makes me rage every time as well. If I recall correctly, I believe the client side validation differs from the server side one. And if I change my password, the rules and validation is different from entering it for the first time!

    Awful.
  • Grrr
    Crappy Indian coding done by a company run by an Indian, outsourcing jobs everyday!
  • Mike
    Speaking of basic security problems, I wandered over to homerun.com as you suggested. I tried to sign up, but entered something incorrectly and was returned to the sign-up form. To my surprise, my password was pre-filled for me - embedded in the HTML source of the page. Also, sent over HTTP.

    It's not a credit card number, it's a basic throwaway web password, but still. Basic Security.
  • Word. repeat password was pre-filled. that is a bug. fixing it now, thanks.

    it's sent over http but encrypted on the client before being sent.
  • Remington Winters
    Encrypted on the client? That sounds suspiciously like javascript. Client-side security is not security.
  • WOW!!! Credit card number is sent via plain socket! Evil guy "only" needs to stand between you and American Express server to know your card number and your name.

    It's far easier to get that info when you pay for something in the supermarket or in the bar.

    Yes, they suck at security. Yes, page lies saying that form is secure. But that's not an issue. Someone must be really paranoid to be concerned about that (and it seems you are). And if one is so paranoid, then he, probably, should take tons of active precautions about sending any of his data over internet anyway. So you're picking on a really little thing.
  • Yes, by all means, let's forget about people who are not browsing from home. I mean, come on, who browses from an Internet café these days or from their office or a place like that? And even if they did, I'm pretty sure all of the places they browse from are trustworthy and have good security, so nobody can sniff on your data.

    Oh, and I guess that there's no way someone is going to duplicate that form and try to use it for phishing. And if they did, the usefulness of the SSL certificate for preventing phishing is exaggerated, right?

    Wake up, Sergey. There's a reason why HTTPS is important.
  • Brad
    I hope all your personal information gets stolen when you go to your credit card and bank websites from a public location. I also hope you go shopping online too without using saved credit card information and companies that don't use email verification for when shipping address is different than billing.

    Btw, I agree SSL is important. The PCI certification I got pass at my company is at level 3 or 1.. whatever the highest level is. The HTTPS should definitely have been enforced on the submitting page... not the calling page.
  • You're all heart, Brad. Let's see, your contribution was to flame me and then add nothing useful to what I said.

    I'm not going to discuss my personal shopping and banking habits with you, or the security precautions I might take under either normal or exceptional circumstances. I will, however, point out why your reply was a completely useless flame: if a public location is not safe due to factors that have to do with the customer or the location itself, it's not an excuse for a bank or a merchant to fail at their end of the security. That's what we were all criticizing here, in case you forgot.
  • Troll? I'm just saying, using SSL is pretty easy. Why not use SSL and not worry about it?
  • Sure, SSL is easy and why not. Probably, it's just much less important for me than for you. My intent was rather sarcastic, not trolling actually.
  • I wish I could say that this surprises me, but given their god-awful website and their shoddy password restrictions, I am not surprised in the least.
  • Jeff
    Given proper handling of authentication (e.g. timeouts, banning suspect IPs), complex passwords aren't necessary.
  • As if the lack of special characters in passwords on their min site wasn't bad enough...
  • wagerlabs
  • Dhruv
    No.
blog comments powered by Disqus