GCC might generate implicit calls to standard functions, for example
memcpy when copying a struct. Implement these functions upfront, to
ensure that GCC never uses some optimized version, which could do
unaligned device memory accesses. We only implement memcpy and memset
for the moment. The others (memmove, memcmp, ...) can be added in the
future if required.
We also add flags "-ffunction-sections", "-fdata-sections" to GCC, and
"--gc-sections" to ld, in order to avoid linking those functions into
the final image when they aren't used.
Signed-off-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
CPPFLAGS += $(INITRD_FLAGS)
CFLAGS += -Iinclude/ -I$(ARCH_SRC)/include/
CFLAGS += -Wall -fomit-frame-pointer
+CFLAGS += -ffunction-sections -fdata-sections
+LDFLAGS += --gc-sections
-OFILES += boot_common.o ns.o $(GIC) cache.o
+OFILES += boot_common.o ns.o $(GIC) cache.o lib.o
OFILES += $(addprefix $(ARCH_SRC),boot.o stack.o mmu.o $(BOOTMETHOD) utils.o)
all: $(IMAGE)
CLEANFILES = $(IMAGE) $(OFILES) model.lds fdt.dtb
$(IMAGE): $(OFILES) model.lds fdt.dtb $(KERNEL_IMAGE) $(FILESYSTEM)
- $(LD) $(OFILES) -o $@ --script=model.lds
+ $(LD) $(LDFLAGS) $(OFILES) -o $@ --script=model.lds
%.o: %.S Makefile
$(CC) $(CPPFLAGS) -D__ASSEMBLY__ $(CFLAGS) $(DEFINES) -c -o $@ $<
--- /dev/null
+/*
+ * lib.c - Standard utilities that might be needed by GCC
+ *
+ * Copyright (C) 2015 ARM Limited. All rights reserved.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE.txt file.
+ */
+
+#include <stddef.h>
+
+void *memcpy(void *dest, const void *src, size_t n)
+{
+ int i;
+ char *cdest = dest;
+ const char *csrc = src;
+
+ for (i = 0; i < n; i++)
+ cdest[i] = csrc[i];
+
+ return dest;
+}
+
+void *memset(void *s, int c, size_t n)
+{
+ int i;
+ char *cs = s;
+
+ for (i = 0; i < n; i++)
+ cs[i] = c;
+
+ return s;
+}
+
+/* TODO: memmove and memcmp could also be called */
.boot PHYS_OFFSET: {
*(.init)
- *(.text .data .rodata* .bss COMMON)
+ *(.text* .data* .rodata* .bss* COMMON)
*(.vectors)
*(.stack)
*(.pgtables)