Alfred Huang's Weblog

Disclaimer: Whatever I suggested in my blog is what I would do, it does not necessarily mean the only way to do it.


« AMD64 Memory Models | Main | A look into the... »
Tuesday Jan 31, 2006

Kpic under Small model versus Medium model

Continuing on my previous discussion of medium model. Some people pointed out their previously "address does not fit" application actually would link and run using -Kpic, without using -xmodel=medium.

Yes, Position Independent Code may buy you farther addressing capability, but it has a limit based on code size and number of global statics and may not be as efficient as the medium model code.

So what is the difference between Position Independent Code under small model and the medium model code?

PIC code goes through the Global Offset Table (GOT), which the linker usually creates beneath the text section. It contains the actual 64-bit addresses of the static objects. Access to an object under the PIC mode consists of a 4-byte displacement reference from the referencing point in the text to the corresponding entry in the GOT, the 64-bit address in the GOT is then picked up and referenced indirectly. With the full 64-bit address referenced indirectly, the limit of 2G addressability as discussed in my previous blog entry is overcome. In a sense, it is pretty similar to that of medium model, where a full 64-bit address is explicitly loaded using the "movabsq" instruction and then referenced indirectly.

The difference is PIC code requires the distance between the referencing point and the actual GOT entry to be within the 2G limit, a reference from the lower address of a very large text section to its corresponding GOT entry may become out of reach, leading to another "address does not fit" error.

Moreover, under PIC mode all global statics will be accessed through GOT, leading to a degradation in performance. Whereas under medium model, only objects larger than 65535 bytes will reside in the special ".lXXXX" sections which require 64-bit access.

Hence for application with normal code size and number of global static, using -Kpic will get around the problem if some degradation in performance can be tolerated.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed

Today's Page Hits: 5