0

Consider the following program:

#include <iostream>

class A {
    public:
        A() {}
        virtual void a() {};
};

class B : public A { };

int main() {
    B();
}

GCC (tested 4.4.0, 8.3.0 and 9.1) generates the following Code for B::B() (godbolt link):

        call    A::A() [base object constructor]
        movl    $vtable for B+16, %edx

Note that it does a 32-bit move in a 64-bit program. How can GCC be sure that the .text section storing the vtable will end up in a 32-bit address?

Sebastian Graf
  • 3,602
  • 3
  • 27
  • 38
  • Because GCC knows where everything is, as it is itself responsible for placing the data there? – Some programmer dude May 07 '19 at 12:35
  • Well, I assume that it can provide the linker with the right flags to arrange it in a way that that is the case. I was wondering where that is appearant in assembler output, which it doesn't seem to be. E.g. you have to provide the right linker flags when linking the resulting a.out. – Sebastian Graf May 07 '19 at 12:44

1 Answers1

0

This is by GCC's small code model (-mcmodel=small). Try adding -mcmodel=large to the compiler options and see how it will emit a movabsq instead.

Cudos to How can gcc/clang assume a string constant's address is 32-bit?.

Sebastian Graf
  • 3,602
  • 3
  • 27
  • 38