Class: Resedit::LEHeader

Inherits:
ExeHeader show all
Defined in:
lib/resedit/mz/le.rb

Constant Summary collapse

MAGIC =
[0x454C, 0x584C, 0x434C]
HSIZE =
0xAC
HDRDESCR =
[:Magic, :BOrd, :WOrd, :FormatLevel, :CpuType, :OsType, :ModuleVersion, :ModuleFlags, :ModulePages, :EIPObj, :EIP, :ESPObj, :ESP,
:PageSize, :PageShift, :FixupSize, :FixupCsum, :LoaderSize, :LoaderCsum, :ObjectTableOfs, :ObjectsInModule,
:ObjectPageOfs, :ObjectIterOfs, :ResourceTableOfs, :ResourceTableEntries, :ResidentTableOfs, :EntryTableOfs,
:ModuleDirectivesOfs, :ModuleDirectives, :FixupPageOfs, :FixupRecordOfs, :ImportTblOfs, :ImportEntries, :ImportProcOfs,
:PerPageCsumOfs, :DataPagesOfs, :PreloadPages, :NonResTableOfs, :NonResTableLen, :NonResTableCsum, :AutoDSObject,
:DebugInfoOfs, :DebugInfoLen, :InstancePreload, :InstanceDemand, :Heapsize]
HDRUNPACK =
"vCCVvvV*"
HDR_OFFSETS =
[:ObjectTableOfs, :ObjectPageOfs, :ObjectIterOfs, :ResourceTableOfs,:ResidentTableOfs, :EntryTableOfs, :ModuleDirectivesOfs,
:FixupPageOfs, :FixupRecordOfs, :ImportTblOfs, :ImportProcOfs, :PerPageCsumOfs, :DataPagesOfs, :NonResTableOfs, :DebugInfoOfs]

Constants inherited from ExeHeader

ExeHeader::BLK, ExeHeader::PARA

Constants inherited from Changeable

Changeable::COL_CHANGED, Changeable::COL_ORIGINAL, Changeable::HOW_CHANGED, Changeable::HOW_ORIGINAL, Changeable::LOG

Instance Attribute Summary collapse

Attributes inherited from ExeHeader

#exe, #info

Instance Method Summary collapse

Methods inherited from ExeHeader

#_headerTable, #change, #fieldOffset, #fieldSize, #loadInfo, #setBodySize, #setFileSize, #setHeaderSize, #setInfo

Methods inherited from Changeable

#addData, #bytes, #change, #changed?, #colStr, #colVal, #curcol, #dbgdump, #debug, #fix, #getChanges, #getData, #hex, #hexify, #insert, #loadChanges, #parseHow, #revert, #saveChanges, #saveData, #setData, #size, #undo, #unhexify

Constructor Details

#initialize(exe, file, fsize) ⇒ LEHeader

Returns a new instance of LEHeader.



20
21
22
23
24
# File 'lib/resedit/mz/le.rb', line 20

def initialize(exe, file, fsize)
    @_tablesOrig = nil
    @sofs = file.tell()
    super(exe, file, fsize)
end

Instance Attribute Details

#tablesObject (readonly)

Returns the value of attribute tables.



18
19
20
# File 'lib/resedit/mz/le.rb', line 18

def tables
  @tables
end

Instance Method Details

#addPageReloc(pageId, ofs, obj, val) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/resedit/mz/le.rb', line 93

def addPageReloc(pageId, ofs, obj, val)
    return true if pageRelocs(pageId, ofs, obj, val)
    #extend page fixups
    buf = [7, 0x10, ofs, obj, val].pack("CCs<CV")
    #change fixups offsets
    pgs = getData(@info[:FixupPageOfs]+4*(pageId+1), (@info[:ModulePages]-pageId)*4).unpack("V*")
    d = buf.length
    insert(@info[:FixupRecordOfs]+pgs[0], buf)
    pgs = pgs.map{|v| v+d} #fixup sizes change
    change(@info[:FixupPageOfs]+4*(pageId+1), pgs.pack("V*"))
    #fix header
    fixOffsets(@info[:FixupRecordOfs], d)
    @info[:FixupSize]+=d
    setInfo(:FixupSize, @info[:FixupSize])
    @info[:LoaderSize]+=d
    setInfo(:LoaderSize, @info[:LoaderSize])
    @_tables = nil
    mode(HOW_CHANGED)
    rels = pageRelocs(pageId)
