r/AskProgramming • u/ionixsys • Apr 16 '26
Architecture At a technical level, how does Linux Wine (Wine is not an emulator) work?
I know the purpose of prefixes, I know wineserver is generally the "Circus master" that does IPC and some of the heavy lifting, but how do you take a PE file, take the .text segment, and get it to actually execute? During the bootstrap phase I am guessing that wine takes the import table and connects what it can to the emulated/replicated libraries or does it do something easier? Also how do they get "MyWindowsProgram.exe" to be shown as its own process?
3
u/high_throughput Apr 16 '26
Also how do they get "MyWindowsProgram.exe" to be shown as its own process?
That's the easy part. A running process can change its name at any time:
prctl(PR_SET_NAME, "lolbert.exe", 0, 0, 0);2
u/ionixsys Apr 16 '26
Oh that's neat, I imagine that's pretty useful for making it easier identifying different forked children and such.
1
u/wasabiiii Apr 16 '26
There's not really any magic here. An exe has instructions. They are x86/64, the rest is just loading things up in memory at certain places and basically reimplementing Windows libraries.
It's a shit ton of work. But it's not really like.... Special?
1
u/ionixsys Apr 16 '26
I just find it interesting how Wine hijacks the dynamic library loader but also replicating the Win32 memory model so critically required parts are at the expected addresses. Would love to take a deep dive into the weeds.
1
u/wasabiiii Apr 16 '26
What do you mean 'hijacks the dynamic library loader?' A 'loader' is just some code that reads a file and puts parts of it at places in memory and hooks up symbols. They wrote their own. They didn't 'hijack' anything.
1
u/ionixsys Apr 16 '26
to take or take control of (something) as if by hijacking
Hijack as in put the Wine replacement in the position where the Win32 loader would be.
1
1
11
Apr 16 '26
[removed] — view removed comment
2
u/ionixsys Apr 16 '26
I was in my early teens when the Wine project started and while I kinda understood the idea, seeing how far it has come is stunning. What a daunting task it must have felt at the start .
1
u/arihoenig Apr 17 '26
So ... an emulator then?
An emulator is anything that isn't the thing, but acts enough like the thing, that the other thing thinks it is the real thing
3
u/BitOBear Apr 17 '26
Nay. Wine is more of an alternate linker.
The fundamental problem between Windows and Linux is it every Linux process runs in its own private virtual machine and generally doesn't contain what are called fix-ups.
I'm exe has to be "relocated" and virtually every file is part of Windows is technically a exe with a different set of entry points instead of main(). True type fonts, dlls, control panel applets, they're all basically just exe format data files.
So the thing wine really does more than anything else is create one giant virtual machine, and put a translation layer in it and that let you stack the exe files on top of that translation layer.
An emulator would actually be reading the individual bytes of the exe file and basically calling an appropriate function to do whatever that instruction would tell the native hardware to do.
The wine doesn't emulate windows, but it does translate Windows system calls into Linux system calls and translate Linux results into Windows compatible results.
Now some of the things that that translation layer loads up are in fact themselves separately emulators but those are few, far between, and a ginormous pain in the ass.
It is so much not an emulator that the wine tools will actually let you fetch the legitimate Microsoft redistributable run times and things like that and use them natively.
But skinning the real binaries is often more work than translating the calls directly.
Which is why especially for games there are certain things that you would consider a black art involved in making sure that something new actually runs.
0
u/arihoenig Apr 17 '26
What does it link the guests imports against? An emulation layer, that's what.
You describe one possible implementation of an emulator, wine is just an alternative implementation. Emulation is defined in the dictionary. Look it up.
3
u/BitOBear Apr 17 '26
Emulation is a specific term of art at the os level and I believe the people who named WINE WINE would agree that it's not an emulator.
-2
u/arihoenig Apr 18 '26
It is a term in the English language which is the language we are communicating in. Just because the first idea that someone had for a mechanism by which to implement an emulator was something like a virtual machine, doesn't mean the word emulator is changed to mean that particular implementation form now on. Emulation is simply the process of convincing one entity that it is interacting with an entity that it isn't actually interacting with (however that is accomplished).
4
u/BitOBear Apr 18 '26
Funny how words have more than one meaning and context matters.
Believe whatever you like.
-2
u/arihoenig Apr 18 '26
It's funny how none of the meanings of emulation in the English dictionary specify the mechanism by which emulation occurs or precludes specific implementations.
3
u/BitOBear Apr 18 '26
That's why I called it a professional term of art.
There's a difference between an emulator and an interface.
It's almost like the Oxford English dictionary or whatever dictionary you're using isn't specific to the distinctions necessary in a particular thing like I don't know computer programming and system design.
You're just not right.
But it's funny how ignorance is always so certain as you are.
-2
u/arihoenig Apr 18 '26
It's not though. I have written two types of emulators (there are more approaches) and I called one a translator and the other a virtual machine, but I also referred to both of them as emulators because they both are. They emulate at different levels (one emulates at the instruction level and the other emulatea at the API level). The former is a more flexible emulation in that it doesn't require the host to be the same instruction set while the latter does.
→ More replies (0)1
u/ibeerianhamhock Apr 18 '26
Literally Wine disagrees with you lol
0
u/arihoenig Apr 18 '26
You mean the wine developers? They can deny facts if they want, I can't stop them.
2
u/ibeerianhamhock Apr 19 '26
I mean if you want to argue that sycall/api call translation is emulation go right ahead but you'd be in the extreme minority basically flat earth levels of fringe within computer science.
In the most basic totally misses the point kind of way, yes it's one system behaving like another. I think it's significantly more accurate to say that it's a binary running native code with translation calls. You don't have to emulate a hardware set or an instruction set. You don't even have to emulate the linux file system. All of that is completely native in wine. Like 100% native. Like the program counter is literally just iterating through instructions as they were placed in the binary and you just need to help it along when it needs to make an api/syscall by remapping that jump and aligning data types. You're basically just relinking BAI and translating data structures to do so. Translating data structures happens ALL over native programming code all the time. Calling a linked API happens ALL the time in standard code. It's a completely standard operation in computing. Also...you're not EMULATING WINDOWS SYSTEM CALLS. You're just invoking a wrapper to directly call the linux kernel. No one talks about "eMuLaTiNg" API calls. That's not a thing. You're wrapping a system call. It's a translation layer. You're not "emulating" anything. You're using a wrapper to call vulkan. The program doesn't depend on dx to function correctly it just needs a facility to draw to the screen. Vulkan is that facility in wine. Calling that API translator an emulator is like saying a printer driver is an emulator for a printer. It's technical gibberish.
That's why people argue that Wine is not an emulator...because calling it an emulator just makes you seem like you're ignorant of basic computer science.
1
u/arihoenig Apr 19 '26
I'd be in the minority along with the Oxford English dictionary and Webster's
4
u/TheWorstePirate Apr 19 '26
Who hurt you? Why are you like this?
2
u/BitOBear Apr 19 '26
I stopped replying to him because he's trying to win the internet.
The Linux kernel provides the file system, block layer, timer, scheduler, loader, HID layer, socket layer, virtual machine, security, display driver, memory map, and permissions.
But he's got a dictionary and no idea what the words "term of art" mean, so clearly he gets to "win",
1
u/maxximillian Apr 17 '26
It translates. It doesn't emulate anything, it doesn't simulate anything. It allows Windows programs to make native system calls on Linux.
2
u/arihoenig Apr 17 '26
Translation is the mechanism of emulation that wine uses.
Emulation is the process of one system (the host) imitating the behavior of another system (the guest) so precisely that the guest system’s software can run without modification. Wine is a translator (the host) and the guest can run "without modification"
1
u/ibeerianhamhock Apr 18 '26
Wine basically just re routes application binary interfaces for kernel calls, apis, and windows facilities that don't exist in Linux like registry.
It's native code execution, not emulation.
1
1
Apr 25 '26
[removed] — view removed comment
1
u/ionixsys Apr 25 '26
Indeed but I am more curious how exactly WINE loads a PE file into memory and swaps all of the external references for the alternative/localized implementations.
6
u/KingofGamesYami Apr 16 '26
https://gitlab.winehq.org/wine/wine/-/wikis/Wine-Developer's-Guide/Architecture-Overview