Aug 13 2010

Linux下写动态链接库.so

Category: Uncategorizedwuxicn @ 9:54 PM

Linux下的动态链接库是.so文件,即:Shared Object,下面是一个简单的例子说明如何写.so以及程序如何动态载入.so中的函数和对象。

testso.h:

#ifndef _TESTSO_H
#define _TESTSO_H
extern "C" {
    int myadd(int a, int b);
    typedef int myadd_t(int, int); // myadd function type
}
#endif // _TESTSO_H

testso.cpp:

#include "testso.h"

extern "C"
int myadd(int a, int  b)
{
    return a + b;
}

编译so:

g++  -shared  -fPIC  -o testso.so testso.cpp

注意,-shared参数和-fPIC参数非常重要:
-shared 告诉gcc要生成的是动态链接库;
-fPIC 告诉gcc生成的生成的代码是非位置依赖的,方面的用于动态链接。

Continue reading “Linux下写动态链接库.so”

Tags: , , ,


Apr 25 2010

[zz]Guide: Function Calling Conventions

Category: Uncategorizedwuxicn @ 9:55 PM

Guide: Function Calling Conventions

GCC follows certain rules in generating and calling its functions. If you are writing portable C or C++ code, you never need to know about these rules. However, if you are writing assembly language or nonportable code that depends on these rules, you need to know what they are. This document attemps to describe them, and gives some examples.

Notes

This document assumes a familiarity with assembly language. The assembler code used here is written in the AT&T syntax, as used by GNU as. If you’re using an Intel-syntax assembler, like nasm, you’ll have to translate appropriately.

What’s described here are GCC’s standard calling conventions. Many can be changed by using options like -mregparm, but that’s outside the scope of this document.

These conventions apply to C. C++ introduces several additional complications (such as class pointers and name mangling), some of which can change between compiler versions. Thus, I suggest that asm functions called from C++ code be declared as extern "C". This will cause C calling conventions to be used.

Writing Assembly-Language Functions

Naming

In DJGPP, a function’s assembly-language name is the same as its C name, with an underscore (“_“) prepended. Thus, the C function foo would be named _foo in assembly language. (This is in fact true for all symbol names, such as variables.) C++ has some much more complicated rules.

Registers

GCC requires that some registers not change across a function call. If you want to use these registers in an assembly function, you must save and restore their values. They are:

  • %ebx
  • %esi
  • %edi
  • %ebp
  • The segment registers %ds%es and %ss

Other registers are available for your use (though some have other special uses; read on).

  • Integers (of any size up to 32 bits) and pointers are returned in the %eax register.
  • Floating point values are returned in the 387 top-of-stack register, st(0).
  • Return values of type long long int are returned in %edx:%eax (the most significant word in %edx and the least significant in %eax).
  • Returning a structure is complicated and rarely useful; try to avoid it. (Note that this is different from returning a pointer to a structure.)

If your function returns void (e.g. no value), the contents of these registers are not used.

          Last argument
	  ...
4(%esp)	  First argument
(%esp)    Return address

  • Integers up to 32 bits and pointers are pushed as a single longword.
  • long long int is pushed as two longwords; the least significant is pushed last (and so is located first in memory).
  • float and double are pushed as a double-precision value, occupying 8 bytes.
  • long double is pushed as an extended-precision value followed by 2 bytes of padding, totalling 12 bytes.
  • As before, structures are more complicated and best avoided.

These rules also apply to functions which take a variable number of arguments (like printf). As with any variadic function, the function must find its own way of determining how many arguments were actually passed (usually based on one of the required args).

The stack below the return address is available for temporary storage, but be sure to decrement %esp appropriately. Memory below %esp may be overwritten asynchronously, by interrupt handlers and such. Restore its value when exiting, so that the return works correctly. You may also push and pop at will.

You may modify your arguments in place if you wish; they will not be reused by the caller. Do not, however, attempt to pop them; the caller handles this.

Calling C Functions From Assembly Language

An assembly language function may wish to call a function written in C, either your own or one from the standard library. The same rules already explained apply; you just see them from the other side.

First, you push the function’s arguments (if any) onto the stack, last argument first. See above for the formats used. (Floating point values are usually most easily handled by making space on the stack and then executing a store instruction; i.e. subl $8,%esp; fstpl (%esp).)

Use a simple call instruction to call the function.

You are responsible for removing the arguments you have pushed. They may have changed, so you may not reuse them. You need not, however, discard them at once; it may be more convenient when calling several functions to leave the arguments on the stack and pop them all together at the end. addl n,%esp is an efficient way to do this. It may also be convenient in this case to use %ebp as a frame pointer, since it need not change all the time. (The C compiler does this.)

The return value may be found as detailed above.

Expect the registers %eax%ecx, and %edx, as well as the floating-point stack, to have changed. Standard library functions may modify the %gs register, and the_far* functions may modify %fs. Other registers will be preserved.

Conclusion

These are the basic calling conventions used by GCC; however, there are special cases, optional modifications, etc. that can apply in situations not covered here. In this case, gcc -S is your best friend – from assembly output, you can usually figure out the rules. Also helpful is the GCC source: see i386.h and i386.md inconfig/i386. They are well commented.

Examples

(略,见原文)

原文:http://www.delorie.com/djgpp/doc/ug/asm/calling.html

Tags: , ,


Dec 17 2009

【转】Using GNU C __attribute__

Category: Uncategorizedwuxicn @ 4:04 PM

原文:http://unixwiz.net/techtips/gnu-c-attributes.html

One of the best (but little known) features of GNU C is the __attribute__ mechanism, which allows a developer to attach characteristics to function declarations to allow the compiler to perform more error checking. It was designed in a way to be compatible with non-GNU implementations, and we’ve been using this for years in highly portable code with very good results.

Note that __attribute__ spelled with two underscores before and two after, and there are always two sets of parentheses surrounding the contents. There is a good reason for this – see below. Gnu CC needs to use the -Wall compiler directive to enable this (yes, there is a finer degree of warnings control available, but we are very big fans of max warnings anyway).

Table of Contents

  1. __attribute__ format
  2. __attribute__ noreturn
  3. __attribute__ const
  4. Putting them together
  5. Compatibility with non-GNU compilers
  6. Other References

Continue reading “【转】Using GNU C __attribute__”

Tags: ,


Nov 15 2009

gcc -S 将c源程序编译成汇编代码

Category: Uncategorizedwuxicn @ 10:00 PM

用gcc将C的源程序进行部分编译,将其翻译成汇编代码(不进行目标代码的编译和链接)。
方法:gcc -S
例子:
gcc -S hello.c

通过这个可以看看写的代码将会生成的汇编,分析代码的效率。

Tags: ,