Archives
- October 2025
- September 2025
- August 2025
- July 2025
- June 2025
- May 2025
- April 2025
- March 2025
- February 2025
- January 2025
- December 2024
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- June 2021
- May 2021
- April 2021
- March 2021
- February 2021
- January 2021
- December 2020
- November 2020
- October 2020
- September 2020
- August 2020
- July 2020
- June 2020
- May 2020
- April 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- July 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- April 2013
- March 2013
- February 2013
- January 2013
- December 2012
- November 2012
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- August 2010
- July 2010
Complications, Complications
The other day someone asked how hard it would be to modify the Open Watcom linker, wlink, to properly support exports from IOPL segments in OS/2 LX modules. Not terribly hard it turned out, all it needed was to emit a different “bundle type” for exports from IOPL segments. Rather than a regular 32-bit export, it needs to be a special 16-bit call gate export.
A very slight complication is that the 16-bit call gate export is naturally limited to 16-bit offsets, so the linker needs to error out if an attempt is made to export an entry at an offset 65,536 bytes or more into the segment/object. That is easy enough. There are also complications when calling into IOPL segments from the same module, but that’s a separate topic.
When I tried to do rudimentary testing of whether the linker now produces sensible output, I ran into an unexpected problem. For calling into any IOPL segment, in both NE and LX modules, OS/2 uses call gates. The Intel 286/386 call gate mechanism has a provision for the CPU to automatically copy a certain number of parameters (either words or dwords, depending on whether the call gate is 16-bit or 32-bit) when switching stacks. In OS/2, these call gates are always 16-bit, so there are parameter words optionally copied.
The NE and LX executable formats both use the top 5 bits of a flag byte to specify the number of parameter words (those five bits get copied into the CPU-defined call gate descriptor, which also has 5 bits for the parameter count). The Microsoft and IBM linkers (LINK/LINK386), sensibly enough, accept the number of parameter words through the EXPORTS directive.
Now, the Watcom linker already does that too… but things were not adding up. I realized that the Watcom IOPL_WORD_SHIFT macro is not defined as 3 as I expected, but rather as 2. So the number or parameter words in the resulting executable is wrong. Except… hang on a sec.
The wlink documentation is actually pretty clear that the linker takes the number of bytes, not words, to copy—unlike MS/IBM tools. So if you wanted 2 parameter words to be copied, you tell wlink to copy 4 bytes. Instead of emitting 2 << 3 as expected, the linker does 4 << 2 instead, which produces exactly the same result. Except not always because the user can specify, say, 5 parameter bytes, and then a reserved bit will be set in the flag byte.
It gets worse. In the linker documentation one can find the following error message:
MSG 2042: too many IOPL words in EXPORT directive
The maximum number of IOPL words for a 16-bit executable is 63
The error message gives the impression that the EXPORT directive takes words, when the actual directive documentation says bytes. Furthermore, the maximum number of parameter bytes is 63, and even that is wrong—it’s five bits, so the maximum is 31 words, which would be 62 bytes. Clearly people previously working on the compiler were just as confused by this as I was.
That is further evidenced by the fact that internally the linker keeps the size of call gate parameters in a variable named iopl_words, even though the variable actually holds bytes.
Get Me Out of Here!
It would be so much better if wlink accepted the number of parameter words rather than bytes, because that is what the hardware does, therefore that is what the executable format uses, and therefore that is what other tools take as input.
But can I just change wlink to do that? People might be producing 16-bit NE modules with IOPL segments and exports with non-zero parameter word count. If they expect that the linker takes the number of bytes, their code may break in rather nasty ways if the linker behavior suddenly changes.
And therein lies the dilemma: Is the set of such people non-empty? IOPL segments are rather exotic on OS/2, and that’s quite aside from the fact that OS/2 itself is pretty exotic these days. Changing the linker to be more compatible with other tools seems like a good thing, but breaking existing code is not. And determining whether existing code is a concern at all is quite difficult.
Searching the Internet for existing code turned out to be fruitless, but of course that does not prove anything. Finding existing code would have settled the matter, but finding nothing is not meaningful.
MS2WLINK Offers Insight?
Then it occurred to me that perhaps I should check the ms2wlink utility that ships with the Open Watcom compilers. It takes .DEF files intended for Microsoft/IBM linkers and produces command files suitable for wlink.
Checking the ms2wlink source code revealed that it is similarly confused with regard to range checking, but contains this crucial comment:
// convert iopl value to a byte value
The ms2wlink utility expects parameter words on input (same as MS/IBM linkers, as it should) and multiplies the value by two to produce wlink compatible output.
Hmm, so that would be a reason not to change the wlink behavior. Of course I could change both wlink and ms2wlink, but that could lead to quite interesting results when mixing and matching different wlink and ms2wlink versions.
Hitting Paydirt
OK, so search engines got me nowhere, but there is one more thing to check—the Open Watcom source tree itself. And sure enough, I found something. A makefile (\bld\trap\par\os2v2.pio\makefile) with the following, suddenly relevant comment:
Once WLINK properly supports 16-bit IOPL segments in 32-bit DLLs, the port I/O can be built into the parallel trap file/server modules, which is where it belongs in the first place.
I’m almost certain that comment was written by me about 20 years ago, and now I’m adding that support for IOPL segments in 32-bit DLLs. Nearby I found what I’d been looking for:
extra_linker = segment 'port' IOPL export INPORT 2, OUTPORT 4
Sure enough, the INPORT function takes 2 bytes or one word, OUTPORT takes 4 bytes or two words. So, someone does rely on that, and never mind that I completely forgot what I’d written aeons ago.
How Did This Happen?
I couldn’t help but wonder if this word/byte mess was merely an oddity or a bug. After reviewing old Watcom C versions, I’m certain it was a bug. The tools behavior and the usage help printed by wlink has not changed since Watcom C/C++ 10.5 (1995).
However, in Watcom C/C++ 10.0 (1994) and older versions, the wlink online help claimed that the EXPORT directive takes IOPL words, not bytes. The catch is that the linker behavior wasn’t any different. So in version 10.5, the linker usage help was adjusted to match what the linker already did.
The linker documentation in version 10.0 (older versions are harder to check because only hardcopy documentation was provided) quite clearly stated that the EXPORT directive took IOPL words, and incorrectly claimed that the maximum was 63 words. The version 10.5 documentation simply changed ‘words’ to ‘bytes’.
There was also a change in the ms2wlink utility behavior. In Watcom C version 9.0 (1992), ms2wlink did not change the number of IOPL words specified in Microsoft/IBM .def files. Someone must have noticed that this produced incorrect results and in Watcom C version 9.5 (1993), ms2wlink multiplied the IOPL words by two.
The oldest version of IOPL-capable wlink that I have been able to examine is version 7.0 (1991) shipped with Watcom C 8.5. It is no different from the later versions. The only even older wlink version I have is 4.1 from 1988, but that is not capable of generating OS/2 executables.
My best guess is that the Watcom linker was always intended to take IOPL words and not bytes as input, as evidenced by the linker source code as well as pre-1995 documentation. Somehow the original authors messed it up, probably by getting the IOPL_WORD_SHIFT macro wrong. Users noticed that to get the expected result, they must feed the linker the number of bytes rather than words. A few releases down the road, the Watcom developers first fixed the ms2wlink utility to account for the linker behavior, and later made the same decision I did—rather than breaking people’s code, document what the linker actually does.
A curious thing about this bug is that although the linker clearly didn’t do what it was supposed to do, by trivially massaging the linker command input users were able to achieve the desired results. When the error was discovered years later, changing the documentation to account for the buggy behavior was the lesser evil.
Conclusion
Fixing past mistakes is sometimes impractical. I can’t make the wlink behavior more sane without breaking existing code. All things considered, living with the odd syntax seems less bad than blowing other people’s code up (even if “other people” might be me).
In the end I decided to leave the “IOPL byte” syntax the way it was. It’s strange and unfortunately different from other tools, but it actually does work, and it’s been that way for quite a long time.
However, I improved the error checking so that the linker complains if an attempt is made to supply an odd number of parameter bytes, and fixed the range checks to allow up to 31 words or 62 bytes. While I was at it, I also changed the IOPL_WORD_SHIFT macro to do what the name suggests, and changed the input processing in the linker such that the iopl_words variable really holds the count of words and not bytes.
Problem solved, about as well as it could be.
2 Responses to Complications, Complications
Interesting oddities.
May I ask a somewhat related question here?
I was using gcc+emx to write OS/2 software, but mainly to port some unix stuff I was working on in university at the time (between 1993 and 2000). I loved it.
At that time I was a member of Fidonet and wrote a lot of supporting software, and I experimented with the ISDN CAPI which was only available as 16 bit DLL.
Do you know if it would have been possible to construct a 32-bit callback function that is callable from a 16-bit DLL? I managed to invoke all 16-bit functions in the CAPI DLL by “thunking” but I never managed to have the DLL call me back, which stopped me from pursuing that any further since this function was essential for receiving and answering calls.
According to the OS/2 2.0 Application Design Guide, page 3-33, it was doable, at least with the IBM compiler. The catch is that the 16-bit compiler offers absolutely no help in this direction, so the 32-bit compiler needs to do all the work. It ought to be possible to pass the 16-bit code the address of a 32-bit function. Whether that would have worked with emx gcc I don’t know.
I have no doubt that in the worst case, writing some thunking functions in assembly would have been doable. If you recall, OS/2 was unusual in that all memory of a 32-bit process was generally also addressable from 16-bit code because OS/2 restricted the 32-bit process address space. The DosFlatToSel and DosSelToFlat APIs did the 1632 address translation.
This site uses Akismet to reduce spam. Learn how your comment data is processed.