⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coff.py

📁 用python编写的能看TI CCS输出的coff格式out文件
💻 PY
📖 第 1 页 / 共 2 页
字号:
                Symbol.Auxiliary = Auxiliary
            Symbols.append(Symbol)
        tmp.close()
        
        if FileHeaderStruct.optionalHeaderBytes!=0:
            OptionalFileHeader = Struct( self.coffOptionalFileHeader, "Option File Header" )
            OptionalFileHeader.feed( f )

        SectionHeaders = [None] *  FileHeaderStruct.sectionHeadersNumber
        for i in range(FileHeaderStruct.sectionHeadersNumber):
                SectionHeaders[i] = Struct( self.coffSectionHeader, "Section %d Header" % i)
                SectionHeaders[i].feed( f )
                if SectionHeaders[i].rawDataPointer != 0:
                    # read raw data
                    rawdata = self.readFile(SectionHeaders[i].rawDataPointer, SectionHeaders[i].sectionBytes)
                    SectionHeaders[i].rawdata = rawdata
                    relocaitonNum = SectionHeaders[i].relocationEntriesNumber
                    Relocations = [None] * relocaitonNum
                   
                if SectionHeaders[i].relocationEntriesPointer!=0:
                    # read relocation information
                    tmp = open(self.coffFileName, "rb")
                    tmp.seek( SectionHeaders[i].relocationEntriesPointer )
                    for j in range( relocaitonNum ):
                        Relocations[j] = Struct( self.coffRelocationInformation, "Relocation %d" % j)
                        Relocations[j].feed( tmp )
                        if j<2:
                            print Relocations[j]
                    SectionHeaders[i].Relocations = Relocations
                    tmp.close()

        self.FileHeaderStruct = FileHeaderStruct
        self.StringTable = StringTable
        self.Symbols = Symbols
        self.SectionHeaders = SectionHeaders        
        try:
            self.OptionalFileHeader = OptionalFileHeader
        except:
            pass

            
