node/lib/metasm/metasm/exe_format/coff_encode.rb
def encode_sections_fixup
		if @optheader.headers_size.kind_of?(::String)
			@encoded.fixup! @optheader.headers_size => @encoded.virtsize
			@optheader.headers_size = @encoded.virtsize
		end
		@encoded.align @optheader.file_align

		baseaddr = @optheader.image_base.kind_of?(::Integer) ? @optheader.image_base : 0x400000
		binding = @encoded.binding(baseaddr)

		curaddr = baseaddr + @optheader.headers_size
		@sections.each { |s|
			# align
			curaddr = EncodedData.align_size(curaddr, @optheader.sect_align)
			if s.rawaddr.kind_of?(::String)
				@encoded.fixup! s.rawaddr => @encoded.virtsize
				s.rawaddr = @encoded.virtsize
			end
			if s.virtaddr.kind_of?(::Integer)
				raise "E: COFF: cannot encode section #{s.name}: hardcoded address too short" if curaddr > baseaddr + s.virtaddr
				curaddr = baseaddr + s.virtaddr
			end
			binding.update s.encoded.binding(curaddr)
			curaddr += s.virtsize

			pre_sz = @encoded.virtsize
			@encoded << s.encoded[0, s.encoded.rawsize]
			@encoded.align @optheader.file_align
			if s.rawsize.kind_of?(::String)
				@encoded.fixup! s.rawsize => (@encoded.virtsize - pre_sz)
				s.rawsize = @encoded.virtsize - pre_sz
			end
		}

		# not aligned ? spec says it is, visual studio does not
		binding[@optheader.image_size] = curaddr - baseaddr if @optheader.image_size.kind_of?(::String)

		# patch the iat where iat_p was defined
		# sort to ensure a 0-terminated will not overwrite an entry
		# (try to dump notepad.exe, which has a forwarder;)
		@imports.find_all { |id| id.iat_p.kind_of?(Integer) }.sort_by { |id| id.iat_p }.each { |id|
			s = sect_at_rva(id.iat_p)
			@encoded[s.rawaddr + s.encoded.ptr, id.iat.virtsize] = id.iat
			binding.update id.iat.binding(baseaddr + id.iat_p)
		} if imports

		@encoded.fill
		@encoded.fixup! binding

		if @optheader.checksum.kind_of?(::String) and @encoded.reloc.length == 1
			# won't work if there are other unresolved relocs
			checksum = self.class.checksum(@encoded.data, @endianness)
			@encoded.fixup @optheader.checksum => checksum
			@optheader.checksum = checksum
		end
	end
# append the section bodies to @encoded, and link the resulting binary
append the section bodies to @encoded, and link the resulting binary
append the section bodies to @encoded and link the resulting binary
def encode_sections_fixup
		if @optheader.headers_size.kind_of?(::String)
			@encoded.fixup! @optheader.headers_size => @encoded.virtsize
			@optheader.headers_size = @encoded.virtsize
		end
		@encoded.align @optheader.file_align
		baseaddr = @optheader.image_base.kind_of?(::Integer) ? @optheader.image_base : 0x400000
		binding = @encoded.binding(baseaddr)
		curaddr = baseaddr + @optheader.headers_size
		@sections.each { |s|
			curaddr = EncodedData.align_size(curaddr, @optheader.sect_align)
			if s.rawaddr.kind_of?(::String)
				@encoded.fixup! s.rawaddr => @encoded.virtsize
				s.rawaddr = @encoded.virtsize
			end
			if s.virtaddr.kind_of?(::Integer)
				raise "E: COFF: cannot encode section #{s.name}: hardcoded address too short" if curaddr > baseaddr + s.virtaddr
				curaddr = baseaddr + s.virtaddr
			end
			binding.update s.encoded.binding(curaddr)
			curaddr += s.virtsize
			pre_sz = @encoded.virtsize
			@encoded << s.encoded[0, s.encoded.rawsize]
			@encoded.align @optheader.file_align
			if s.rawsize.kind_of?(::String)
				@encoded.fixup! s.rawsize => (@encoded.virtsize - pre_sz)
				s.rawsize = @encoded.virtsize - pre_sz
			end
		}
		binding[@optheader.image_size] = curaddr - baseaddr if @optheader.image_size.kind_of?(::String)
		@imports.find_all { |id| id.iat_p.kind_of?(Integer) }.sort_by { |id| id.iat_p }.each { |id|
			s = sect_at_rva(id.iat_p)
			@encoded[s.rawaddr + s.encoded.ptr, id.iat.virtsize] = id.iat
			binding.update id.iat.binding(baseaddr + id.iat_p)
		} if imports
		@encoded.fill
		@encoded.fixup! binding
		if @optheader.checksum.kind_of?(::String) and @encoded.reloc.length == 1
			checksum = self.class.checksum(@encoded.data, @endianness)
			@encoded.fixup @optheader.checksum => checksum
			@optheader.checksum = checksum
		end
	end
def encode_sections_fixup if @optheader . headers_size . kind_of? ( :: String ) @encoded . fixup! @optheader . headers_size => @encoded . virtsize @optheader . headers_size = @encoded . virtsize end @encoded . align @optheader . file_align baseaddr = @optheader . image_base . kind_of? ( :: Integer ) ? @optheader . image_base : 0x400000 binding = @encoded . binding ( baseaddr ) curaddr = baseaddr + @optheader . headers_size @sections . each { | s | curaddr = EncodedData . align_size ( curaddr , @optheader . sect_align ) if s . rawaddr . kind_of? ( :: String ) @encoded . fixup! s . rawaddr => @encoded . virtsize s . rawaddr = @encoded . virtsize end if s . virtaddr . kind_of? ( :: Integer ) raise "E: COFF: cannot encode section #{s.name}: hardcoded address too short" if curaddr > baseaddr + s . virtaddr curaddr = baseaddr + s . virtaddr end binding . update s . encoded . binding ( curaddr ) curaddr += s . virtsize pre_sz = @encoded . virtsize @encoded << s . encoded [ 0 , s . encoded . rawsize ] @encoded . align @optheader . file_align if s . rawsize . kind_of? ( :: String ) @encoded . fixup! s . rawsize => ( @encoded . virtsize - pre_sz ) s . rawsize = @encoded . virtsize - pre_sz end } binding [ @optheader . image_size ] = curaddr - baseaddr if @optheader . image_size . kind_of? ( :: String ) @imports . find_all { | id | id . iat_p . kind_of? ( Integer ) } . sort_by { | id | id . iat_p } . each { | id | s = sect_at_rva ( id . iat_p ) @encoded [ s . rawaddr + s . encoded . ptr , id . iat . virtsize ] = id . iat binding . update id . iat . binding ( baseaddr + id . iat_p ) } if imports @encoded . fill @encoded . fixup! binding if @optheader . checksum . kind_of? ( :: String ) and @encoded . reloc . length == 1 checksum = self . class . checksum ( @encoded . data , @endianness ) @encoded . fixup @optheader . checksum => checksum @optheader . checksum = checksum end end
append the section bodies to @encoded, and link the resulting binary
append the section bodies to @encoded and link the resulting binary
# align # not aligned ? spec says it is, visual studio does not # patch the iat where iat_p was defined # sort to ensure a 0-terminated will not overwrite an entry # (try to dump notepad.exe, which has a forwarder;) # won't work if there are other unresolved relocs