Monday, February 27, 2012

Fun and games with assembly

This is a problem that came up during my research. Suppose I have a function foo whose assembly looks like the following:

foo:
  mov global($rip), %rax
  mov (%rax), %rax
  retq

Are the following two lines of code equivalent?

  /* defines somewhere */
  int val;
  extern int foo(void);
  /* actual code */
  asm("callq foo" : "=a"(val));
  val = foo();

The answer, as you might guess by the fact that I'm writing this post, is "no." The reason why is a lot more complex. In the case that caused me endless toil and grief, the function foo is not located in the same executable as the caller; it is in an external library file. Now, the platform ABI allows functions to clobber a fair number of registers which callers must assume are unusable. If the loader needs to perform work when calling a global relocated function, then it is a good idea to use those registers where possible, since you don't need to bother saving those values. When the asm construct is invoked, we've declared that only the return register is clobbered, so the compiler is free to store some values over the call. When the function call is direct and doesn't need to go through the GOT or anything similar, this is perfectly fine and works as one would expect. But if you need to invoke the loader when calling through the GOT, then you now have registers whose values have suddenly mysteriously changed on you when calling a function which doesn't (appear to) use them. Hope you have more fun debugging those cases than I did!

3 comments:

Josiah said...

The loader aside, claiming that a call instruction only clobbers particular registers is pretty dubious stylistically when the function called is off in space somewhere. Seems easy to modify the assembly function and not realize that the code depended on a weird, non-standard calling convention. I'd want that commented well on both ends of the call, at least.

Tech to Review said...

YoMovies is a popular streaming website known for offering a wide selection of movies and TV shows, including Bollywood, Hollywood, and regional content. With a user-friendly interface, YoMovies makes it easy for users to browse through genres, top-rated titles, and the latest releases. However, users should be aware that the content on YoMovies may not always be legally licensed. For those looking to enjoy quality streaming, exploring legal streaming options is advisable to support creators and enjoy uninterrupted, ad-free content.

Daneen Kareem said...

Fun game is a part of life. Art is often done by big people and games are often played by small people but this is not the case, play games and art all your life but work hard on your studies my friends after hard work of best logo designer. The first game is Human Resource Machine, originally released in 2015 by Tomorrow Corp.