Joseph Keane
Joseph Keane

Reputation: 321

Unexpected bloat of .text in Microblaze embedded C++ program when using new/static variables

I'm creating an embedded C++ program for a Microblaze Microcontroller System. The IP only supports up to 128Kb of memory. I want to allocate memory on the heap, but as soon as I use the new keyword, the .text section (code length) instantly bloats by 178Kb (I'm guessing from standard library includes, but I don't recognise a lot of the names).

My questions are:

  1. Is it normal for new to bloat the program so much
  2. If so, what alternatives are there? Is there a way to initialize a class on the heap using malloc?

Example code with/without bloat:

// .text 3,844 B
int main() {
    return 0;
}

// .text 179,020 B
int main() {
    new int;
    return 0;
}

Some of the larger symbols added after adding new:

00005054 l     F .text  000010c0 execute_stack_op
0013614 l     F .text   00003540 d_print_comp_inner
0001b934 g     F .text  00002ff4 _svfprintf_r
00024320 g     F .text  000015f4 _svfiprintf_r
000262c0 g     F .text  000018ec _vfiprintf_r

Edit: This also seems to occur when using local static variables with constructors

class A {
public:
    A(){}
};

// .text 3,804 B
int main() {
    A a;
    return 0;
}

// .text 179,056 B
int main() {
    static A a;
    return 0;
}

System: Windows 11

Target: Microblaze MCS 128Kb, Nexys A7-100T

Toolchain: Vitis 2024.1, mb-g++ 12.2.0, standalone toolchain (no OS)

Flags: mb-g++.exe -isystem <include-dirs> -O2 -mlittle-endian -mno-xl-reorder -mxl-soft-mul -mcpu=v11.0 -DSDT -MMD -MP -specs=<spec-path> -I<include-path> -Wall -Wextra -Os -fno-exceptions -fno-rtti -U__clang__ -MD -MT <obj> -MF <obj.d> -o <obj> -c <src-file>

And yes, it is strange that -O2 and -Os are both being set - I can control the -Os, but the -O2 is automatically added by Vitis. Note that -Os -fno-exceptions -fno-rtti were added after comment suggestions - -O0 may be needed to get the new example to work, but -Os also runs into issues in the static example and if the memory is actually used.

Upvotes: 1

Views: 102

Answers (0)

Related Questions