1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-17 02:52:12 +01:00

OP-913: ensured that UAVO data structs in .bss/.data sections and on the stack have 4 byte alignment. Also changed linker options to sort common and sort sections by alignment, which reduces the amount of fill/padding in the .bss and .data sections.

+review OPReview-444
This commit is contained in:
Richard Flay (Hyper) 2013-04-23 22:32:38 +09:30
parent b22204e939
commit 146e082e74
4 changed files with 22 additions and 3 deletions

View File

@ -68,6 +68,12 @@ void PIOS_DEBUG_Panic(const char *msg);
#define PIOS_Assert(test) if (!(test)) while (1);
#endif
/* Static (compile-time) assertion for use in a function.
If test evaluates to 0 (ie fails) at compile time then compilation will
fail with the error: "size of unnamed array is negative" */
#define PIOS_STATIC_ASSERT(test) ((void)sizeof(int[1 - 2*!(test)]))
#endif /* PIOS_DEBUG_H */
/**

View File

@ -51,11 +51,19 @@ int32_t $(NAME)Initialize();
UAVObjHandle $(NAME)Handle();
void $(NAME)SetDefaults(UAVObjHandle obj, uint16_t instId);
// Object data
// Packed Object data (unaligned).
// Should only be used where 4 byte alignment can be guaranteed
// (eg a single instance on the heap)
typedef struct {
$(DATAFIELDS)
} __attribute__((packed)) $(NAME)Data;
} __attribute__((packed)) $(NAME)DataPacked;
// Packed Object data.
// Alignment is forced to 4 bytes so as to avoid the potential for CPU usage faults
// on Cortex M4F during load/store of float UAVO fields
typedef $(NAME)DataPacked __attribute__((aligned(4))) $(NAME)Data;
// Typesafe Object access functions
/**
* @function $(NAME)Get(dataOut)

View File

@ -49,6 +49,11 @@ static UAVObjHandle handle __attribute__((section("_uavo_handles")));
*/
int32_t $(NAME)Initialize(void)
{
// Compile time assertion that the $(NAME)DataPacked and $(NAME)Data structs
// have the same size (though instances of $(NAME)Data
// should be placed in memory by the linker/compiler on a 4 byte alignment).
PIOS_STATIC_ASSERT(sizeof($(NAME)DataPacked) == sizeof($(NAME)Data));
// Don't set the handle to null if already registered
if(UAVObjGetByID($(NAMEUC)_OBJID) != NULL)
return -2;

View File

@ -153,7 +153,7 @@ ASFLAGS += -Wa,-adhlns=$(addprefix $(OUTDIR)/, $(notdir $(addsuffix .lst, $(base
# -Map: create map file
# --cref: add cross reference to map file
LDFLAGS += -nostartfiles
LDFLAGS += -Wl,--warn-common,--fatal-warnings,--gc-sections
LDFLAGS += -Wl,--warn-common,--fatal-warnings,--sort-common,--sort-section=alignment,--gc-sections
LDFLAGS += -Wl,-Map=$(OUTDIR)/$(TARGET).map,--cref
LDFLAGS += $(patsubst %,-L%,$(EXTRA_LIBDIRS))
LDFLAGS += $(patsubst %,-l%,$(EXTRA_LIBS))