Add option to enable GCC LTO

Add CONFIG_LTO to use GCC Link-Time Optimizations to try to reduce the
flash footprint of the firmware.

Add additional protection to some functions/data to avoid removal by the
linker when their usage is not obvious.

Signed-off-by: Vincent Palatin <vpalatin@chromium.org>

BRANCH=none
BUG=none
TEST=make buildall (with and without LTO enable on all boards)

Change-Id: I586b8c1eda4592b416c85383b65153c1d5ab0059
Reviewed-on: https://chromium-review.googlesource.com/271291
Trybot-Ready: Vincent Palatin <vpalatin@chromium.org>
Tested-by: Vincent Palatin <vpalatin@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Reviewed-by: Alec Berg <alecaberg@chromium.org>
Commit-Queue: Vincent Palatin <vpalatin@chromium.org>
This commit is contained in:
Vincent Palatin
2015-04-27 14:47:19 -07:00
committed by ChromeOS Commit Bot
parent cb29daa58c
commit 2650ff3d70
24 changed files with 51 additions and 25 deletions

View File

@@ -55,7 +55,7 @@ LIBFTDI_LDLIBS=$(shell $(PKG_CONFIG) --libs lib${LIBFTDI_NAME})
BUILD_CFLAGS= $(LIBFTDI_CFLAGS) $(CPPFLAGS) -O3 $(CFLAGS_DEBUG) $(CFLAGS_WARN)
HOST_CFLAGS=$(CPPFLAGS) -O3 $(CFLAGS_DEBUG) $(CFLAGS_WARN) -DHOST_TOOLS_BUILD
LDFLAGS=-nostdlib -Wl,-X -Wl,--gc-sections -Wl,--build-id=none
LDFLAGS=-nostdlib -Wl,-X -Wl,--gc-sections -Wl,--build-id=none $(LDFLAGS_EXTRA)
BUILD_LDFLAGS=$(LIBFTDI_LDLIBS)
HOST_TEST_LDFLAGS=-T core/host/host_exe.lds -lrt -pthread -rdynamic -lm\
$(if $(TEST_COVERAGE),-fprofile-arcs,)

View File

