Skip to content

Instantly share code, notes, and snippets.

@NWPlayer123
Last active October 28, 2019 01:47
Show Gist options
  • Select an option

  • Save NWPlayer123/4236ff5ab30bfe59051fa072ef16a4e4 to your computer and use it in GitHub Desktop.

Select an option

Save NWPlayer123/4236ff5ab30bfe59051fa072ef16a4e4 to your computer and use it in GitHub Desktop.
'''
GameCube .map goes like this
.init section layout, symbol section header w/e
list all symbols
\r\n\r\n
x12 w/e
then memory map, then linker symbols
so we need to extract all symbols and their section, and then print it out as specified
'''
from os import listdir
data = open("nddemo.elf.map", "rb").read()
data = data.split("\r\n\r\n")
header = data[0]
functions = data[2]
header = header.strip()
header_map = {}
header_data = []
for entry in header.split("\r\n")[1:]:
line = entry.strip()
for i in range(4):
line = line.replace(" ", " ")
segment = int(line.split(":")[0], 16)
segname = line.split(" ")[2]
header_map[segment] = segname
length = int(line.split(" ")[1][:-1], 16)
header_data.append([segment, segname, length])
functions = functions.strip()
function_list = []
for entry in functions.split("\r\n"):
line = entry.strip()
for i in range(4):
line = line.replace(" ", " ")
segment = int(line.split(":")[0], 16)
address = int(line.split(":")[1].split(" ")[0], 16)
symbol = " ".join(line.split(" ")[1:])
function_list.append([segment, address, symbol])
#assuming that IDA is sane and spits out in order
last_segment = 0xFFFFFFFF #placeholder for check
constants = []
segment_starts = [function_list[0][1]]
pos = 0;size = 0
for symbol in function_list:
if symbol[0] != last_segment: #we're in new segment, print header
if symbol[0] != 0 and header_map[symbol[0]] != ".prgend":
if last_segment != 0xFFFFFFFF:
print
segment_starts.append(symbol[1])
print
print("%s section layout" % header_map[symbol[0]]) #".init"
print(" Starting Virtual")
print(" address Size address")
print(" -----------------------")
last_segment = symbol[0]
if symbol[0] != 0 and header_map[symbol[0]] != ".prgend":
print(" %08x %06x %08x %2d %s \t%s" % (symbol[1], 0, symbol[1], 4, symbol[2], "main.o"))
else:
constants.append(symbol)
print;print
print("Memory map:")
print(" " * 19 + "Starting Size File")
print(" " * 19 + "address Offset")
for i in range(len(header_data) - 1):
print("%s %08x %08x %08x" % (header_data[i][1].rjust(17), segment_starts[i], header_data[i][2], 0))
print(" .debug_srcinfo 000000 00000000")
print(" .debug_sfnames 000000 00000000")
print(" .debug 3169b4 00154ad8")
print(" .line 04db98 00106f40")
print;print
print("Linker generated symbols:")
for entry in constants:
print("%s %08x" % (entry[2].lstrip("Abs ").rjust(25), entry[1]))
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment