This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt] Use macros to simplify weak alias for Windows and add some documentation.
ClosedPublic

Authored by mpividori on Jan 10 2017, 11:56 AM.

Details

Summary

I create some useful macros for dealing with pragma directives in Windows.
Also, after some experience with "alternatename" pragma, I decided to add some information for future users.
Thanks.

Diff Detail

Repository
rL LLVM

Event Timeline

mpividori updated this revision to Diff 83841.Jan 10 2017, 11:56 AM
mpividori retitled this revision from to [compiler-rt] Use macros to simplify weak alias for Windows and add some documentation..
mpividori updated this object.
mpividori added reviewers: kcc, rnk, aizatsky, zturner.
mpividori set the repository for this revision to rL LLVM.
mpividori added a subscriber: llvm-commits.
kubamracek added inline comments.Jan 10 2017, 12:11 PM
lib/ubsan/ubsan_flags.cc
20 ↗(On Diff #83841)

I think this needs to be included only on Windows, because sanitizer_win_defs.h errors out if _MCS_VER is not defined.

rnk edited edge metadata.Jan 10 2017, 1:00 PM

Thanks for the refactoring! I'd been thinking of doing it for a while.

lib/asan/asan_globals_win.h
1 ↗(On Diff #83841)

I think with your change, this header is no longer needed. We can replace all instances of ASAN_LINK_GLOBALS_WIN() with WIN_FORCE_LINK(__asan_dso_reg_hook).

lib/sanitizer_common/sanitizer_win_defs.h
17 ↗(On Diff #83841)

The existing pattern in sanitizer_linux.h and sanitizer_mac.h is to allow the header to be included everywhere but ifdef out the contents. I think this reduced ifdefs and is in general consistent with how Kostya likes to do things in the sanitizer libraries. Let's do that. So, please change this to #if SANITIZER_WINDOWS.

lib/ubsan/ubsan_flags.cc
20 ↗(On Diff #83841)

Let's fix the header to make it easy to include instead.

@kubabrecka , you are right. Sorry, I was going to test on linux before submitting the commit to svn.

@rnk Ok. Yes, I agree.

mpividori updated this revision to Diff 83867.Jan 10 2017, 2:04 PM
mpividori edited edge metadata.

Done.

rnk accepted this revision.Jan 10 2017, 2:23 PM
rnk edited edge metadata.

lgtm

This revision is now accepted and ready to land.Jan 10 2017, 2:23 PM

Hi, also I think we could create a general macro, like:

#if SANITIZER_WINDOWS
#define WEAK_ALIAS(Name, Default)  \
  __pragma(comment(linker, "/alternatename:" #Name "=" #Default))
#else
#define STRINGIFY(A) #A
#define WEAK_ALIAS(Name, Default) \
  _Pragma( STRINGIFY(weak Name =  Default) )
#endif

And always use that macro when need to support Windows. For example:

int some_fun_def() {
   .. // Default impl.
}
WEAK_ALIAS(some_fun, some_fun_def)

This definitely simplifies the code, but has the disadvantage that we always need to define a default function, which is not strictly necessary for weak functions in non-windows systems, and as they have the same visibility, we would also be exporting the default functions (unless we find some way to avoid this).

Another option is to define another macro:

#if SANITIZER_WINDOWS
#define WEAK(ReturnType, Function) \ 
   WEAK_ALIAS(Function, Function##_def)
   ReturnType Function##_def
#else
#define WEAK(function) \ 
   ReturnType __attribute__((weak)) Function
#endif

Then we can use:

WEAK(int, some_fun) {
   .. // Default impl.
}

What do you think?
Thanks.

rnk added a comment.Jan 10 2017, 2:48 PM

Defining a cross-platform weak mechanism seems like a great idea, even if we can only use it when we have default implementations. It greatly simplify our Windows porting efforts. Adding new weak functions is one of the more common ways to break the WinASan build.

rnk added a comment.Jan 10 2017, 2:49 PM

... but this is a great incremental improvement, so let's commit this first.

mpividori updated this revision to Diff 84020.Jan 11 2017, 1:58 PM
mpividori edited edge metadata.
mpividori removed rL LLVM as the repository for this revision.

Hi, I updated the diff to use the macro STRINGIFY() instead of directly # , so we ensure we expand the parameters before stringifying.

Ready to land?

rnk added a comment.Jan 11 2017, 4:32 PM

Yep, looks good.

mpividori updated this revision to Diff 85166.Jan 20 2017, 11:33 AM
This revision was automatically updated to reflect the committed changes.