Skip to content

[BLE] Unused Cordio Features Not Removed from Binary #14607

@AGlass0fMilk

Description

@AGlass0fMilk

Description of defect

Related to my work that discovered #14053.

The BLE stack allows a number of features to be disabled to save flash and RAM space. eg: My device will never be a central, so I can remove that part of the BLE stack.

However, it seems that Cordio is not so configurable. I do not require extended advertising and so I disable it and periodic advertising. Thanks to #14111 this now works as it should.

However, I am looking through the output of arm-none-eabi-nm and am finding some extended advertising code symbols are still in the binary even though extended advertising has been disabled. Cordio doesn't appear to have any way to preprocessor these (relatively large) chunks of code out and so the binary size is bloated.

The command I am using is:

arm-none-eabi-nm -S --size-sort --demangle BUILD/my_binary.elf | grep " [Tt] "

And I get some output similar to the following:

[...]
00038ac8 000002dc T strcmp
000373d0 000002e0 T __sfvwrite_r
00020b60 000002e8 T hciEvtProcessCmdCmpl
0000b44c 000002f0 T lctrSlvExtAdvEndOp
00009ae4 00000318 T lctrMstDiscoverRxExtAdvPktPostProcessHandler
00036f9c 00000324 T _fseeko_r
0002eb24 0000033c t BbBleDrvRadioIRQHandler
00003e7c 00000358 t bbMstAuxScanRxCompCback
00018c7c 00000378 T lhciConnEncodeEvtPkt
0001ea2a 00000386 t vli_modSquare_fast
00009dfc 000003be T lctrMstDiscoverRxAuxAdvPktHandler
0000bdc0 000003d8 T lctrMstConnEndOp
00038364 000003e0 T _realloc_r
0001eff0 000003f8 t EccPoint_mult.constprop.0
0003fb0c 00000400 t crc32Table
00018658 0000042c T lhciMstExtScanEncodeEvtPkt
00017108 00000434 T lhciCommonDecodeCmdPkt
00013ab4 00000448 t lctrPackExtAdvHeader
00035470 000004cc t queue_process
00020e48 000004d0 T hciEvtProcessMsg
0000c480 00000558 T lctrSlvConnEndOp
0003796c 00000574 T _malloc_r
00015310 000005b0 T lctrLlcpExecuteCommonSm
0001e460 000005ca t vli_modMult_fast
00030000 00000a3c T mbedtls_internal_sha512_process
0003b210 00000eec T _dtoa_r
00039410 00001b32 T _svfprintf_r

In my testing, I have been able to stub out (comment out the entire function and return a dummy value, if necessary) many of these functions and have saved about 4kB! This was just by targeting the largest functions. There are many smaller, unrelated functions that probably add up to a sizable chunk of flash.

It would be nice if the Cordio stack could be configured so that this code space was not wasted.

You can see some of my tested modifications on this branch:

https://github.com/AGlass0fMilk/mbed-os/tree/cordio-code-bloat

Target(s) affected by this defect ?

Cordio BLE targets

Toolchain(s) (name and version) displaying this defect ?

arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 9-2019-q4-major) 9.2.1 20191025 (release) [ARM/arm-9-branch revision 277599]

What version of Mbed-os are you using (tag or sha) ?

mbed-os-6.9.0

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)

Package             Version     Location
------------------- ----------- -------------------------------------------------------------------
appdirs             1.4.3
asn1ate             0.6.0
attrs               19.3.0
Automat             20.2.0
beautifulsoup4      4.6.3
ble-serial          1.2.1
bleak               0.9.1
bluepy              1.3.0
cbor                1.0.0
certifi             2019.11.28
cffi                1.14.1
chardet             3.0.4
click               7.1
cmsis-pack-manager  0.2.10
cobs                1.1.4
cogapp              3.0.0
colorama            0.3.9
constantly          15.1.0
crc16               0.1.1
crccheck            0.6
cryptography        2.9.2
cycler              0.10.0
dataclasses         0.8
distlib             0.3.1
docopt              0.6.2
ecdsa               0.15
elftools            0.1.0.dev0
fasteners           0.15
filelock            3.0.12
Flask               1.1.2
future              0.16.0
futures             3.1.1
fuzzywuzzy          0.18.0
gitdb               4.0.5
GitPython           3.1.13
grip                4.5.2
hidapi              0.9.0.post2
hyperlink           20.0.1
icetea              1.2.4
idna                2.7
imgtool             1.7.0rc1
importlib-metadata  1.6.0
importlib-resources 5.1.2
incremental         17.5.0
inflection          0.5.1
iniconfig           1.1.1
intelhex            2.2.1
itsdangerous        1.1.0
Jinja2              2.10.3
jsonmerge           1.7.0
jsonschema          2.6.0
junit-xml           1.8
keyrings.alt        4.0.2
kiwisolver          1.2.0
lockfile            0.12.2
Logbook             1.5.3
Mako                1.1.4
manifest-tool       1.5.2
Markdown            3.3.2
MarkupSafe          1.1.1
matplotlib          3.3.0
mbed-cli            1.10.4
mbed-cloud-sdk      2.0.8
mbed-flasher        0.10.1
mbed-greentea       1.7.4
mbed-host-tests     1.5.10
mbed-ls             1.7.12
mbed-os-tools       0.0.15
mbed-tools          7.1.2
milksnake           0.1.5
monotonic           1.5
numpy               1.19.1
packaging           20.4
path-and-address    2.0.1
pc-ble-driver-py    0.14.2
pdoc3               0.9.2
Pillow              7.2.0
pip                 20.2
pkg-resources       0.0.0
pluggy              0.13.1
prettytable         0.7.2
protobuf            3.5.2.post1
psutil              5.6.6
py                  1.9.0
pyasn1              0.2.3
pycparser           2.20
pycryptodome        3.9.8
pyelftools          0.25
Pygments            2.7.1
PyHamcrest          2.0.2
pyparsing           2.4.7
pyrsistent          0.16.0
pyserial            3.4
pytest              6.1.1
python-dateutil     2.8.1
python-dotenv       0.14.0
pyudev              0.22.0
pyusb               1.0.2
PyYAML              4.2b1
requests            2.20.1
semver              2.10.2
setuptools          46.1.3
six                 1.12.0
smmap               3.0.5
soupsieve           2.0
tabulate            0.8.9
toml                0.10.1
tqdm                4.57.0
trollius            2.1.post2
Twisted             20.3.0
txdbus              1.1.2
typing-extensions   3.7.4.3
urllib3             1.24.2
virtualenv          20.4.3
Werkzeug            1.0.1
wheel               0.34.2
wrapt               1.12.1
yattag              1.13.2
zipp                3.1.0
zope.interface      5.2.0

How is this defect reproduced ?

Build a BLE app with extended advertising features disabled. Analyze the binary using the above command. Extended advertising-related link layer functions will still be present in the binary despite never being used.

@pan- @paul-szczepanek-arm @noonfom

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions