node/lib/metasm/metasm/exe_format/elf_encode.rb
def arch_386_create_reloc(section, off, binding, rel=nil)
		rel ||= section.encoded.reloc[off]
		if rel.endianness != @endianness or not [:u32, :i32, :a32].include? rel.type
			puts "ELF: 386_create_reloc: ignoring reloc #{rel.target} in #{section.name}: bad reloc type" if $VERBOSE
			return
		end
		startaddr = label_at(@encoded, 0)
		r = Relocation.new
		r.offset = Expression[label_at(section.encoded, 0, 'sect_start'), :+, off]
		if Expression[rel.target, :-, startaddr].bind(binding).reduce.kind_of?(::Integer)
			# this location is relative to the base load address of the ELF
			r.type = 'RELATIVE'
		else
			et = rel.target.externals
			extern = et.find_all { |name| not binding[name] }
			if extern.length != 1
				puts "ELF: 386_create_reloc: ignoring reloc #{rel.target} in #{section.name}: #{extern.inspect} unknown" if $VERBOSE
				return
			end
			if not sym = @symbols.find { |s| s.name == extern.first }
				puts "ELF: 386_create_reloc: ignoring reloc #{rel.target} in #{section.name}: undefined symbol #{extern.first}" if $VERBOSE
				return
			end
			r.symbol = sym
			rel.target = Expression[rel.target, :-, sym.name]
			if rel.target.bind(binding).reduce.kind_of? ::Integer
				r.type = '32'
			elsif Expression[rel.target, :+, label_at(section.encoded, 0)].bind(section.encoded.binding).reduce.kind_of? ::Integer
				rel.target = Expression[[rel.target, :+, label_at(section.encoded, 0)], :+, off]
				r.type = 'PC32'
			# TODO tls ?
			else
				puts "ELF: 386_create_reloc: ignoring reloc #{sym.name} + #{rel.target}: cannot find matching standard reloc type" if $VERBOSE
				return
			end
		end
		@relocations << r
	end
# references to FUNC symbols are transformed to JMPSLOT relocations (aka call to .plt)
# TODO ET_REL support
references to FUNC symbols are transformed to JMPSLOT relocations (aka call to .plt)
TODO ET_REL support
references to FUNC symbols are transformed to JMPSLOT relocations ( aka call to . plt ) TODO ET_REL support
def arch_386_create_reloc(section, off, binding, rel=nil)
		rel ||= section.encoded.reloc[off]
		if rel.endianness != @endianness or not [:u32, :i32, :a32].include? rel.type
			puts "ELF: 386_create_reloc: ignoring reloc #{rel.target} in #{section.name}: bad reloc type" if $VERBOSE
			return
		end
		startaddr = label_at(@encoded, 0)
		r = Relocation.new
		r.offset = Expression[label_at(section.encoded, 0, 'sect_start'), :+, off]
		if Expression[rel.target, :-, startaddr].bind(binding).reduce.kind_of?(::Integer)
			r.type = 'RELATIVE'
		else
			et = rel.target.externals
			extern = et.find_all { |name| not binding[name] }
			if extern.length != 1
				puts "ELF: 386_create_reloc: ignoring reloc #{rel.target} in #{section.name}: #{extern.inspect} unknown" if $VERBOSE
				return
			end
			if not sym = @symbols.find { |s| s.name == extern.first }
				puts "ELF: 386_create_reloc: ignoring reloc #{rel.target} in #{section.name}: undefined symbol #{extern.first}" if $VERBOSE
				return
			end
			r.symbol = sym
			rel.target = Expression[rel.target, :-, sym.name]
			if rel.target.bind(binding).reduce.kind_of? ::Integer
				r.type = '32'
			elsif Expression[rel.target, :+, label_at(section.encoded, 0)].bind(section.encoded.binding).reduce.kind_of? ::Integer
				rel.target = Expression[[rel.target, :+, label_at(section.encoded, 0)], :+, off]
				r.type = 'PC32'
			else
				puts "ELF: 386_create_reloc: ignoring reloc #{sym.name} + #{rel.target}: cannot find matching standard reloc type" if $VERBOSE
				return
			end
		end
		@relocations << r
	end
def arch_386_create_reloc ( section , off , binding , rel = nil ) rel ||= section . encoded . reloc [ off ] if rel . endianness != @endianness or not [ :u32 , :i32 , :a32 ] . include? rel . type puts "ELF: 386_create_reloc: ignoring reloc #{rel.target} in #{section.name}: bad reloc type" if $VERBOSE return end startaddr = label_at ( @encoded , 0 ) r = Relocation . new r . offset = Expression [ label_at ( section . encoded , 0 , 'sect_start' ) , :+ , off ] if Expression [ rel . target , :- , startaddr ] . bind ( binding ) . reduce . kind_of? ( :: Integer ) r . type = 'RELATIVE' else et = rel . target . externals extern = et . find_all { | name | not binding [ name ] } if extern . length != 1 puts "ELF: 386_create_reloc: ignoring reloc #{rel.target} in #{section.name}: #{extern.inspect} unknown" if $VERBOSE return end if not sym = @symbols . find { | s | s . name == extern . first } puts "ELF: 386_create_reloc: ignoring reloc #{rel.target} in #{section.name}: undefined symbol #{extern.first}" if $VERBOSE return end r . symbol = sym rel . target = Expression [ rel . target , :- , sym . name ] if rel . target . bind ( binding ) . reduce . kind_of? :: Integer r . type = '32' elsif Expression [ rel . target , :+ , label_at ( section . encoded , 0 ) ] . bind ( section . encoded . binding ) . reduce . kind_of? :: Integer rel . target = Expression [ [ rel . target , :+ , label_at ( section . encoded , 0 ) ] , :+ , off ] r . type = 'PC32' else puts "ELF: 386_create_reloc: ignoring reloc #{sym.name} + #{rel.target}: cannot find matching standard reloc type" if $VERBOSE return end end @relocations << r end
references to FUNC symbols are transformed to JMPSLOT relocations (aka call to .plt)
TODO ET_REL support
references to FUNC symbols are transformed to JMPSLOT relocations ( aka call to . plt ) TODO ET_REL support
# this location is relative to the base load address of the ELF # TODO tls ?
section
off
binding
rel