[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v9 2/5] xen: introduce SYMBOLS_SUBTRACT and SYMBOLS_COMPARE



Introduce two macros meant used everywhere symbols such as _stext and
_etext are compared or subtracted in the code.

They are needed because the C standard forbids for both comparisons and
substraction (see C Standard, 6.5.6 [ISO/IEC 9899:2011] and [1]) between
pointers pointing to different objects. _stext, _etext, etc. are all
pointers to different objects from ANCI C point of view.

[1] 
https://wiki.sei.cmu.edu/confluence/display/c/ARR36-C.+Do+not+subtract+or+compare+two+pointers+that+do+not+refer+to+the+same+array

Signed-off-by: Stefano Stabellini <stefanos@xxxxxxxxxx>
CC: JBeulich@xxxxxxxx
CC: andrew.cooper3@xxxxxxxxxx
CC: wei.liu2@xxxxxxxxxx
---
Note that the MACROs don't do any type checking at the moment.
Specifically, they don't check that either parameter is a linker symbol.

---
Changes in v9:
- introduce SYMBOLS_SUBTRACT and SYMBOLS_COMPARE
---
 xen/include/xen/compiler.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/xen/include/xen/compiler.h b/xen/include/xen/compiler.h
index ff6c0f5..43620eb 100644
--- a/xen/include/xen/compiler.h
+++ b/xen/include/xen/compiler.h
@@ -99,6 +99,28 @@
     __asm__ ("" : "=r"(__ptr) : "0"(ptr));      \
     (typeof(ptr)) (__ptr + (off)); })
 
+/*
+ * Calculate (end - start), where start and/or end are linker symbols,
+ * returning a ptrdiff_t. The size is in units of start's referent.
+ */
+#define SYMBOLS_SUBTRACT(end, start) ({                                       \
+    (ptrdiff_t)((uintptr_t)(end) - (uintptr_t)(start)) / sizeof(*start);      \
+})
+
+/*
+ * Given two pointers A, B of arbitrary types, returns the difference
+ * B - A in bytes.  Can be used for comparisons:
+ *   If A < B, returns a negative number
+ *   if A == B, returns zero
+ *   If A > B, returns a positive number
+ *
+ * Legal even if the pointers are to different objects. It can be used
+ * when A and/or B are linker symbols.
+ */
+#define SYMBOLS_COMPARE(A, B) ({                                        \
+    (ptrdiff_t)((uintptr_t)(A) - (uintptr_t)(B));                       \
+})
+
 #ifdef __GCC_ASM_FLAG_OUTPUTS__
 # define ASM_FLAG_OUT(yes, no) yes
 #else
-- 
1.9.1


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.