@@ -216,7 +216,7 @@ uint32_t task_wait_event(int timeout_us)
return evt;
}
void cpu_reset(void)
void __keep cpu_reset(void)
{
/* Disable interrupts */
asm volatile("cpsid i");

View File

@@ -73,7 +73,7 @@ int watchdog_init(void)
}
#ifdef CONFIG_WATCHDOG_HELP
void watchdog_check(uint32_t excep_lr, uint32_t excep_sp)
void __keep watchdog_check(uint32_t excep_lr, uint32_t excep_sp)
{
/* Clear status */
MEC1322_TMR16_STS(0) |= 1;

View File

@@ -45,7 +45,7 @@ void watchdog_init_warning_timer(void)
task_enable_irq(ITIM16_INT(ITIM_WDG_NO));
}
void watchdog_check(uint32_t excep_lr, uint32_t excep_sp)
void __keep watchdog_check(uint32_t excep_lr, uint32_t excep_sp)
{
int wd_cnt;
/* Clear timeout status for event */

View File

@@ -115,7 +115,7 @@ int gpio_enable_interrupt(enum gpio_signal signal)
/*****************************************************************************/
/* Interrupt handler */
void gpio_interrupt(void)
void __keep gpio_interrupt(void)
{
int bit;
/* process only GPIO EXTINTs (EXTINT0..15) not other EXTINTs */

View File

@@ -372,7 +372,7 @@ int __hw_clock_source_init(uint32_t start_t)
#ifdef CONFIG_WATCHDOG_HELP
void watchdog_check(uint32_t excep_lr, uint32_t excep_sp)
void __keep watchdog_check(uint32_t excep_lr, uint32_t excep_sp)
{
struct timer_ctlr *timer = (struct timer_ctlr *)TIM_WD_BASE;

View File

@@ -189,7 +189,7 @@ int __hw_clock_source_init(uint32_t start_t)
#define IRQ_WD IRQ_TIM(TIM_WATCHDOG)
void watchdog_check(uint32_t excep_lr, uint32_t excep_sp)
void __keep watchdog_check(uint32_t excep_lr, uint32_t excep_sp)
{
/* clear status */
STM32_TIM_SR(TIM_WATCHDOG) = 0;

View File

@@ -5,6 +5,8 @@
* Software emulation for CLZ instruction
*/
#include "common.h"
/**
* Count leading zeros
*
@@ -12,7 +14,7 @@
* @return the number of leading 0-bits in x,
* starting at the most significant bit position.
*/
int __clzsi2(int x)
int __keep __clzsi2(int x)
{
int r = 0;

View File

@@ -33,7 +33,7 @@
#define CPRINTF(format, args...) cprintf(CC_SYSTEM, format, ## args)
#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ## args)
test_mockable int main(void)
test_mockable __keep int main(void)
{
/*
* Pre-initialization (pre-verified boot) stage. Initialization at

View File

@@ -11,14 +11,14 @@
#include "ec_version.h"
#include "version.h"
const struct version_struct version_data
const struct version_struct __keep version_data
__attribute__((section(".rodata.ver"))) = {
CROS_EC_VERSION_COOKIE1,
CROS_EC_VERSION32,
CROS_EC_VERSION_COOKIE2
};
const char build_info[] __attribute__((section(".rodata.buildinfo"))) =
const char build_info[] __keep __attribute__((section(".rodata.buildinfo"))) =
CROS_EC_VERSION " " DATE " " BUILDER;
uint32_t ver_get_numcommits(void)

View File

@@ -17,6 +17,11 @@ CFLAGS_CPU+=-mthumb -Os -mno-sched-prolog
CFLAGS_CPU+=-mno-unaligned-access
CFLAGS_CPU+=$(CFLAGS_FPU-y)
ifneq ($(CONFIG_LTO),)
CFLAGS_CPU+=-flto
LDFLAGS_EXTRA+=-flto
endif
core-y=cpu.o init.o ldivmod.o uldivmod.o
core-$(CONFIG_COMMON_PANIC_OUTPUT)+=panic.o
core-$(CONFIG_COMMON_RUNTIME)+=switch.o task.o

View File

@@ -24,6 +24,7 @@
#define DECLARE_IRQ(irq, routine, priority) DECLARE_IRQ_(irq, routine, priority)
#define DECLARE_IRQ_(irq, routine, priority) \
void IRQ_HANDLER(irq)(void) __attribute__((naked)); \
void __keep routine(void); \
void IRQ_HANDLER(irq)(void) \
{ \
asm volatile("mov r0, lr\n" \

View File

@@ -302,7 +302,7 @@ void panic_data_print(const struct panic_data *pdata)
#endif
}
void report_panic(void)
void __keep report_panic(void)
{
struct panic_data *pdata = pdata_ptr;
uint32_t sp;
@@ -352,7 +352,7 @@ void report_panic(void)
*
* Declare this as a naked call so we can extract raw LR and IPSR values.
*/
void exception_panic(void) __attribute__((naked));
void __keep exception_panic(void) __attribute__((naked));
void exception_panic(void)
{
/* Save registers and branch directly to panic handler */

View File

@@ -284,7 +284,7 @@ void __schedule(int desched, int resched)
}
#ifdef CONFIG_TASK_PROFILING
void task_start_irq_handler(void *excep_return)
void __keep task_start_irq_handler(void *excep_return)
{
/*
* Get time before checking depth, in case this handler is
@@ -312,7 +312,7 @@ void task_start_irq_handler(void *excep_return)
}
#endif
void task_resched_if_needed(void *excep_return)
void __keep task_resched_if_needed(void *excep_return)
{
/*
* Continue iff a rescheduling event happened or profiling is active,
@@ -414,7 +414,7 @@ void task_enable_irq(int irq)
CPU_NVIC_EN(irq / 32) = 1 << (irq % 32);
}
void task_disable_irq(int irq)
void __keep task_disable_irq(int irq)
{
CPU_NVIC_DIS(irq / 32) = 1 << (irq % 32);
}

View File

@@ -12,7 +12,7 @@
#include "uart.h"
#include "watchdog.h"
void watchdog_trace(uint32_t excep_lr, uint32_t excep_sp)
void __keep watchdog_trace(uint32_t excep_lr, uint32_t excep_sp)
{
uint32_t psp;
uint32_t *stack;

View File

@@ -23,7 +23,7 @@
#asm_op" %0, %0, %2\n" \
" str %0, [%1]\n" \
" cpsie i\n" \
: "=&r" (reg0) \
: "=&b" (reg0) \
: "b" (a), "r" (v) : "cc"); \
} while (0)

View File

@@ -13,6 +13,11 @@ CROSS_COMPILE?=arm-none-eabi-
CFLAGS_CPU+=-mthumb -Os -mno-sched-prolog
CFLAGS_CPU+=-mno-unaligned-access
ifneq ($(CONFIG_LTO),)
CFLAGS_CPU+=-flto
LDFLAGS_EXTRA+=-flto
endif
core-y=cpu.o init.o thumb_case.o div.o lmul.o ldivmod.o uldivmod.o
core-$(CONFIG_COMMON_PANIC_OUTPUT)+=panic.o
core-$(CONFIG_COMMON_RUNTIME)+=switch.o task.o

View File

@@ -104,7 +104,7 @@ void panic_data_print(const struct panic_data *pdata)
print_reg(15, sregs, 6);
}
void report_panic(void)
void __keep report_panic(void)
{
struct panic_data *pdata = pdata_ptr;
uint32_t sp;
@@ -139,7 +139,7 @@ void report_panic(void)
*
* Declare this as a naked call so we can extract raw LR and IPSR values.
*/
void exception_panic(void) __attribute__((naked));
__keep void exception_panic(void) __attribute__((naked));
void exception_panic(void)
{
/* Save registers and branch directly to panic handler */

View File

@@ -181,7 +181,7 @@ int task_start_called(void)
/**
* Scheduling system call
*/
task_ *__svc_handler(int desched, task_id_t resched)
task_ __attribute__((noinline)) *__svc_handler(int desched, task_id_t resched)
{
task_ *current, *next;
#ifdef CONFIG_TASK_PROFILING

View File

@@ -58,6 +58,16 @@
#define __packed __attribute__((packed))
#endif
/*
* Force the toolchain to keep a symbol even with Link Time Optimization
* activated.
*
* Useful for C functions called only from assembly or through special sections.
*/
#ifndef __keep
#define __keep __attribute__((used)) __attribute__((externally_visible))
#endif
/* There isn't really a better place for this */
#define C_TO_K(temp_c) ((temp_c) + 273)
#define K_TO_C(temp_c) ((temp_c) - 273)

View File

@@ -970,6 +970,9 @@
/* Support LPC interface */
#undef CONFIG_LPC
/* Use Link-Time Optimizations to try to reduce the firmware code size */
#undef CONFIG_LTO
/* Presence of a Bosh Sensortec BMM150 magnetometer behind a BMI160. */
#undef CONFIG_MAG_BMI160_BMM150

View File

@@ -134,13 +134,13 @@ void console_has_input(void);
#ifdef CONFIG_CONSOLE_CMDHELP
#define DECLARE_CONSOLE_COMMAND(name, routine, argdesc, shorthelp, longhelp) \
static const char __con_cmd_label_##name[] = #name; \
const struct console_command __con_cmd_##name \
const struct console_command __keep __con_cmd_##name \
__attribute__((section(".rodata.cmds." #name))) \
= {__con_cmd_label_##name, routine, argdesc, shorthelp}
#else
#define DECLARE_CONSOLE_COMMAND(name, routine, argdesc, shorthelp, longhelp) \
static const char __con_cmd_label_##name[] = #name; \
const struct console_command __con_cmd_##name \
const struct console_command __keep __con_cmd_##name \
__attribute__((section(".rodata.cmds." #name))) \
= {__con_cmd_label_##name, routine}
#endif

View File

@@ -231,7 +231,7 @@ int hook_call_deferred(void (*routine)(void), int us);
* order in which hooks are called.
*/
#define DECLARE_HOOK(hooktype, routine, priority) \
const struct hook_data CONCAT4(__hook_, hooktype, _, routine) \
const struct hook_data __keep CONCAT4(__hook_, hooktype, _, routine) \
__attribute__((section(".rodata." STRINGIFY(hooktype)))) \
= {routine, priority}

View File

@@ -209,7 +209,7 @@ void host_packet_receive(struct host_packet *pkt);
/* Register a host command handler */
#define DECLARE_HOST_COMMAND(command, routine, version_mask) \
const struct host_command __host_cmd_##command \
const struct host_command __keep __host_cmd_##command \
__attribute__((section(".rodata.hcmds"))) \
= {routine, command, version_mask}