Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit fc7a8fb

Browse files
franksinankayajanvorli
authored andcommitted
Coreclr gnuport (#22129)
* Abstract away NOINLINE statement MSVC and GNU compilers use different attributes for noinline. Abstract away compiler differences. * Replace __sync_swap with __atomic_exchange_n __sync_swap doesn't exist on GNU. Replacing with __atomic_exchange_n which is universally available. * Define CDECL for GNUC __cdecl is not defined by default on GNU compilers. * Define gcc version of __declspec(thread) * Correct pointer casting A pointer value is usually unsigned long on most platforms. Casting it to integer causes signedness issues. Use size_t to be efficient on all 32 and 64 bit architectures. * Put quotes around the error string Correct error statement. GNU G++ is picky about the string following the error statement with ' character in it. It needs to be enclosed with double quotes. * Fix casting problem Seeing these warnings with GNU G++ compiler src/pal/src/sync/cs.cpp: In function ‘void CorUnix::InternalInitializeCriticalSectionAndSpinCount(PCRITICAL_SECTION, DWORD, bool)’: src/pal/src/sync/cs.cpp:630:48: warning: converting to non-pointer type ‘SIZE_T {aka long unsigned int}’ from NULL [-Wconversion-null] pPalCriticalSection->OwningThread = NULL; ^ src/pal/src/sync/cs.cpp: In function ‘void CorUnix::InternalLeaveCriticalSection(CorUnix::CPalThread*, _CRITICAL_SECTION*)’: src/pal/src/sync/cs.cpp:880:43: warning: converting to non-pointer type ‘SIZE_T {aka long unsigned int}’ from NULL [-Wconversion-null] pPalCriticalSection->OwningThread = NULL; ^ * Abstract optnone compiler attribute GNU compiler doesn't support optnone attribute. pal/src/exception/seh-unwind.cpp:449:77: warning: ‘optnone’ attribute directive ignored [-Wattributes] * Set the aligned attribute for GNU compiler * Make __rotl and __rotr functions portable GNU compiler doesn't have an intrinsic for these. Open code them using the provided implementation. * Define deprecated attribute for gcc * Add throw specifier for GCC /usr/include/string.h:43:28: error: declaration of ‘void* memcpy(void*, const void*, size_t) throw ()’ has a different exception specifier size_t __n) __THROW __nonnull ((1, 2));
1 parent a8a05e8 commit fc7a8fb

File tree

15 files changed

+89
-44
lines changed

15 files changed

+89
-44
lines changed

src/inc/palclr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
#define _DEBUG_IMPL 1
3232
#endif
3333

34+
#if __GNUC__
35+
#ifndef __cdecl
36+
#define __cdecl __attribute__((__cdecl__))
37+
#endif
38+
#endif
39+
3440
//
3541
// CPP_ASSERT() can be used within a class definition, to perform a
3642
// compile-time assertion involving private names within the class.

src/inc/staticcontract.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
#define SCAN_WIDEN2(x) L ## x
1616
#define SCAN_WIDEN(x) SCAN_WIDEN2(x)
1717

