@@ -1281,20 +1281,6 @@ class ESP32H21FirmwareImage(ESP32C6FirmwareImage):
1281
1281
1282
1282
1283
1283
class ELFFile (object ):
1284
- SEC_TYPE_PROGBITS = 0x01
1285
- SEC_TYPE_STRTAB = 0x03
1286
- SEC_TYPE_NOBITS = 0x08 # e.g. .bss section
1287
- SEC_TYPE_INITARRAY = 0x0E
1288
- SEC_TYPE_FINIARRAY = 0x0F
1289
- SEC_TYPE_PREINITARRAY = 0x10
1290
-
1291
- PROG_SEC_TYPES = (
1292
- SEC_TYPE_PROGBITS ,
1293
- SEC_TYPE_INITARRAY ,
1294
- SEC_TYPE_FINIARRAY ,
1295
- SEC_TYPE_PREINITARRAY ,
1296
- )
1297
-
1298
1284
LEN_SEC_HEADER = 0x28
1299
1285
1300
1286
SEG_TYPE_LOAD = 0x01
@@ -1353,6 +1339,22 @@ def _read_elf_file(self, f):
1353
1339
self ._read_segments (f , _phoff , _phnum , shstrndx )
1354
1340
1355
1341
def _read_sections (self , f , section_header_offs , section_header_count , shstrndx ):
1342
+ SEC_TYPE_PROGBITS = 0x01
1343
+ SEC_TYPE_STRTAB = 0x03
1344
+ SEC_TYPE_NOBITS = 0x08 # e.g. .bss section
1345
+ SEC_TYPE_INITARRAY = 0x0E
1346
+ SEC_TYPE_FINIARRAY = 0x0F
1347
+ SEC_TYPE_PREINITARRAY = 0x10
1348
+
1349
+ PROG_SEC_TYPES = (
1350
+ SEC_TYPE_PROGBITS ,
1351
+ SEC_TYPE_INITARRAY ,
1352
+ SEC_TYPE_FINIARRAY ,
1353
+ SEC_TYPE_PREINITARRAY ,
1354
+ )
1355
+
1356
+ KNOWN_SEC_TYPES = PROG_SEC_TYPES + (SEC_TYPE_NOBITS , SEC_TYPE_STRTAB )
1357
+
1356
1358
f .seek (section_header_offs )
1357
1359
len_bytes = section_header_count * self .LEN_SEC_HEADER
1358
1360
section_header = f .read (len_bytes )
@@ -1384,17 +1386,13 @@ def read_section_header(offs):
1384
1386
) = struct .unpack_from ("<LLLLLLLLL" , section_header [offs :])
1385
1387
return (name_offs , sec_type , lma , size , sec_offs , _flags , align )
1386
1388
1387
- all_sections = [read_section_header (offs ) for offs in section_header_offsets ]
1388
- prog_sections = [s for s in all_sections if s [1 ] in ELFFile .PROG_SEC_TYPES ]
1389
- nobits_secitons = [s for s in all_sections if s [1 ] == ELFFile .SEC_TYPE_NOBITS ]
1390
-
1391
1389
# search for the string table section
1392
1390
if (shstrndx * self .LEN_SEC_HEADER ) not in section_header_offsets :
1393
1391
raise FatalError (f"ELF file has no STRTAB section at shstrndx { shstrndx } " )
1394
1392
_ , sec_type , _ , sec_size , sec_offs , _flags , align = read_section_header (
1395
1393
shstrndx * self .LEN_SEC_HEADER
1396
1394
)
1397
- if sec_type != ELFFile . SEC_TYPE_STRTAB :
1395
+ if sec_type != SEC_TYPE_STRTAB :
1398
1396
log .warning (f"ELF file has incorrect STRTAB section type { sec_type :#04x} " )
1399
1397
f .seek (sec_offs )
1400
1398
string_table = f .read (sec_size )
@@ -1410,23 +1408,42 @@ def read_data(offs, size):
1410
1408
f .seek (offs )
1411
1409
return f .read (size )
1412
1410
1413
- prog_sections = [
1414
- ELFSection (
1415
- lookup_string (n_offs ),
1416
- lma ,
1417
- read_data (offs , size ),
1418
- flags = _flags ,
1419
- align = align ,
1420
- )
1421
- for (n_offs , _type , lma , size , offs , _flags , align ) in prog_sections
1422
- if lma != 0 and size > 0
1423
- ]
1424
- self .sections = prog_sections
1425
- self .nobits_sections = [
1426
- ELFSection (lookup_string (n_offs ), lma , b"" , flags = _flags , align = align )
1427
- for (n_offs , _type , lma , size , offs , _flags , align ) in nobits_secitons
1428
- if lma != 0 and size > 0
1429
- ]
1411
+ all_sections = [read_section_header (offs ) for offs in section_header_offsets ]
1412
+
1413
+ self .sections = []
1414
+ self .nobits_sections = []
1415
+ # Process all sections and raise an error if an unknown section type is found
1416
+ for section in all_sections :
1417
+ n_offs , sec_type , lma , size , offs , _flags , align = section
1418
+
1419
+ # Skip sections with lma == 0 or size == 0
1420
+ if lma == 0 or size == 0 :
1421
+ continue
1422
+
1423
+ if sec_type not in KNOWN_SEC_TYPES :
1424
+ log .warning (f"Unknown section type { sec_type :#04x} in ELF file" )
1425
+ continue
1426
+
1427
+ if sec_type in PROG_SEC_TYPES :
1428
+ self .sections .append (
1429
+ ELFSection (
1430
+ lookup_string (n_offs ),
1431
+ lma ,
1432
+ read_data (offs , size ),
1433
+ flags = _flags ,
1434
+ align = align ,
1435
+ )
1436
+ )
1437
+ elif sec_type == SEC_TYPE_NOBITS :
1438
+ self .nobits_sections .append (
1439
+ ELFSection (
1440
+ lookup_string (n_offs ),
1441
+ lma ,
1442
+ b"" ,
1443
+ flags = _flags ,
1444
+ align = align ,
1445
+ )
1446
+ )
1430
1447
1431
1448
def _read_segments (self , f , segment_header_offs , segment_header_count , shstrndx ):
1432
1449
f .seek (segment_header_offs )
0 commit comments