0

A windows application I've been working on, is due to delivred without the use of CRT. There's a few good articles descirbing the guidlines and benefits of doing so (none of which I could find upon writing this question - sorry) . Other then not using CRT symbols explicitly, it's all about linker and compailer configuration. My Release and Debug configurations are nearly identical now , and both specify /NODEFAULTLIB:"LIBCMT" only diffrence is the Run time flag /MT (in Release) vs. /MTd (in Debug) - that is required as I'm using __try/__except syntax for SEH, and so the only needed function is _except_handler3(). However, the Debug version is beeing built successfully, and the Release version fail with Unresolved Symbol _except_handler3 linkage error. When forcing Release version to compile with /MTd - this issue is solved.

How is that possible? which .lib contains the _except_handler3 export? and how can I solve this?

  • downvoter - PLEASE EXPLAIN YOURSELF – Paul M. Adams Feb 20 '14 at 20:24
  • Though I'm not the one who downvoted, am I right you use `/NODEFAULTLIB:"LIBCMT"` with `/MTd`? – qwm Feb 20 '14 at 20:40
  • @qwn yes. The symbols of CRT are located on a several .lib files, one of which is the libcmt.lib. However, the desired _except_handler3() is not located at that file. I've specified `/MT` to let the linker find it, and avoid the greater portion of the CRT which is indeed located at `libcmt.lib`. – Paul M. Adams Feb 20 '14 at 21:29
  • `_except_handler3` is indeed located in `libcmt.lib`. And your question can be renamed to 'Why, oh, why I don't know about `libcmtd`?'. – qwm Feb 20 '14 at 21:33
  • @qwm ok the joke is on me . – Paul M. Adams Feb 20 '14 at 21:46
  • Well, at least you have a sense of humor -) – qwm Feb 20 '14 at 21:53
  • @qwm If I'll paste _except_handler3 from CRT source code , I'll safe right? (SEH- wise I mean) – Paul M. Adams Feb 20 '14 at 22:05
  • @qwm alternatively, how can I instruct the linker to link only this specific function? – Paul M. Adams Feb 20 '14 at 22:10
  • You will have to paste all its dependencies too. You will ultimately end up doing all the work linker makes for free. Linker doesn't link in `obj`s from `lib`s that has not been referenced, so if you want only this function (+dependencies) you just have to use none of the other `libcmt`'s symbols – qwm Feb 20 '14 at 22:13
  • Alternatively, you either can use third-party `_except_handler3` implementation (there are some on the web), or implement it yourself (it will cost you a looooooot of time). – qwm Feb 20 '14 at 22:17
  • @qwm much appreciation. one last thing, is there any way to verify (for this case and others) which symbols (of CRT) where eventually linked to the final binary? – Paul M. Adams Feb 20 '14 at 22:26
  • compile with `-Zi` or link with `-debug` and then either parse `pdb` or use some tool (I would use `IDA`, though). Or what did you mean by 'eventually'? I can predict that `_except_handler3` will get pretty much code linked into binary 'cause EH is quite heavy-weighted mechanism. – qwm Feb 20 '14 at 22:30
  • Hmmm... I just linked small testing program with EH and some winapi calls, and it's just 7.5kb (<0x0e00 bytes of code, ~0x600 of data). I thought it would be about 20kb – qwm Feb 20 '14 at 22:44
  • @qwm exacly what you thought. thanks! – Paul M. Adams Feb 20 '14 at 22:57
  • Feel free to ask, but I will answer tomorrow, it's 3am here. – qwm Feb 20 '14 at 23:00
  • @qwm perhaps later I'll go to reimplementing direction, as I don't really use SEH at it's most, but rather to wrap my entry point making sure I'll catch any exception and exit. – Paul M. Adams Feb 20 '14 at 23:01
  • The most straightforward way for doing this is installing your handler in TEB SEH-frames list. Do not use __try __except at all. – qwm Feb 20 '14 at 23:21
  • @qwm you mean "mov FS:[0] , X" style? thats what I've done before, but it failed once (an exception popped a message somehow) – Paul M. Adams Feb 20 '14 at 23:36
  • Well... it's your fault somehow. `__try __except` uses the very same mechanism – qwm Feb 21 '14 at 06:06
  • @qwm maybe somehow removed by compiler optimization? – Paul M. Adams Feb 23 '14 at 13:44

1 Answers1

1

For an application without CRT see: http://blog.kalmbach-software.de/2008/02/02/smallest-application-size-for-win32-console-application/

But if you want to use __try / __except you need to use the CRT; or you need to write your own "_except_handler3".

See also: _except_handler3

Also you have the CRT source, and you can take a look into the source, to see what it is doing! See: C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\crt\src

Jochen Kalmbach
  • 3,549
  • 17
  • 18