if __name__ == "__main__":    
    from Tkinter import *
    from tkFont import *
    from Tree import Node, Tree      
    from tkFileDialog   import askopenfilename
    
    f = None
       
    root = Tk()
    
    def makeTree(label):
        global t, sb
        try:
            t.grid_forget()
        except:
            pass
        try:
            sb.grid_forget()
        except:
            pass
        t=Tree(master=frame,
               root_id="<root>",
               root_label=label,
               get_contents_callback=get_contents,
               width=200, node_class=MyNode)
        t.grid(row=0, column=0, sticky='nsew')

        # make expandable
        root.grid_rowconfigure(0, weight=1)
        root.grid_columnconfigure(0, weight=1)

        # add scrollbars
        sb=Scrollbar(frame)
        sb.grid(row=0, column=1, sticky='ns')
        t.configure(yscrollcommand=sb.set)
        sb.configure(command=t.yview)

        sb=Scrollbar(frame, orient=HORIZONTAL)
        sb.grid(row=1, column=0, sticky='ew')
        t.configure(xscrollcommand=sb.set)
        sb.configure(command=t.xview)    
    
    def openCoffFile():
        global f, t, root
        filename = askopenfilename(defaultextension=".out", filetypes=[("coff files",".out")], title="Open an out file")
        f = CoffFile( filename )
        f.read()
        makeTree(filename)
        root.title("COFF reader - " + filename)
        
    def ClearTextArea():
        global text
        text.delete(1.0, END)
        
    def makeHexDataTable(header, rawdata):
        rawdatalist = [header, "-"*48]
        i = 0
        while i < len( rawdata ):
            rawdatalist.append( " ".join([hexlify(x) for x in rawdata[i:i+16]]))
            i += 16
        return "\n".join(rawdatalist)
        
    class MyNode(Node):
        def __init__(self, *args, **kw_args):
            apply(Node.__init__, (self,)+args, kw_args)
            self.widget.tag_bind(self.label, '<1>', self.onClick)
        def onClick(self, event):
            if self.id == "<Header>":
                insertstr = str(f.FileHeaderStruct)
            if self.id == "<OptionalHeader>":
                insertstr = str(f.OptionalFileHeader)
            if self.id.startswith("<Symbol>-"):
                symbolid = int(self.id.split("-")[1])
                symbol = [s for s in f.Symbols if s.name == "Symbol %d" % symbolid][0]
                insertstr = str(symbol)
            if self.id.startswith("<SymbolAddr>-"):
                symbolid = int(self.id.split("-")[1])
                symbol = [s for s in f.Symbols if s.name == "Symbol %d" % symbolid][0]
                header = symbol.symbolName + " at " + ("0x%0x" % symbol.symbolValue)
                symbolSection = f.SectionHeaders[ symbol.sectionNumber - 1]
                start = symbol.symbolValue - symbolSection.physicalAddress
                rawdata = symbolSection.rawdata[start:]
                insertstr = makeHexDataTable(header, rawdata)
            if self.id.startswith("<Section>-"):
                sectionid = int(self.id.split("-")[1])
                insertstr = str(f.SectionHeaders[sectionid])
            if self.id.startswith("<Rawdata>-"):
                sectionid = int(self.id.split("-")[1])
                rawdata = f.SectionHeaders[sectionid].rawdata
                insertstr = makeHexDataTable(f.SectionHeaders[sectionid].sectionName, rawdata)
            try:    
                text.insert(END, insertstr + "\n\n")
                text.yview(MOVETO, 1)
            except:
                pass                
            self.PVT_click(event)


    def get_contents(node):
        if f == None:
            return
        if node.id == "<root>":
            node.widget.add_node(name="Header", id="<Header>", flag=0)
            if hasattr(f, "OptionalFileHeader"):
                node.widget.add_node(name="Optional Header", id="<OptionalHeader>", flag=0)
            node.widget.add_node(name="Sections", id="<Sections>", flag=1)
            node.widget.add_node(name="Symbols", id="<Symbols>", flag=1)
        
        if node.id == "<Sections>":
            for i, section in enumerate(f.SectionHeaders):
                node.widget.add_node(name=section.sectionName, id="<Section>-%d" % i, flag = 1)
                
        if node.id.startswith("<Section>-"):
            sectionid = int(node.id.split("-")[1])
            if hasattr(f.SectionHeaders[sectionid],"rawdata"):
                node.widget.add_node(name="raw_data", id="<Rawdata>-%d" % sectionid, flag = 0)
        
        if node.id == "<Symbols>":
            from sets import Set
            sectionSet = Set()
            for s in f.Symbols:
                sectionSet.add(s.sectionNumber)
            sectionlist = list(sectionSet)
            sectionlist.sort()
            for i in sectionlist:
                if i >= 1:
                    sectionname = f.SectionHeaders[i-1].sectionName
                else:
                    sectionname = str(i)
                node.widget.add_node(name=sectionname, id="<Symbols>_%d" % i, flag = 1)
                
        if node.id.startswith("<Symbol>-"):
            symbolid = node.id.split("-")[1]
            node.widget.add_node(name="data at Symbol Address", id="<SymbolAddr>-" + symbolid, flag = 0)
                
        if node.id.startswith("<Symbols>_"):
            sectionid = int(node.id.split("_")[1])
            sectionSymbols = [s for s in f.Symbols if s.sectionNumber==sectionid]
            symbolSection = f.SectionHeaders[sectionid-1]
            if hasattr(symbolSection,"rawdata"):
                flag = 1
            else:
                flag = 0
            for symbol in sectionSymbols:
                symbolid = symbol.name.replace("Symbol ","<Symbol>-")
                node.widget.add_node(name=symbol.symbolName, id=symbolid, flag = flag)
                
    menu = Frame(root)
    menu.pack(side = TOP, anchor=NW)
    openbutton = Button(menu, text="Open", command=openCoffFile)
    openbutton.pack(side=LEFT)
    clearbutton = Button(menu, text="Clear", command=ClearTextArea)
    clearbutton.pack(side=LEFT)
    font = Font( family = "Courier New", size=14 )
    
    frame = Frame(root)
    frame.pack(side=TOP)
    textscrollbar = Scrollbar(frame)
    textscrollbar.grid(row=0, column=3, sticky='ns')
    text = Text(frame, yscrollcommand = textscrollbar.set, font = font, width = 60)
    text.grid(row=0, column=2, sticky="nsew")
    textscrollbar.configure(command = text.yview)    
    root.title("COFF reader")
    mainloop()    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -