- Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathlink.py
33 lines (29 loc) · 1.33 KB
/
link.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#
# This file is part of the micropython-esp32-ulp project,
# https://github.com/micropython/micropython-esp32-ulp
#
# SPDX-FileCopyrightText: 2018-2023, the micropython-esp32-ulp authors, see AUTHORS file.
# SPDX-License-Identifier: MIT
fromuctypesimportstruct, addressof, LITTLE_ENDIAN, UINT16, UINT32
defmake_binary(text, data, bss_size):
ifnotisinstance(text, bytes):
raiseTypeError('text section must be binary bytes')
ifnotisinstance(data, bytes):
raiseTypeError('data section must be binary bytes')
binary_header_struct_def=dict(
magic=0|UINT32,
text_offset=4|UINT16,
text_size=6|UINT16,
data_size=8|UINT16,
bss_size=10|UINT16,
)
header=bytearray(12)
h=struct(addressof(header), binary_header_struct_def, LITTLE_ENDIAN)
# https://github.com/espressif/esp-idf/blob/master/components/ulp/ld/esp32.ulp.ld
# ULP program binary should have the following format (all values little-endian):
h.magic=0x00706c75# (4 bytes)
h.text_offset=12# offset of .text section from binary start (2 bytes)
h.text_size=len(text) # size of .text section (2 bytes)
h.data_size=len(data) # size of .data section (2 bytes)
h.bss_size=bss_size# size of .bss section (2 bytes)
returnbytes(header) +text+data