https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108345
Bug ID: 108345
Summary: Mismatch __attribute__((aligned(x))) between
declaration and definition does not raise
error/warning
Product: gcc
Version: 10.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: dumoulin.thibaut at gmail dot com
Target Milestone: ---
If `__attribute__((aligned(x)))` is present in function declaration but NOT
present in function implementation, GCC does not fire any warning/error.
Example:
```
#include <stdint.h>
typedef uint32_t _type_unaligned __attribute__((aligned(1)));
/* Prototype with attribute aligned */
void function_a(_type_unaligned* a);
int main(void) {
struct test_t {
uint8_t a;
uint32_t b;
uint32_t c;
} __attribute__((__packed__)) test = {.a = 0, .b = 1, .c = 2};
function_a(&(test.b));
return 0;
}
/* Declaration WITHOUT attribute aligned */
void function_a(uint32_t* a) {
uint32_t _a = *(a + 0);
uint32_t _b = *(a + 1);
*(a + 3) = _a + _b;
}
```
`arm-none-eabi-gcc --specs=nosys.specs -mcpu=cortex-m4 -Wall -Wextra -O3
-mthumb -mlittle-endian`
```
$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (15:10.3-2021.07-4) 10.3.1 20210621 (release)
```
Assembly generated for function_a:
```
0000811c <function_a>:
811c: e9d0 3200 ldrd r3, r2, [r0] --> illegal ARM V7
instruction if unaligned!
8120: 4413 add r3, r2
8122: 60c3 str r3, [r0, #12]
8124: 4770 bx lr
8126: bf00 nop
```
Header and implementation of the function mismatch about `__attribute__` and
this is misleading. Here, GCC does NOT generate code to align the pointer in
`function_a` implementation.
Correct code would be:
```
/* Declaration with attribute aligned */
void function_a(_type_unaligned* a) {
uint32_t _a = *(a + 0);
uint32_t _b = *(a + 1);
*(a + 3) = _a + _b;
}
```
Usually, when function declaration and definition mismatch, GCC fires a
warning/error.
I think this is a bug here, GCC should not allow to compile this code or at
least should raise a warning.