18+
#ifndef NOINLINE
19+
#if __GNUC__
20+
#define NOINLINE __attribute__((noinline))
21+
#else
22+
#define NOINLINE __declspec(noinline)
23+
#endif
24+
#endif
25+
1826
//
1927
// PDB annotations for the static contract analysis tool. These are seperated
2028
// from Contract.h to allow their inclusion in any part of the system.
@@ -211,7 +219,7 @@ namespace StaticContract
211219
{
212220
struct ScanThrowMarkerStandard
213221
{
214-
__declspec(noinline) ScanThrowMarkerStandard()
222+
NOINLINE ScanThrowMarkerStandard()
215223
{
216224
METHOD_CANNOT_BE_FOLDED_DEBUG;
217225
STATIC_CONTRACT_THROWS;
@@ -225,7 +233,7 @@ namespace StaticContract
225233

226234
struct ScanThrowMarkerTerminal
227235
{
228-
__declspec(noinline) ScanThrowMarkerTerminal()
236+
NOINLINE ScanThrowMarkerTerminal()
229237
{
230238
METHOD_CANNOT_BE_FOLDED_DEBUG;
231239
}
@@ -237,7 +245,7 @@ namespace StaticContract
237245

238246
struct ScanThrowMarkerIgnore
239247
{
240-
__declspec(noinline) ScanThrowMarkerIgnore()
248+
NOINLINE ScanThrowMarkerIgnore()
241249
{
242250
METHOD_CANNOT_BE_FOLDED_DEBUG;
243251
}
@@ -283,21 +291,21 @@ template <UINT COUNT>
283291
class BlockMarker
284292
{
285293
public:
286-
__declspec(noinline) void MarkBlock()
294+
NOINLINE void MarkBlock()
287295
{
288296
ANNOTATION_MARK_BLOCK_ANNOTATION;
289297
METHOD_CANNOT_BE_FOLDED_DEBUG;
290298
return;
291299
}
292300

293-
__declspec(noinline) void UseMarkedBlockAnnotation()
301+
NOINLINE void UseMarkedBlockAnnotation()
294302
{
295303
ANNOTATION_USE_BLOCK_ANNOTATION;
296304
METHOD_CANNOT_BE_FOLDED_DEBUG;
297305
return;
298306
}
299307

300-
__declspec(noinline) void EndUseMarkedBlockAnnotation()
308+
NOINLINE void EndUseMarkedBlockAnnotation()
301309
{
302310
ANNOTATION_END_USE_BLOCK_ANNOTATION;
303311
METHOD_CANNOT_BE_FOLDED_DEBUG;

src/pal/inc/mbusafecrt.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ typedef int errno_t;
3131
// define the return value for success
3232
#define SAFECRT_SUCCESS 0
3333

34+
#if defined(_MSC_VER) || defined(__llvm__)
35+
#define THROW_DECL
36+
#else
37+
#define THROW_DECL throw()
38+
#endif
39+
3440
#ifdef __cplusplus
3541
extern "C" {
3642
#endif
@@ -98,7 +104,7 @@ extern int swscanf_s( const WCHAR *string, const WCHAR *format, ... );
98104
extern int _snscanf_s( const char *string, size_t count, const char *format, ... );
99105
extern int _snwscanf_s( const WCHAR *string, size_t count, const WCHAR *format, ... );
100106

101-
extern errno_t memcpy_s( void * dst, size_t sizeInBytes, const void * src, size_t count );
107+
extern errno_t memcpy_s( void * dst, size_t sizeInBytes, const void * src, size_t count ) THROW_DECL;
102108
extern errno_t memmove_s( void * dst, size_t sizeInBytes, const void * src, size_t count );
103109

104110
#ifdef __cplusplus

src/pal/inc/pal.h

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ typedef void * NATIVE_LIBRARY_HANDLE;
143143
#define LANG_THAI 0x1e
144144

145145
/******************* Compiler-specific glue *******************************/
146+
#if defined(_MSC_VER) || defined(__llvm__)
147+
#define THROW_DECL
148+
#else
149+
#define THROW_DECL throw()
150+
#endif
146151

147152
#ifndef _MSC_VER
148153
#if defined(CORECLR)
@@ -156,7 +161,7 @@ typedef void * NATIVE_LIBRARY_HANDLE;
156161
#if defined(_MSC_VER) || defined(__llvm__)
157162
#define DECLSPEC_ALIGN(x) __declspec(align(x))
158163
#else
159-
#define DECLSPEC_ALIGN(x)
164+
#define DECLSPEC_ALIGN(x) __attribute__ ((aligned(x)))
160165
#endif
161166

162167
#define DECLSPEC_NORETURN PAL_NORETURN
@@ -176,6 +181,14 @@ typedef void * NATIVE_LIBRARY_HANDLE;
176181
#endif
177182
#endif
178183

184+
#ifndef NOOPT_ATTRIBUTE
185+
#if defined(__llvm__)
186+
#define NOOPT_ATTRIBUTE optnone
187+
#else
188+
#define NOOPT_ATTRIBUTE optimize("O0")
189+
#endif
190+
#endif
191+
179192
#ifndef PAL_STDCPP_COMPAT
180193

181194
#if __GNUC__
@@ -3503,7 +3516,7 @@ InterlockedExchange(
35033516
IN OUT LONG volatile *Target,
35043517
IN LONG Value)
35053518
{
3506-
LONG result = __sync_swap(Target, Value);
3519+
LONG result = __atomic_exchange_n(Target, Value, __ATOMIC_ACQ_REL);
35073520
PAL_ArmInterlockedOperationBarrier();
35083521
return result;
35093522
}
@@ -3517,7 +3530,7 @@ InterlockedExchange64(
35173530
IN OUT LONGLONG volatile *Target,
35183531
IN LONGLONG Value)
35193532
{
3520-
LONGLONG result = __sync_swap(Target, Value);
3533+
LONGLONG result = __atomic_exchange_n(Target, Value, __ATOMIC_ACQ_REL);
35213534
PAL_ArmInterlockedOperationBarrier();
35223535
return result;
35233536
}
@@ -4316,7 +4329,7 @@ PALIMPORT int __cdecl memcmp(const void *, const void *, size_t);
43164329
PALIMPORT void * __cdecl memset(void *, int, size_t);
43174330
PALIMPORT void * __cdecl memmove(void *, const void *, size_t);
43184331
PALIMPORT void * __cdecl memchr(const void *, int, size_t);
4319-
PALIMPORT long long int __cdecl atoll(const char *);
4332+
PALIMPORT long long int __cdecl atoll(const char *) THROW_DECL;
43204333
PALIMPORT size_t __cdecl strlen(const char *);
43214334
PALIMPORT int __cdecl strcmp(const char*, const char *);
43224335
PALIMPORT int __cdecl strncmp(const char*, const char *, size_t);
@@ -4355,7 +4368,7 @@ PALIMPORT int __cdecl toupper(int);
43554368
#define _TRUNCATE ((size_t)-1)
43564369
#endif
43574370

4358-
PALIMPORT errno_t __cdecl memcpy_s(void *, size_t, const void *, size_t);
4371+
PALIMPORT errno_t __cdecl memcpy_s(void *, size_t, const void *, size_t) THROW_DECL;
43594372
PALIMPORT errno_t __cdecl memmove_s(void *, size_t, const void *, size_t);
43604373
PALIMPORT char * __cdecl _strlwr(char *);
43614374
PALIMPORT int __cdecl _stricmp(const char *, const char *);
@@ -4426,7 +4439,15 @@ inline WCHAR *PAL_wcsstr(WCHAR *_S, const WCHAR *_P)
44264439
}
44274440
#endif
44284441

4429-
#if !__has_builtin(_rotl)
4442+
#if defined(__llvm__)
4443+
#define HAS_ROTL __has_builtin(_rotl)
4444+
#define HAS_ROTR __has_builtin(_rotr)
4445+
#else
4446+
#define HAS_ROTL 0
4447+
#define HAS_ROTR 0
4448+
#endif
4449+
4450+
#if !HAS_ROTL
44304451
/*++
44314452
Function:
44324453
_rotl
@@ -4444,14 +4465,14 @@ unsigned int __cdecl _rotl(unsigned int value, int shift)
44444465
retval = (value << shift) | (value >> (sizeof(int) * CHAR_BIT - shift));
44454466
return retval;
44464467
}
4447-
#endif // !__has_builtin(_rotl)
4468+
#endif // !HAS_ROTL
44484469

44494470
// On 64 bit unix, make the long an int.
44504471
#ifdef BIT64
44514472
#define _lrotl _rotl
44524473
#endif // BIT64
44534474

4454-
#if !__has_builtin(_rotr)
4475+
#if !HAS_ROTR
44554476

44564477
/*++
44574478
Function:
@@ -4471,7 +4492,7 @@ unsigned int __cdecl _rotr(unsigned int value, int shift)
44714492
return retval;
44724493
}
44734494

4474-
#endif // !__has_builtin(_rotr)
4495+
#endif // !HAS_ROTR
44754496

44764497
PALIMPORT int __cdecl abs(int);
44774498
// clang complains if this is declared with __int64
@@ -4487,10 +4508,10 @@ PALIMPORT double __cdecl acos(double);
44874508
PALIMPORT double __cdecl acosh(double);
44884509
PALIMPORT double __cdecl asin(double);
44894510
PALIMPORT double __cdecl asinh(double);
4490-
PALIMPORT double __cdecl atan(double);
4491-
PALIMPORT double __cdecl atanh(double);
4511+
PALIMPORT double __cdecl atan(double) THROW_DECL;
4512+
PALIMPORT double __cdecl atanh(double) THROW_DECL;
44924513
PALIMPORT double __cdecl atan2(double, double);
4493-
PALIMPORT double __cdecl cbrt(double);
4514+
PALIMPORT double __cdecl cbrt(double) THROW_DECL;
44944515
PALIMPORT double __cdecl ceil(double);
44954516
PALIMPORT double __cdecl cos(double);
44964517
PALIMPORT double __cdecl cosh(double);
@@ -4520,10 +4541,10 @@ PALIMPORT float __cdecl acosf(float);
45204541
PALIMPORT float __cdecl acoshf(float);
45214542
PALIMPORT float __cdecl asinf(float);
45224543
PALIMPORT float __cdecl asinhf(float);
4523-
PALIMPORT float __cdecl atanf(float);
4524-
PALIMPORT float __cdecl atanhf(float);
4544+
PALIMPORT float __cdecl atanf(float) THROW_DECL;
4545+
PALIMPORT float __cdecl atanhf(float) THROW_DECL;
45254546
PALIMPORT float __cdecl atan2f(float, float);
4526-
PALIMPORT float __cdecl cbrtf(float);
4547+
PALIMPORT float __cdecl cbrtf(float) THROW_DECL;
45274548
PALIMPORT float __cdecl ceilf(float);
45284549
PALIMPORT float __cdecl cosf(float);
45294550
PALIMPORT float __cdecl coshf(float);

src/pal/inc/rt/safecrt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ typedef _W64 unsigned int uintptr_t;
135135
#define _UINTPTR_T_DEFINED
136136
#endif
137137

138+
#ifdef __GNUC__
139+
#define SAFECRT_DEPRECATED __attribute__((deprecated))
140+
#else
138141
#define SAFECRT_DEPRECATED __declspec(deprecated)
142+
#endif
139143

140144
/* errno_t */
141145
#if !defined(_ERRCODE_DEFINED)

src/pal/src/exception/seh-unwind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ PAL_FreeExceptionRecords(IN EXCEPTION_RECORD *exceptionRecord, IN CONTEXT *conte
444444
--*/
445445
PAL_NORETURN
446446
__attribute__((noinline))
447-
__attribute__((optnone))
447+
__attribute__((NOOPT_ATTRIBUTE))
448448
static void
449449
RtlpRaiseException(EXCEPTION_RECORD *ExceptionRecord, CONTEXT *ContextRecord)
450450
{

src/pal/src/exception/seh.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,12 @@ bool CatchHardwareExceptionHolder::IsEnabled()
394394
395395
--*/
396396

397-
#ifdef __llvm__
398-
__thread
399-
#else // __llvm__
400-
__declspec(thread)
401-
#endif // !__llvm__
402-
static NativeExceptionHolderBase *t_nativeExceptionHolderHead = nullptr;
397+
#if defined(__GNUC__)
398+
static __thread
399+
#else // __GNUC__
400+
__declspec(thread) static
401+
#endif // !__GNUC__
402+
NativeExceptionHolderBase *t_nativeExceptionHolderHead = nullptr;
403403

404404
extern "C"
405405
NativeExceptionHolderBase **

src/pal/src/include/pal/context.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ inline static DWORD64 CONTEXTGetPC(LPCONTEXT pContext)
486486
#elif defined(_ARM64_) || defined(_ARM_)
487487
return pContext->Pc;
488488
#else
489-
#error don't know how to get the program counter for this architecture
489+
#error "don't know how to get the program counter for this architecture"
490490
#endif
491491
}
492492

@@ -499,7 +499,7 @@ inline static void CONTEXTSetPC(LPCONTEXT pContext, DWORD64 pc)
499499
#elif defined(_ARM64_) || defined(_ARM_)
500500
pContext->Pc = pc;
501501
#else
502-
#error don't know how to set the program counter for this architecture
502+
#error "don't know how to set the program counter for this architecture"
503503
#endif
504504
}
505505

@@ -514,7 +514,7 @@ inline static DWORD64 CONTEXTGetFP(LPCONTEXT pContext)
514514
#elif defined(_ARM64_)
515515
return pContext->Fp;
516516
#else
517-
#error don't know how to get the frame pointer for this architecture
517+
#error "don't know how to get the frame pointer for this architecture"
518518
#endif
519519
}
520520

src/pal/src/init/pal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ PAL_SetInitializeDLLFlags(
239239
real life scenarios work.
240240
241241
--*/
242-
__attribute__((noinline,optnone))
242+
__attribute__((noinline,NOOPT_ATTRIBUTE))
243243
void
244244
EnsureStackSize(SIZE_T stackSize)
245245
{

src/pal/src/locale/utf8.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ class UTF8Encoding
14671467
}
14681468

14691469
// get pSrc 2-byte aligned
1470-
if (((int)pSrc & 0x1) != 0) {
1470+
if (((size_t)pSrc & 0x1) != 0) {
14711471
ch = *pSrc;
14721472
pSrc++;
14731473
if (ch > 0x7F) {
@@ -1476,7 +1476,7 @@ class UTF8Encoding
14761476
}
14771477

14781478
// get pSrc 4-byte aligned
1479-
if (((int)pSrc & 0x2) != 0) {
1479+
if (((size_t)pSrc & 0x2) != 0) {
14801480
ch = *(USHORT*)pSrc;
14811481
if ((ch & 0x8080) != 0) {
14821482
goto LongCodeWithMask16;
@@ -1906,7 +1906,7 @@ class UTF8Encoding
19061906
pTarget++;
19071907

19081908
// get pSrc to be 2-byte aligned
1909-
if ((((int)pSrc) & 0x1) != 0) {
1909+
if ((((size_t)pSrc) & 0x1) != 0) {
19101910
ch = *pSrc;
19111911
pSrc++;
19121912
if (ch > 0x7F) {
@@ -1917,7 +1917,7 @@ class UTF8Encoding
19171917
}
19181918

19191919
// get pSrc to be 4-byte aligned
1920-
if ((((int)pSrc) & 0x2) != 0) {
1920+
if ((((size_t)pSrc) & 0x2) != 0) {
19211921
ch = *(USHORT*)pSrc;
19221922
if ((ch & 0x8080) != 0) {
19231923
goto LongCodeWithMask16;
@@ -2740,7 +2740,7 @@ class UTF8Encoding
27402740
}
27412741

27422742
// get pSrc aligned
2743-
if (((int)pSrc & 0x2) != 0) {
2743+
if (((size_t)pSrc & 0x2) != 0) {
27442744
ch = *pSrc;
27452745
pSrc++;
27462746
if (ch > 0x7F) // Not ASCII

src/pal/src/safecrt/memcpy_s.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ errno_t __cdecl memcpy_s(
5454
size_t sizeInBytes,
5555
const void * src,
5656
size_t count
57-
)
57+
) THROW_DECL
5858
{
5959
if (count == 0)
6060
{

src/pal/src/sync/cs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ namespace CorUnix
627627
pPalCriticalSection->LockCount = 0;
628628
pPalCriticalSection->RecursionCount = 0;
629629
pPalCriticalSection->SpinCount = dwSpinCount;
630-
pPalCriticalSection->OwningThread = NULL;
630+
pPalCriticalSection->OwningThread = 0;
631631
pPalCriticalSection->LockSemaphore = NULL;
632632
pPalCriticalSection->fInternal = fInternal;
633633

@@ -877,7 +877,7 @@ namespace CorUnix
877877
}
878878

879879
// Reset CS ownership
880-
pPalCriticalSection->OwningThread = NULL;
880+
pPalCriticalSection->OwningThread = 0;
881881

882882
// Load the current LockCount value
883883
lVal = pPalCriticalSection->LockCount;

0 commit comments

Comments
 (0)