generated code for vtable

support for the ARM toolchain
Post Reply
Peter
Posts: 6
Joined: Fri Apr 10, 2009 9:03 am
Contact:

generated code for vtable

Post by Peter » Fri Apr 10, 2009 9:18 am

I wonder why the virtual function table always contains two 0 addresses beside the actual addresses of member functions marked with the virtual keyword.

Here is an example:

Code: Select all

#include <stdio.h>

class MyClass
{
public:
  virtual void Peng() {}
  virtual void Boing(){}
};

int main(void)
{
  MyClass myClass;
  myClass.Peng();
  myClass.Boing();

  return 0;
}
I compiled the code with R25, with -marm and optimization level -O1. The remaining code generation settings are the same as the from the libnds example. The generated instructions for the code above are:

Code: Select all

	.arch armv5te
	.fpu softvfp
	.eabi_attribute 23, 1
	.eabi_attribute 24, 1
	.eabi_attribute 25, 1
	.eabi_attribute 26, 1
	.eabi_attribute 30, 1
	.eabi_attribute 18, 4
	.file	"main.cpp"
	.section	.text._ZN7MyClass4PengEv,"axG",%progbits,_ZN7MyClass4PengEv,comdat
	.align	2
	.weak	_ZN7MyClass4PengEv
	.type	_ZN7MyClass4PengEv, %function
_ZN7MyClass4PengEv:
	@ args = 0, pretend = 0, frame = 0
	@ frame_needed = 0, uses_anonymous_args = 0
	@ link register save eliminated.
	bx	lr
	.size	_ZN7MyClass4PengEv, .-_ZN7MyClass4PengEv
	.section	.text._ZN7MyClass5BoingEv,"axG",%progbits,_ZN7MyClass5BoingEv,comdat
	.align	2
	.weak	_ZN7MyClass5BoingEv
	.type	_ZN7MyClass5BoingEv, %function
_ZN7MyClass5BoingEv:
	@ args = 0, pretend = 0, frame = 0
	@ frame_needed = 0, uses_anonymous_args = 0
	@ link register save eliminated.
	bx	lr
	.size	_ZN7MyClass5BoingEv, .-_ZN7MyClass5BoingEv
	.text
	.align	2
	.global	main
	.type	main, %function
main:
	@ args = 0, pretend = 0, frame = 0
	@ frame_needed = 0, uses_anonymous_args = 0
	@ link register save eliminated.
	mov	r0, #0
	bx	lr
	.size	main, .-main
	.weak	_ZTV7MyClass
	.section	.rodata._ZTV7MyClass,"aG",%progbits,_ZTV7MyClass,comdat
	.align	3
	.type	_ZTV7MyClass, %object
	.size	_ZTV7MyClass, 16
_ZTV7MyClass:
	.word	0
	.word	0
	.word	_ZN7MyClass4PengEv
	.word	_ZN7MyClass5BoingEv
	.ident	"GCC: (devkitARM release 25) 4.3.3"

I have two questions regarding the following code:

Code: Select all

	.weak	_ZTV7MyClass
	.section	.rodata._ZTV7MyClass,"aG",%progbits,_ZTV7MyClass,comdat
	.align	3
	.type	_ZTV7MyClass, %object
	.size	_ZTV7MyClass, 16
_ZTV7MyClass:
	.word	0
	.word	0
	.word	_ZN7MyClass4PengEv
	.word	_ZN7MyClass5BoingEv
	.ident	"GCC: (devkitARM release 25) 4.3.3"
  • Why there are two 0 entries in the vtable _ZTV7MyClass?
  • Why .align 3 rather than .align 4?
Thanks in advance!

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests