Clang is using the wrong memory model

Mark Geisert mark@maxrnd.com
Fri Aug 16 08:25:00 GMT 2019


Hi, please don't start a new Subject: by replying on another thread.  It fouls 
up mail threading for folks trying to follow multiple threads.  Just start a new 
thread with your new Subject:.  Thanks.

Agner Fog wrote:
> Cygwin Clang is using -mcmodel=medium as default for Win64, according to my 
> tests, while the right model is -mcmodel=small
> 
> Linux Clang with --target=x86_64-pc-cygwin gives the small memory model.
> 
> I took this to the LLVM Bugzilla as you asked me to: 
> https://bugs.llvm.org/show_bug.cgi?id=42983

Disclaimer: I am not a Cygwin maintainer or the Cygwin Clang maintainer.  This 
is a general Cygwin users' mailing list, not a private support channel.  I'm 
just some guy on the list, albeit somebody interested in solving your issue.

Cygwin is not Windows.  Cygwin's shared libraries are more than 32 bits distant 
from executables you build.  -mcmodel=small won't work.  There may be a better 
-mcmodel setting, TBD, but small isn't it, unfortunately.

The reasons are longstanding and technical.  The quick version is that there are 
thousands of different DLLs that a Cygwin user might want installed on their 
system.  Cygwin is a Posix environment, and to support fork() the address space 
for those DLLs is chosen to avoid Windows interference, so much as possible.

> This gave the following conclusion:
> 
> -mcmodel=small does something different when the target is Windows. This 
> difference appears to be undocumented. The small memory model with a Linux 
> target puts everything below the 2GB limit so that 32-bit absolute addresses can 
> be used. The small memory model with a Windows target is using 32-bit relative 
> addresses instead, which is the correct thing to do in Windows.
> 
> I told the LLVM guy that this difference needs to be documented so that you can 
> rely on it, but this request has so far been ignored.
> 
> He says that you must have modified the source code to change the default memory 
> model.

Now that I've downloaded and unpacked the Clang source tree, I can confirm we 
have a patch to set -mcmodel=medium on 64-bit Cygwin; it's left at small for 
32-bit Cygwin.

> The medium memory model gives inefficient code because it uses an extra 
> instruction to load a 64-bit absolute address into a register before every 
> access to static data.

My understanding is that Clang makes an attempt to accept all the same options 
as gcc.  Could you please try your Clang build with option "-mcmodel=small"?  I 
suspect the link phase won't work because the Clang runtime library is more than 
32 bits away from your executable's code.  You might try another -mcmodel 
setting.  I don't know what all of them are.

First step is just getting the thing to link.  Then testing whether it works 
properly.  Then code efficiency :-).

> I cannot blame you Cygwin people for not using the small memory model as long as 
> it is undocumented.

The necessary reason for our patch was explained above.

> I will ask you to please join the discussion at 
> https://bugs.llvm.org/show_bug.cgi?id=42983 so that we can clarify how to solve 
> this problem.

This issue does now appear to be an issue with Cygwin's Clang build, so let's 
keep discussing here.  We can sync up with the LLVM bugtracker later on.

BTW is it a goal to be building for Cygwin?  Or is it that you want 
Windows-native executables built with Cygwin tools?  If the latter, there are 
cross-compilers provided by Cygwin that can do that.  I don't think there's a 
Clang for that situation yet, though.

..mark

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list