end

#addReloc(ofs, trg = nil) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/resedit/mz/le.rb', line 180

def addReloc(ofs, trg=nil)
    mode(HOW_CHANGED)
    trg = trg ? @exe.body.raw2addr(trg) : getData(ofs, 4)
    obj = val = -1
    @tables[:Objects].each_with_index{|o,i|
        obj,val = [i+1,trg-o[1]] if o[1]<=trg && o[0]+o[1]>trg
    }
    raise "Target not found for offset: #{trg.to_i(trg)}" if obj==-1
    psz = @info[:PageSize]
    pgid = ofs / psz
    #puts "Adding reloc #{ofs.to_s(16)} -> #{trg.to_s(16)} #{obj}:#{val.to_s(16)} #{pgid}"
    ofs = ofs % psz
    addPageReloc(pgid, ofs, obj, val)
    addPageReloc(pgid+1, ofs-psz, obj, val) if ofs+4>psz
    @_tables = nil
    mode(HOW_CHANGED)
    @exe.body.clearRelocs()
    return true
end

#addSegment(size) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/resedit/mz/le.rb', line 134

def addSegment(size)
    mode(HOW_CHANGED)
    psz = @info[:PageSize]
    tail = size % psz
    pgs = size / psz + (tail==0 ? 0 : 1)
    tail = psz if tail==0

    #add new object
    last = @tables[:Objects][-1]
    virt = last[1]+last[0]  #lastobject virt base+size
    virt = (virt+psz-1) & ~(psz-1) #align vbase to page size
    obj = [psz*pgs, virt, 0x2047, last[3]+last[4], pgs, 0].pack("V*")  # 32b rwx preloaded object
    insert(@info[:ObjectTableOfs] + 0x18*@info[:ObjectsInModule], obj)
    fixOffsets(@info[:ObjectTableOfs], 0x18)
    setInfo(:ObjectsInModule, @info[:ObjectsInModule]+1)

    #add pages info
    pinfo = ''
    pgs.times{|i|
        pid = @info[:ModulePages]+i+1
        pinfo+=[(pid*@info[:PageSize])<<4].pack("V")
    }
    insert(@info[:ObjectPageOfs]+@info[:ModulePages]*4, pinfo)
    fixOffsets(@info[:ObjectPageOfs], pinfo.length)

    #add empty fixups
    fixofs = 4*@info[:ModulePages]
    fixval = getData(@info[:FixupPageOfs]+fixofs, 4).unpack("V")[0]
    pinfo = [fixval].pack("V") * pgs #end of fixup table <added pages> times
    insert(@info[:FixupPageOfs]+fixofs+4, pinfo)
    fixOffsets(@info[:FixupPageOfs], pinfo.length)
    setInfo(:FixupSize, @info[:FixupSize]+pinfo.length)

    #change pages and tail, fix loader size
    setInfo(:ModulePages, @info[:ModulePages]+pgs)
    setInfo(:PageShift, tail)
    setInfo(:LoaderSize, @info[:ImportTblOfs]-@info[:ObjectTableOfs]+1)

    @_tables = nil
    #reload tables
    mode(HOW_CHANGED)

    return (@info[:ModulePages]-pgs)*@info[:PageSize]
end

#entryObject



67
# File 'lib/resedit/mz/le.rb', line 67

def entry; sprintf("0x%08X", @info[:EIP]) end

#fileSizeObject



66
# File 'lib/resedit/mz/le.rb', line 66

def fileSize(); headerSize()+(@info[:ModulePages]-1)*@info[:PageSize]+@info[:PageShift] end

#fixOffsets(after, val) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/resedit/mz/le.rb', line 125

def fixOffsets(after, val)
    HDR_OFFSETS.each{|ofs|
        next if @info[ofs]==0 or @info[ofs]<=after
        @info[ofs]+=val
        setInfo(ofs, @info[ofs])
    }
end

#headerSizeObject



