diff mbox series

[1/2] options: add support for -fpic, -fPIC, -fpie & -fPIE

Message ID 20191028214102.70737-2-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show
Series add support for -mcmodel & -f{pic,PIC,pie,PIE} | expand

Commit Message

Luc Van Oostenryck Oct. 28, 2019, 9:41 p.m. UTC
Do the parsing and output the corresponding predefines.
Follow the same rules as GCC: pic/pie -> 1, PIC/PIE -> 2
and pie/PIE implies pic/PIC.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 lib.c | 24 ++++++++++++++++++++++++
 lib.h |  2 ++
 2 files changed, 26 insertions(+)
diff mbox series

Patch

diff --git a/lib.c b/lib.c
index 75a4f9870..e9e637d29 100644
--- a/lib.c
+++ b/lib.c
@@ -310,6 +310,8 @@  unsigned long fdump_ir;
 int fmem_report = 0;
 unsigned long long fmemcpy_max_count = 100000;
 unsigned long fpasses = ~0UL;
+int fpic = 0;
+int fpie = 0;
 int funsigned_char = UNSIGNED_CHAR;
 
 int preprocess_only;
@@ -483,6 +485,9 @@  static void handle_arch_msize_long_finalize(void)
 static void handle_arch_finalize(void)
 {
 	handle_arch_msize_long_finalize();
+
+	if (fpie > fpic)
+		fpic = fpie;
 }
 
 static const char *match_option(const char *arg, const char *prefix)
@@ -592,6 +597,12 @@  static int handle_switches(const char *ori, const char *opt, const struct flag *
 	return 0;
 }
 
+static int handle_switch_setval(const char *arg, const char *opt, const struct flag *flag, int options)
+{
+	*(flag->flag) = flag->mask;
+	return 1;
+}
+
 
 #define	OPTNUM_ZERO_IS_INF		1
 #define	OPTNUM_UNLIMITED		2
@@ -940,6 +951,10 @@  static struct flag fflags[] = {
 	{ "tabstop=",		NULL,	handle_ftabstop },
 	{ "mem2reg",		NULL,	handle_fpasses,	PASS_MEM2REG },
 	{ "optim",		NULL,	handle_fpasses,	PASS_OPTIM },
+	{ "pic",		&fpic,	handle_switch_setval, 1 },
+	{ "PIC",		&fpic,	handle_switch_setval, 2 },
+	{ "pie",		&fpie,	handle_switch_setval, 1 },
+	{ "PIE",		&fpie,	handle_switch_setval, 2 },
 	{ "signed-char",	&funsigned_char, NULL,	OPT_INVERSE },
 	{ "unsigned-char",	&funsigned_char, NULL, },
 	{ },
@@ -1382,6 +1397,15 @@  static void predefined_macros(void)
 		predefine("__i386", 1, "1");
 		break;
 	}
+
+	if (fpic) {
+		predefine("__pic__", 0, "%d", fpic);
+		predefine("__PIC__", 0, "%d", fpic);
+	}
+	if (fpie) {
+		predefine("__pie__", 0, "%d", fpie);
+		predefine("__PIE__", 0, "%d", fpie);
+	}
 }
 
 static void create_builtin_stream(void)
diff --git a/lib.h b/lib.h
index 18c97b4ff..b3616fd45 100644
--- a/lib.h
+++ b/lib.h
@@ -199,6 +199,8 @@  extern int fmem_report;
 extern unsigned long fdump_ir;
 extern unsigned long long fmemcpy_max_count;
 extern unsigned long fpasses;
+extern int fpic;
+extern int fpie;
 extern int funsigned_char;
 
 extern int arch_m64;