Split MAYBE_STATIC to _BSS and _NONZERO variants

These are required to cover the absensce of .data and
.bss sections in some programs, most notably ARCH_X86
in execute-in-place with cache-as-ram.

Change-Id: I80485ebac94b88c5864a949b17ad1dccdfda6a40
Signed-off-by: Kyösti Mälkki <kyosti.malkki@gmail.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/35003
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
diff --git a/src/device/device_const.c b/src/device/device_const.c
index c472aea..0712d0c 100644
--- a/src/device/device_const.c
+++ b/src/device/device_const.c
@@ -204,7 +204,7 @@
 DEVTREE_CONST struct bus *pci_root_bus(void)
 {
 	DEVTREE_CONST struct device *pci_domain;
-	MAYBE_STATIC DEVTREE_CONST struct bus *pci_root = NULL;
+	MAYBE_STATIC_BSS DEVTREE_CONST struct bus *pci_root = NULL;
 
 	if (pci_root)
 		return pci_root;
diff --git a/src/ec/google/chromeec/ec_boardid.c b/src/ec/google/chromeec/ec_boardid.c
index 01e3c90..1307ce1 100644
--- a/src/ec/google/chromeec/ec_boardid.c
+++ b/src/ec/google/chromeec/ec_boardid.c
@@ -18,7 +18,7 @@
 
 uint32_t board_id(void)
 {
-	MAYBE_STATIC uint32_t id = BOARD_ID_INIT;
+	MAYBE_STATIC_NONZERO uint32_t id = BOARD_ID_INIT;
 
 	if (id == BOARD_ID_INIT) {
 		if (google_chromeec_get_board_version(&id))
diff --git a/src/ec/google/chromeec/ec_lpc.c b/src/ec/google/chromeec/ec_lpc.c
index feea9dd..1d7e7ee 100644
--- a/src/ec/google/chromeec/ec_lpc.c
+++ b/src/ec/google/chromeec/ec_lpc.c
@@ -401,7 +401,7 @@
 
 int google_chromeec_command(struct chromeec_command *cec_command)
 {
-	MAYBE_STATIC int command_version = 0;
+	MAYBE_STATIC_BSS int command_version = 0;
 
 	if (command_version <= 0)
 		command_version = google_chromeec_command_version();
diff --git a/src/ec/google/wilco/boardid.c b/src/ec/google/wilco/boardid.c
index e602e6e..2a7e575 100644
--- a/src/ec/google/wilco/boardid.c
+++ b/src/ec/google/wilco/boardid.c
@@ -18,7 +18,7 @@
 
 uint32_t board_id(void)
 {
-	MAYBE_STATIC uint32_t id = BOARD_ID_INIT;
+	MAYBE_STATIC_NONZERO uint32_t id = BOARD_ID_INIT;
 
 	if (id == BOARD_ID_INIT) {
 		uint8_t ec_id;
diff --git a/src/include/stddef.h b/src/include/stddef.h
index 7cae2e6..a2c9c50 100644
--- a/src/include/stddef.h
+++ b/src/include/stddef.h
@@ -35,11 +35,16 @@
 #define DEVTREE_CONST
 #endif
 
-/* Work around non-writable data segment in execute-in-place romstage on x86. */
-#if defined(__PRE_RAM__) && CONFIG(ARCH_X86)
-#define MAYBE_STATIC
+#if ENV_STAGE_HAS_DATA_SECTION
+#define MAYBE_STATIC_NONZERO static
 #else
-#define MAYBE_STATIC static
+#define MAYBE_STATIC_NONZERO
+#endif
+
+#if ENV_STAGE_HAS_BSS_SECTION
+#define MAYBE_STATIC_BSS static
+#else
+#define MAYBE_STATIC_BSS
 #endif
 
 #ifndef __ROMCC__
diff --git a/src/lib/lzma.c b/src/lib/lzma.c
index eecbb26..71c016e 100644
--- a/src/lib/lzma.c
+++ b/src/lib/lzma.c
@@ -26,7 +26,7 @@
 	int res;
 	CLzmaDecoderState state;
 	SizeT mallocneeds;
-	MAYBE_STATIC unsigned char scratchpad[15980];
+	MAYBE_STATIC_BSS unsigned char scratchpad[15980];
 	const unsigned char *cp;
 
 	memcpy(properties, src, LZMA_PROPERTIES_SIZE);
diff --git a/src/lib/timestamp.c b/src/lib/timestamp.c
index 1319b86..89152fd 100644
--- a/src/lib/timestamp.c
+++ b/src/lib/timestamp.c
@@ -128,7 +128,7 @@
 
 static struct timestamp_table *timestamp_table_get(void)
 {
-	MAYBE_STATIC struct timestamp_table *ts_table = NULL;
+	MAYBE_STATIC_BSS struct timestamp_table *ts_table = NULL;
 	struct timestamp_cache *ts_cache;
 
 	if (ts_table != NULL)
diff --git a/src/mainboard/google/stout/chromeos.c b/src/mainboard/google/stout/chromeos.c
index 44784b4..0914450 100644
--- a/src/mainboard/google/stout/chromeos.c
+++ b/src/mainboard/google/stout/chromeos.c
@@ -71,8 +71,8 @@
  */
 int get_recovery_mode_switch(void)
 {
-	MAYBE_STATIC int ec_in_rec_mode = 0;
-	MAYBE_STATIC int ec_rec_flag_good = 0;
+	MAYBE_STATIC_BSS int ec_in_rec_mode = 0;
+	MAYBE_STATIC_BSS int ec_rec_flag_good = 0;
 
 	if (ec_rec_flag_good)
 		return ec_in_rec_mode;
diff --git a/src/mainboard/intel/glkrvp/boardid.c b/src/mainboard/intel/glkrvp/boardid.c
index 0676eac..1a7b3e3 100644
--- a/src/mainboard/intel/glkrvp/boardid.c
+++ b/src/mainboard/intel/glkrvp/boardid.c
@@ -25,7 +25,7 @@
 
 uint32_t board_id(void)
 {
-	MAYBE_STATIC int id = -1;
+	MAYBE_STATIC_NONZERO int id = -1;
 	if (id < 0) {
 		if (CONFIG(EC_GOOGLE_CHROMEEC))
 			id = variant_board_id();
diff --git a/src/mainboard/intel/glkrvp/variants/baseboard/boardid.c b/src/mainboard/intel/glkrvp/variants/baseboard/boardid.c
index 69a0a91..89a736f 100644
--- a/src/mainboard/intel/glkrvp/variants/baseboard/boardid.c
+++ b/src/mainboard/intel/glkrvp/variants/baseboard/boardid.c
@@ -19,7 +19,7 @@
 
 int variant_board_id(void)
 {
-	MAYBE_STATIC uint32_t id = BOARD_ID_INIT;
+	MAYBE_STATIC_NONZERO uint32_t id = BOARD_ID_INIT;
 
 	if (CONFIG(EC_GOOGLE_CHROMEEC)) {
 		if (id == BOARD_ID_INIT) {
diff --git a/src/mainboard/intel/icelake_rvp/board_id.c b/src/mainboard/intel/icelake_rvp/board_id.c
index b56f047..c0def22 100644
--- a/src/mainboard/intel/icelake_rvp/board_id.c
+++ b/src/mainboard/intel/icelake_rvp/board_id.c
@@ -31,7 +31,7 @@
 /* Get Board ID via EC I/O port write/read */
 int get_board_id(void)
 {
-	MAYBE_STATIC int id = -1;
+	MAYBE_STATIC_NONZERO int id = -1;
 
 	if (id < 0) {
 		if (CONFIG(EC_GOOGLE_CHROMEEC))
diff --git a/src/mainboard/intel/kblrvp/board_id.c b/src/mainboard/intel/kblrvp/board_id.c
index a978e0f..dc5afcf 100644
--- a/src/mainboard/intel/kblrvp/board_id.c
+++ b/src/mainboard/intel/kblrvp/board_id.c
@@ -22,7 +22,7 @@
  */
 int get_ec_boardinfo(void)
 {
-	MAYBE_STATIC int ec_info = -1;
+	MAYBE_STATIC_NONZERO int ec_info = -1;
 	if (ec_info < 0) {
 		uint8_t buffer[2];
 		uint8_t index;
diff --git a/src/security/tpm/tspi/log.c b/src/security/tpm/tspi/log.c
index 4019962..99e6a43 100644
--- a/src/security/tpm/tspi/log.c
+++ b/src/security/tpm/tspi/log.c
@@ -26,7 +26,7 @@
 
 static struct tcpa_table *tcpa_cbmem_init(void)
 {
-	MAYBE_STATIC struct tcpa_table *tclt = NULL;
+	MAYBE_STATIC_BSS struct tcpa_table *tclt = NULL;
 	if (tclt)
 		return tclt;
 
@@ -47,7 +47,7 @@
 
 static struct tcpa_table *tcpa_log_init(void)
 {
-	MAYBE_STATIC struct tcpa_table *tclt = NULL;
+	MAYBE_STATIC_BSS struct tcpa_table *tclt = NULL;
 
 	/* We are dealing here with pre CBMEM environment.
 	 * If cbmem isn't available use CAR or SRAM */
diff --git a/src/soc/intel/baytrail/northcluster.c b/src/soc/intel/baytrail/northcluster.c
index 5f33c86..654d237 100644
--- a/src/soc/intel/baytrail/northcluster.c
+++ b/src/soc/intel/baytrail/northcluster.c
@@ -64,7 +64,7 @@
 
 uint32_t nc_read_top_of_low_memory(void)
 {
-	MAYBE_STATIC uint32_t tolm = 0;
+	MAYBE_STATIC_BSS uint32_t tolm = 0;
 
 	if (tolm)
 		return tolm;
diff --git a/src/soc/intel/braswell/northcluster.c b/src/soc/intel/braswell/northcluster.c
index 8a5f02e4..e3e3aa7 100644
--- a/src/soc/intel/braswell/northcluster.c
+++ b/src/soc/intel/braswell/northcluster.c
@@ -71,7 +71,7 @@
 
 uint32_t nc_read_top_of_low_memory(void)
 {
-	MAYBE_STATIC uint32_t tolm = 0;
+	MAYBE_STATIC_BSS uint32_t tolm = 0;
 
 	if (tolm)
 		return tolm;
diff --git a/toolchain.inc b/toolchain.inc
index 875cb20..4c0408c 100644
--- a/toolchain.inc
+++ b/toolchain.inc
@@ -72,7 +72,7 @@
 # stack use, we use 1.5K as heuristic, assuming that we typically have lots
 # of tiny stack frames and the odd large one.
 #
-# Store larger buffers in BSS, use MAYBE_STATIC to share code with __PRE_RAM__
+# Store larger buffers in BSS, use MAYBE_STATIC_BSS to share code with __PRE_RAM__
 # on x86.
 # Since GCCs detection of dynamic array bounds unfortunately seems to be
 # very basic, you'll sometimes have to use a static upper bound for the