The documentation of option -mcmodel=medium says:
-mcmodel=medium
Generate code for the medium model: The program is linked in the lower 2 GB of
the address space but symbols can be located anywhere in the address space.
Programs can be statically or dynamically linked, but building of shared
libraries are not supported with the medium model.

This is misleading since the compiler still uses 32-bit addresses for data
objects on Linux (and BSD?) targets. The program data are still limited to
addresses < 2 GB. Dynamically allocated memory (new or malloc) can probably
exceed the 2GB address limit in both the small and the medium memory model.
Whatever the difference is between small and medium memory models, it is not
covered by the above explanation. On Mac OS X (Darwin) targets, all addresses
are above 4GB by default for both small and medium models. On Windows targets,
a DLL can be loaded at addresses > 2 GB though this rarely happens.

Example:
---------- code file a.cpp ---------------
int * mypointer = 0;
int myarray[100] = {0};

int myfunction (int x) {
   mypointer = myarray + 1;
   return myarray[x];
}
---------- end of code file a.cpp ---------------

Command line:
g++ -m64 -mcmodel=medium -S a.cpp

gcc version:
gcc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)

---------- assembly output (excerpt) -----------
_Z10myfunctioni:
.LFB2:
        pushq   %rbp
.LCFI0:
        movq    %rsp, %rbp
.LCFI1:
        movl    %edi, -4(%rbp)
        movl    $myarray+4, %eax         # uses 32-bit zero-extended address
here!
        movq    %rax, mypointer(%rip)
        movl    -4(%rbp), %eax
        cltq
        movl    myarray(,%rax,4), %eax   # uses 32-bit sign-extended address
here!
        leave
        ret
---------- end of assembly output (excerpt) -----------

This example shows that the statement "symbols can be located anywhere in the
address space" is misleading. Static symbols must be located at addresses < 2GB
for the above code to work. Does the above statement apply to symbols on the
stack or only to objects allocated with new or malloc? The statement is
definitely wrong for Mac targets, and possibly also for Windows targets.

If a correct description would be too long then there may be a link to more
exact descriptions elsewhere.


-- 
           Summary: Documentation of option -mcmodel=medium is wrong
           Product: gcc
           Version: 4.2.3
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: agner at agner dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37880

Reply via email to