Skip to content

Commit 60aa9e1

Browse files
Jordan Yatesnashif
authored andcommitted
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 <[email protected]>
1 parent 4d181d6 commit 60aa9e1

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

scripts/build/elf_parser.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class ZephyrElf:
117117
"""
118118
def __init__(self, kernel, edt, device_start_symbol):
119119
self.elf = ELFFile(open(kernel, "rb"))
120+
self.relocatable = self.elf['e_type'] == 'ET_REL'
120121
self.edt = edt
121122
self.devices = []
122123
self.ld_consts = self._symbols_find_value(set([device_start_symbol, *Device.required_ld_consts, *DevicePM.required_ld_consts]))
@@ -152,9 +153,13 @@ def symbol_data(self, sym):
152153
length = sym.entry.st_size
153154
# Section associated with the symbol
154155
section = self.elf.get_section(sym.entry['st_shndx'])
155-
offset = addr - section['sh_addr']
156-
# Extract bytes
157-
return bytes(section.data()[offset:offset + length])
156+
data = section.data()
157+
# Relocatable data does not appear to be shifted
158+
offset = addr - (0 if self.relocatable else section['sh_addr'])
159+
# Validate data extraction
160+
assert offset + length <= len(data)
161+
# Extract symbol bytes from section
162+
return bytes(data[offset:offset + length])
158163

159164
def _symbols_find_value(self, names):
160165
symbols = {}

0 commit comments

Comments
 (0)