scripts: build: elf_parser: fix relocatable data offsets

Based on investigations with relocatable `.elf` files, the symbol table
data in relocatable files is not shifted by the section address. Remove
the shift from these files to ensure that data can be pulled from the
symbol table.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
Jordan Yates 2023-05-27 11:24:21 +10:00 committed by Anas Nashif
parent 4d181d6395
commit 60aa9e1857

View file

@ -117,6 +117,7 @@ class ZephyrElf:
"""
def __init__(self, kernel, edt, device_start_symbol):
self.elf = ELFFile(open(kernel, "rb"))
self.relocatable = self.elf['e_type'] == 'ET_REL'
self.edt = edt
self.devices = []
self.ld_consts = self._symbols_find_value(set([device_start_symbol, *Device.required_ld_consts, *DevicePM.required_ld_consts]))
@ -152,9 +153,13 @@ class ZephyrElf:
length = sym.entry.st_size
# Section associated with the symbol
section = self.elf.get_section(sym.entry['st_shndx'])
offset = addr - section['sh_addr']
# Extract bytes
return bytes(section.data()[offset:offset + length])
data = section.data()
# Relocatable data does not appear to be shifted
offset = addr - (0 if self.relocatable else section['sh_addr'])
# Validate data extraction
assert offset + length <= len(data)
# Extract symbol bytes from section
return bytes(data[offset:offset + length])
def _symbols_find_value(self, names):
symbols = {}