65
# File 'lib/resedit/mz/le.rb', line 65

def headerSize(); @info[:DataPagesOfs]-@exe.mzSize end

#loadTables(file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/resedit/mz/le.rb', line 26

def loadTables(file)
    if @_tablesOrig==nil
        objtblend = @sofs+@info[:ObjectTableOfs]+@info[:ObjectsInModule]*0x18
        #addData(file, objtblend-file.tell())
        objtblend = @sofs+@info[:ObjectPageOfs]+@info[:ModulePages]*8
        addData(file, objtblend-file.tell())
    end
    tbl = {:Objects => [], :Pages => []}
    ofs = 0
    for i in 0..@info[:ObjectsInModule]-1
        descr = getData(@info[:ObjectTableOfs]+i*0x18, 0x18).unpack("V*")
        tbl[:Objects] += [descr]
        @exe.env.set("seg#{i}".to_sym, ofs.to_s)
        ofs += descr[4]*@info[:PageSize]
    end
    for i in 0..@info[:ModulePages]-1
        tbl[:Pages] += getData(@info[:ObjectPageOfs]+i*4, 4).unpack("V")
    end
    if @_tablesOrig==nil
        @tables = @_tablesOrig = tbl
    end
    return tbl
end

#loadTail(file) ⇒ Object



50
51
52
# File 'lib/resedit/mz/le.rb', line 50

def loadTail(file);
    addData(file, headerSize() + @sofs - file.tell())
end

#mode(how) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/resedit/mz/le.rb', line 54

def mode(how)
    super(how)
    if @mode == HOW_ORIGINAL
        @tables = @_tablesOrig
    else
        @_tables = loadTables(nil) if !@_tables
        @tables = @_tables
    end
end

#pageRelocs(pageId, fixOfs = nil, fixObj = nil, fixVal = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/resedit/mz/le.rb', line 69

def pageRelocs(pageId, fixOfs=nil, fixObj=nil, fixVal=nil)
    def read(pos,cnt, unp); [getData(@info[:FixupRecordOfs]+pos, cnt).unpack(unp), pos+cnt] end
    pgs = getData(@info[:FixupPageOfs]+4*pageId, 8).unpack("V*")
    pgofs = pageId * @info[:PageSize]
    ret = {}
    pos = pgs[0]
    while pos<pgs[1]
        v,pos = read(pos, 5, "CCs<C")
        raise "Unknown fixup type #{v[0]} #{v[1]}" if (v[0]!=7 && v[0]!=2) || (v[1] & ~0x10 !=0 )
        trg, pos=read(pos, v[1]==0x10 ? 4 : 2,v[1]==0x10 ? "V" : "v")
        next if fixOfs!=nil && (v[2]!=fixOfs || (v[1]==0 && fixVal>0xFF))
        if fixOfs
            buf = [fixObj, fixVal].pack("C" + v[1]==0x10 ? "V" : "v")
            change(@info[:FixupRecordOfs]+pos-1, buf)       #change fixup object & value
            return true
        end
        next if v[2]<0
        ret[pgofs+v[2]] = @tables[:Objects][v[3]-1][1]+trg[0]
    end
    return false if fixOfs
    return ret
end


201
202
203
204
205
206
207
208
209
# File 'lib/resedit/mz/le.rb', line 201

def print(what, how=nil)
    ret = super(what, how)
    if what=="tables"
        puts "Objects: #{@tables[:Objects].map{|o| o.map{|v| v.to_s(16)}}}"
        puts "Pages: #{@tables[:Pages].map{|x| x.to_s(16)}}"
        return true
    end
    return ret
end

#read(pos, cnt, unp) ⇒ Object



70
# File 'lib/resedit/mz/le.rb', line 70

def read(pos,cnt, unp); [getData(@info[:FixupRecordOfs]+pos, cnt).unpack(unp), pos+cnt] end

#readRelocsObject



115
116
117
118
119
120
121
122
# File 'lib/resedit/mz/le.rb', line 115

def readRelocs()
    ret = {}
    for i in 0..@info[:ModulePages]-1
        pgofs = i * @info[:PageSize]
        ret[pgofs] = pageRelocs(i)
    end
    return ret
end