Cortex-M3 low level assert issue.
Mike Franklin
slavo5150 at yahoo.com
Fri May 3 03:00:43 UTC 2019
On Wednesday, 1 May 2019 at 21:55:09 UTC, Jack Applegame wrote:
> LDC compiles `assert(0)` to `_d_assert(file, line)` call and
> puts file name (test.d) to section `.rodata.str1.1`:
>
> ```
> Contents of section .rodata.str1.1:
> 0000 74657374 2e466f6f 2e426172 00746573 test.Foo.Bar.tes
> 0010 742e466f 6f007465 73742e64 00 t.Foo.test.d.
> ```
>
> The problem is that this section also contains symbols
> `test.Foo.Bar` and `test.Foo`. I don't know why these symbols
> are needed, but they are definitely not used in the program.
> And because they are in the same section as `test.d`, this
> whole section goes into firmware.
>
> In my real application, such useless data occupy an
> unacceptable amount of flash memory of the MCU. It would be
> nice to get rid of them.
If you'd like to see a more complete cortex-m project, please see
https://github.com/JinShil/stm32f42_discovery_demo
You can see how I compile with ldc2 at
https://github.com/JinShil/stm32f42_discovery_demo/blob/2f6419de1c30179a85c07e0bf1e3ddd026ace93c/build.d#L78-L88
The technique I use in that project is to create my own minimal
runtime:
https://github.com/JinShil/stm32f42_discovery_demo/tree/master/source/runtime It just contains the runtime implementations that I need; nothing more.
If you want to have support for asserts, you'll have to implement
the runtime hooks as shown here:
https://github.com/JinShil/stm32f42_discovery_demo/blob/master/source/runtime/exception.d
Note that it's ok for your .o files be full of junk. They can be
stripped out at link time with the `--gc-sections` flag. Just be
sure you're compiling with `-ffunction-sections` and
`-fdata-sections` for `--gc-sections` to work well.
Question for LDC developers: Are the above compiler flags
enabled by default? How can one confirm?
You can start your minimal runtime project with just an empty
`object.d`:
object.d
```
module object;
```
test.d
```
module test;
struct Foo {
struct Bar {
struct Baz {
}
}
}
void foo() {
assert(0);
}
```
```
ldc2 -conf= -c -mtriple=thumb-none--eabi -mcpu=cortex-m3 test.d
arm-none-eabi-size test.o
text data bss dec hex filename
48 0 0 48 30 test.o
```
Compare that without the object.d. When the compiler detects a
local object.d file, it will use it instead of the runtime. The
`-conf=` tells the compiler not to use the default configuration
that imports the runtime that comes bundled with the compiler.
Compile with `-v` and compare with and without the `-conf=` flag
to see the difference.
If this just prompts more questions, let them fly.
Mike
More information about the digitalmars-d-ldc
mailing list