Different code output based upon c++ library being used?
Garrett Kajmowicz
gkajmowi@tbaytel.net
Fri May 6 05:19:00 GMT 2005
I'm the lead developer on the embedded C++ library at cxx.uclibc.org. I have
g++ use my library through the use of the switches
-fno-builtin -nostdinc++ -nodefaultlibs -I/includedir -L/libdir -lc
I've noticed that the code created when compiled against my library is
different (and larger) than against GNU libstdc++, even when no such
difference should occur. For example, the class:
class base {
protected:
int a;
public:
base() : a(0){
printf("Executing default base class constructor. a: %i\n",
a);
}
virtual ~base(){
printf("Executing base class destructor\n");
}
virtual void print(){
printf("Base class print function with a=%i\n", a);
}
};
Under gcc 4.0 with my library, the code generated for the function
_ZN4baseC2Ev or base::base() is (from objdump):
08048c00 <_ZN4baseC2Ev>:
8048c00: 55 push %ebp
8048c01: 89 e5 mov %esp,%ebp
8048c03: 53 push %ebx
8048c04: 83 ec 14 sub $0x14,%esp
8048c07: e8 2d 04 00 00 call 8049039
<__i686.get_pc_thunk.bx>
8048c0c: 81 c3 38 1a 00 00 add $0x1a38,%ebx
8048c12: 8b 83 30 00 00 00 mov 0x30(%ebx),%eax
8048c18: 8d 50 08 lea 0x8(%eax),%edx
8048c1b: 8b 45 08 mov 0x8(%ebp),%eax
8048c1e: 89 10 mov %edx,(%eax)
8048c20: 8b 45 08 mov 0x8(%ebp),%eax
8048c23: c7 40 04 00 00 00 00 movl $0x0,0x4(%eax)
8048c2a: 8b 45 08 mov 0x8(%ebp),%eax
8048c2d: 8b 40 04 mov 0x4(%eax),%eax
8048c30: 89 44 24 04 mov %eax,0x4(%esp,1)
8048c34: 8d 83 f0 ea ff ff lea 0xffffeaf0(%ebx),%eax
8048c3a: 89 04 24 mov %eax,(%esp,1)
8048c3d: e8 36 fa ff ff call 8048678 <_init+0x78>
8048c42: 83 c4 14 add $0x14,%esp
8048c45: 5b pop %ebx
8048c46: 5d pop %ebp
8048c47: c3 ret
The code generated against libstdc++ is:
08048ae2 <_ZN4baseC2Ev>:
8048ae2: 55 push %ebp
8048ae3: 89 e5 mov %esp,%ebp
8048ae5: 83 ec 08 sub $0x8,%esp
8048ae8: ba 88 8f 04 08 mov $0x8048f88,%edx
8048aed: 8b 45 08 mov 0x8(%ebp),%eax
8048af0: 89 10 mov %edx,(%eax)
8048af2: 8b 45 08 mov 0x8(%ebp),%eax
8048af5: c7 40 04 00 00 00 00 movl $0x0,0x4(%eax)
8048afc: 8b 45 08 mov 0x8(%ebp),%eax
8048aff: 8b 40 04 mov 0x4(%eax),%eax
8048b02: 89 44 24 04 mov %eax,0x4(%esp,1)
8048b06: c7 04 24 94 8f 04 08 movl $0x8048f94,(%esp,1)
8048b0d: e8 3a fa ff ff call 804854c <_init+0x68>
8048b12: c9 leave
8048b13: c3 ret
The noticable difference is that the code generated against my library calls
<__i686.get_pc_thunk.bx>, plus additional setup code for a total of 20
additional bytes. Now this is very small, but when generating code for
embedded systems, 20 bytes time hundreds of functions becomes an issue.
Please advise on why this difference is occuring and what I can do to avoid
this occurance.
- Garrett Kajmowicz
More information about the Gcc-help
mailing list