Method: Elf::Policy::Policy#write_amd64

Defined in:
lib/mithril/policy/dsl.rb

#write_amd64(elffile) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/mithril/policy/dsl.rb', line 117

def write_amd64(elffile)
  factory = ElfStructFactory.instance(:little,64)
  @imported_symbols.each_key {|symbol|
    if elffile.symbols.include?(symbol)
      elffile.symbols[symbol].is_dynamic = true
    else
      elffile.symbols << Elf::Symbol.new(symbol,nil,Elf::STT::STT_OBJECT, 0, Elf::STB::STB_GLOBAL, 0).tap {|x|
        x.semantics = Elf::SHN::SHN_UNDEF
      }
    end
  }
  out = factory.elfp_header.new()
  state_ids = {}
  tag_ids = {}
  relocations = []
  states = states()
  @start = states.first unless states.include? @start
  #These have to be filled in the order in which they are written
  #FIXME: Make these aware of double transitions to the same range/ state
  states.each_with_index do |state,index|
    id = index + 2
    id = 1 if @start == state
    out.states << factory.elfp_state.new.tap {|x|
      x.id = id
      x.stackid = 0 
    }
    state_ids[state] = id
    print "State #{state} #{id}\n"
  end
  tag_ids[:default] = 0 
  @tags.each_with_index do |(name,ranges),index|
    tag_ids[name] = index+1
    ranges.each do |data|
      out.tags << factory.elfp_tag.new.tap {|x|
        x.tag = index + 1
        x.addr = 0
        x.siz = 0
      }
      out.tags.last.tap {|x|
        x.addr  = resolve_reference(elffile,relocations,x.addr.offset,data.low)
        if data.high.nil?
          x.siz = resolve_size(elffile,relocations,x.siz.offset,data.low)
        else
          pp "Warning, emitting SIZE symbol with value  #{ data.high.to_i rescue data.high.name}"
          x.siz = resolve_reference(elffile,relocations,x.siz.offset,data.high)
        end
      }
    end
    print "Tag #{name} #{index + 1} \n"
  end
  self.calls.each do |call|
    out.calls << factory.elfp_call.new.tap {|x|
      x.from = state_ids[call.from]
      x.to = state_ids[call.to]
      x.parambytes = call.parambytes
      x.returnbytes = call.parambytes
    }
    out.calls.last.off = resolve_reference(elffile,relocations,out.calls.last.off.offset, call.symbol)
  end
  self.data.values.map(&:values).flatten.each do |data|
    out.data << factory.elfp_data.new.tap {|x|
      x.from = state_ids[data.from]
      x.to = state_ids[data.to]
      x.type = 0
      x.type |= ELFP::ELFP_RW_READ if data.read
      x.type |= ELFP::ELFP_RW_WRITE if data.write
      x.type |= ELFP::ELFP_RW_EXEC if data.exec
      raise RuntimeError.new "Unknown tag #{data.tag}" unless tag_ids.include? data.tag
      x.tag =   tag_ids[data.tag]
      print "#{x.from} to #{x.to} data #{data.tag} = #{x.tag} #{data.read ? "r":" "}#{data.write ? "w":" "}#{data.exec ? "x":" "}\n" 
    }
  end
  out = Elf::ProgBits.new(".elfbac",nil,out.to_binary_s)
  out.align = 8
  out.flags = SHF::SHF_ALLOC | SHF::SHF_WRITE
  out.sect_type = SHT::SHT_PROGBITS
  out.phdr = ElfFlags::PhdrType::PT_ELFBAC
  out.phdr_flags =  ElfFlags::PhdrFlags::PF_R
  relocations.each { |rel|
    rel.section = out
    elffile.relocations << rel
  }
  elffile.progbits << out
  
end